Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.

Similar presentations


Presentation on theme: "Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree."— Presentation transcript:

1 Chapter 9 Heaps and Priority Queues Lecture 18

2 Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree

3 Complete Binary Tree (1) A binary tree that is either full or full through the next-to-last level (2) The last level is full from left to right (i.e., leaves are as far to the left as possible) Complete Binary Tree

4 Examples of Different Types of Binary Trees 4

5 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape must be a complete binary tree. –For each node in the heap, the value stored in that node is greater than or equal to the value in each of its children.

6 Are these Both Heaps? C A T treePtr 50 20 18 30 10

7 Is this a Heap? 70 60 40 30 12 810 tree

8 Not unique!

9 Where is the Largest Element in a Heap Always Found? 70 60 40 30 12 8 tree

10 We Can Number the Nodes Left to Right by Level This Way 70 0 60 1 40 3 30 4 12 2 8 5 tree

11 And use the Numbers as Array Indexes to Store the Trees 70 0 60 1 40 3 30 4 12 2 8 5 tree [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 tree.nodes

12 With Array Representation For any node tree.nodes[index] –its left child is in tree.nodes[index*2 + 1] –right child is in tree.nodes[index*2 + 2] –its parent is in tree.nodes[(index – 1)/2] 12 Leaf nodes: tree.nodes[numElements/2] to tree.nodes[numElements - 1]

13 // HEAP SPECIFICATION // Assumes ItemType is either a built-in simple data // type or a class with overloaded relational operators. template struct HeapType { void ReheapDown ( int root, int bottom ) ; void ReheapUp ( int root, int bottom ) ; ItemType* elements; //ARRAY to be allocated dynamically int numElements ; };

14 ReheapDown bottom

15 9-15 ReheapDown // IMPLEMENTATION OF RECURSIVE HEAP MEMBER FUNCTIONS template void HeapType ::ReheapDown ( int root, int bottom ) // Pre: root is the index of the node that may violate the // heap order property // Post: Heap order property is restored between root and bottom { int maxChild ; int rightChild ; int leftChild ; leftChild = root * 2 + 1 ; rightChild = root * 2 + 2 ;

16 ReheapDown (cont) if ( leftChild <= bottom ) // ReheapDown continued { if ( leftChild == bottom ) maxChild = leftChld; else { if (elements [ leftChild ] <= elements [ rightChild ] ) maxChild = rightChild; else maxChild = leftChild; } if ( elements [ root ] < elements [ maxChild ] ) { Swap ( elements [ root ], elements [ maxChild ] ); ReheapDown ( maxChild, bottom ) ; } } } O(logN)

17 ReheapUp bottom

18 // IMPLEMENTATIONcontinued template void HeapType ::ReheapUp ( int root, int bottom ) // Pre: bottom is the index of the node that may violate the heap // order property. The order property is satisfied from root to // next-to-last node. // Post: Heap order property is restored between root and bottom { int parent ; if ( bottom > root ) { parent = ( bottom - 1 ) / 2; if ( elements [ parent ] < elements [ bottom ] ) { Swap ( elements [ parent ], elements [ bottom ] ); ReheapUp ( root, parent ); } } } O(logN) ReheapUp

19

20 Real-life Priority Queue

21 Priority Queue A priority queue is an ADT with the property that only the highest-priority element can be accessed at any time. Queue Enque an item Item returned has been in the queue the longest amount of time. Priority Queue Enque a pair Item returned has the highest priority.

22 ADT Priority Queue Operations Transformers –MakeEmpty –Enqueue –Dequeue Observers –IsEmpty –IsFull change state observe state

23 Implementation Level An unsorted List –dequeuing would require searching through the entire list An Array-Based Sorted List –Enqueuing is expensive A Reference-Based Sorted List –Enqueuing again is 0(N) A Binary Search Tree –On average, 0(log 2 N) steps for both enqueue and dequeue A Heap –guarantees 0(log 2 N) steps, even in the worst case

24 Class PQType Declaration class FullPQ(){}; class EmptyPQ(){}; template class PQType { public: PQType(int); ~PQType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType newItem); void Dequeue(ItemType& item); private: int length; HeapType items; int maxItems; };

25 Class PQType Function Definitions template PQType ::PQType(int max) { maxItems = max; items.elements = new ItemType[max]; length = 0; } template void PQType ::MakeEmpty() { length = 0; } template PQType ::~PQType() { delete [] items.elements; }

26 Class PQType Function Definitions Dequeue Set item to root element from queue Move last leaf element into root position Decrement length items.ReheapDown(0, length-1) Enqueue Increment length Put newItem in next available position items.ReheapUp(0, length-1)

27 Dequeue

28 Dequeue (cont.) c) ReheapDown

29 Code for Dequeue template void PQType ::Dequeue(ItemType& item) { if (length == 0) throw EmptyPQ(); else { item = items.elements[0]; items.elements[0] = items.elements[length-1]; length--; items.ReheapDown(0, length-1); } }

30 Enqueue

31 Code for Enqueue template void PQType ::Enqueue(ItemType newItem) { if (length == maxItems) throw FullPQ(); else { length++; items.elements[length-1] = newItem; items.ReheapUp(0, length-1); } }

32 Comparison of Priority Queue Implementations EnqueueDequeue HeapO(log 2 N) Linked ListO(N) Binary Search Tree BalancedO(log 2 N) SkewedO(N)


Download ppt "Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree."

Similar presentations


Ads by Google