250 likes | 373 Views
Advanced Data Structures and Algorithms. COSC-600 Lecture notes presentation-5. Queues: (FIFO). These are special case of lists. In a queue, the insertion is done at one end and deletion is done at the other end. Dequeue E nqueue
E N D
Advanced Data Structures and Algorithms COSC-600 Lecture notes presentation-5
Queues: (FIFO) • These are special case of lists. In a queue, the insertion is done at one end and deletion is done at the other end. DequeueEnqueue <---------------- <-----------------
Operations (Queue) • Enqueue …………….O(1) • Dequeue …………...O(1) • IsEmpty………….....O(1) • IsFull………………….O(1) • Enqueue : Inserts an element at the end of the list called the rear. • Dequeue: Deletes the element at the start of the list called as front.
Array Implementation of Queues: In Queue, same like stacks, both the linked list and array implementations give fast O(1) running times for every operation. Example: For each queue data structure, we keep an array “thearray” positions “front” and “back” which represents the ends of the queue, number of elements in the queue “currentsize” • To enqueue an element “X”, increment currentsize and back then set thearray[back]= X. • To dequeue an element , set the return value to thearray[front], decrement currentsize and then increment front. There arises a problem with this implementation, after several times of performing enqueue operation the queue will be full since back is now at the last array index, the next queue would be in a non existing position. The simple solution to this problem is that whenever front or back gets to the end of array, it is wrapped around to the beginning, know as Circular array implementation.
Initial state After enqueue(1) After enqueue(3) After dequeue, which returns 2 After dequeue, which returns 4
After dequeue, which returns 1 After dequeue, which returns 3 and makes queue empty
How to distinguish queue is empty to queue is full? For an array, Use upto n-1 items Use “counter” to keep track of # number of items in a queue If counter=0 then, array is full. If counter=1 then, array is empty. Applications of Queues: There are many algorithms to give efficient running times. 1.Generally all waiting lines which we see around is a queue. For instance, Waiting line at a ticket counter are queues, because they follow first come first serve rule. 2.Calls to large companies are generally placed on a queue when all operators are busy. 3.Jobs sent to a line printer are placed on a queue.
CH#4 TREES Trees are special subset of graphs. A tree is a collection of nodes. It consists of a) a distinguished node called root b) zero or more non empty subtrees T1,T2………. Tk , each of these nodes connected by a directed edge from “r” c)recursive definition root Tk T1 T2
Tree is a directed graph without a cycle 1. each node of a tree has only one parent node except root 2 there is only one “path” from root to each node, no cycle Path: a sequence of nodes n1, n2….. nk such that ni is the parent of ni+1 Length of path: number of edges Depth of ni: length of the path from root to ni Height of ni: length of longest path from ni to leaf node Leaf node: which has no child also called as terminal node
Tree Search (tree traverse) 1) Depth first search(DFS)……………… use stacks 2) Breadth first search(BFS) …………… Use Queues Binary tree: special subset of tree, ordered tree Stmt:1) each son of a vertex is distinguished either as a left son or as right son 2) no vertex has more than one left son nor more than one right son a tree in which no node can have more than two children
Implementation: Note: For a number of nodes begin N, the average height of all possible binary tree is O(). Let F be the # number of nodes with 2 children and H be the # no of nodes with 1 child and L be the # no of nodes with no children then 1) F+H+L = N 2) 2F+H=N-1 from 1 &2, F=L-1
Traversal method for a binary tree 1) pre order: visit + traversal of left subtree + traversal of right subtree 2)In order: traversal of left subtree + visit + traversal of right subtree 3)post order: traversal of left subtree + traversal of right subtree + visit visit: print label of nodes Preorder : ABDGJKCEHFILM Inorder : DJGKBAHECLIMF Postorder: JKGDBHELMIFCA
Postorder: 3 2 4 + * 8 2 * 4 / + Preorder : + * 3 + 2 4 / * 8 2 4 Inorder: ((3*(2+4)+((8*2)/4))
Problem: Generate Algorithm to convert postorder to inorder( postfix to infix) Eg: a b + c d e + * * Algorithm: 1) read a token left to right If token = operand Create a one node tree and push it into a stack If token = operator Pop two trees T1,T2 from stack and from new tree whose root is the operator and whose left and right children are T2 and T1
Solution: First two symbols are operands push them into the stack Next is operator + so a,b are popped and new tree is formed and pushed into stack
Next c,d,e are read and they are placed in stack Next operator + is read so trees are merged
Next operator * is read so we pop two trees and form new tree with * as root
Finally last symbol is read two trees are merged and final tree is left on the stack
Binary search tree: Binary Search Tree: Subset of binary tree. It is a binary tree for a set ‘s’ , s is a labeled binary tree such that each vertex V is labeled by an element l(v) s 1)each vertex ‘u’ in the left subtree of ‘v’ l(u) < l(v) 2)each vertex ‘u’ in the right subtree of ‘v’ l(u) > l(v) 3)each element as, there is exactly one vertex v such that l(v)=a If left subtree < root, root < right subtree then only it is binary search tree and No two node values are same .
Operations: 1)find(search) 2)insert 3)delete 4)Find min/ Find max 5)print