Presentation is loading. Please wait.

Presentation is loading. Please wait.

Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.

Similar presentations


Presentation on theme: "Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2."— Presentation transcript:

1 Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2

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 from left to right (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 Array-based representation of binary trees Memory savings (i.e., no pointers) Preserve parent-child relationships Store: (i) level by level, and (ii) left to right 0 1 2 4 3 5 6 7 8 9

5 Parent-child relationships: –left child of tree.nodes[index] = tree.nodes[2*index+1] –right child of tree.nodes[index] = tree.nodes[2*index+2] –parent node of tree.nodes[index] = tree.nodes[(index-1)/2] Leaf nodes: –tree.nodes[numElements/2] to tree.nodes[numElements - 1] Array-based representation of binary trees (cont.) (int division)

6 Full or complete trees can be implemented efficiently using an array-based representation (i.e., elements occupy contiguous array slots). “Dummy nodes" are required for trees which are not full or complete. Array-based representation of binary trees (cont.)

7 What is a heap? It is a binary tree with the following properties: Property 1: it is a complete binary tree heap property Property 2: (heap property): the value stored at a node is greater or equal to the values stored at the children

8 Not unique!

9 Largest heap element From Property 2, the largest value of the heap is always stored at the root

10 Heap implementation Heaps are always implemented as arrays!

11 Heap Specification template struct HeapType { void ReheapDown(int, int); void ReheapUp(int, int); ItemType *elements; // dynamic array int numElements; };

12 The ReheapDown function Assumption: heap property is violated at the root of the tree bottom

13 ReheapDown function template void HeapType ::ReheapDown(int root, int bottom) { int maxChild, rightChild, leftChild; leftChild = 2*root+1; rightChild = 2*root+2; if(leftChild <= bottom) { // left child is part of the heap if(leftChild == bottom) // only one child maxChild = leftChild; else { // two children if(elements[leftChild] <= elements[rightChild]) maxChild = rightChild; else maxChild = leftChild; } if(elements[root] < elements[maxChild]) {// compare max child with parent Swap(elements, root, maxChild); ReheapDown(maxChild, bottom); } rightmost node at the last level O(logN)

14 The ReheapUp function bottom Assumption: heap property is violated at the rightmost node of the last level of the tree bottom

15 ReheapUp function template void HeapType ::ReheapUp(int root, int bottom) { int parent; if(bottom > root) { // tree is not empty parent = (bottom-1)/2; if(elements[parent] < elements[bottom]) { Swap(elements, parent, bottom); ReheapUp(root, parent); } O(logN) rightmost node at the last level

16 Priority Queues What is a priority queue? –It is a queue with each element being associated with a "priority" –From the elements in the queue, the one with the highest priority is dequeued first

17 Priority queue specification template class PQType { public: PQType(int); ~PQType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType); void Dequeue(ItemType&); private: int numItems; // num of elements in the queue HeapType heap; int maxItems; // array size };

18 Dequeue: remove the largest element from the heap (1) Copy the bottom rightmost element to the root (2) Delete the bottom rightmost node (3) Fix the heap property by calling ReheapDown

19 Removing the largest element from the heap (cont.)

20

21 template void PQType ::Dequeue(ItemType& item) { item = heap.elements[0]; heap.elements[0] = heap.elements[numItems-1]; numItems--; heap.ReheapDown(0, numItems-1); } Dequeue bottom O(logN)

22 Enqueue: insert a new element into the heap (1) Insert new element in the leftmost place at the bottom level (start new level if last level is full). (2) Fix the heap property by calling ReheapUp.

23 Inserting a new element into the heap (cont.)

24 template void PQType ::Enqueue(ItemType newItem) { numItems++; heap.elements[numItems-1] = newItem; heap.ReheapUp(0, numItems-1]); } Enqueue bottom O(logN)

25 Other Functions template PQType ::PQType(int max) { maxItems = max; heap.elements = new ItemType[max]; numItems = 0; } template PQType ::MakeEmpty() { numItems = 0; } template PQType ::~PQType() { delete [ ] heap.elements; }

26 template bool PQType ::IsFull() const { return numItems == maxItems; } template bool PQType ::IsEmpty() const { return numItems == 0; } Other Functions (cont.)

27 Comparing heaps with other priority queue implementations Priority queue using a sorted list Priority queue using heaps - Remove a key in O(logN) time - Insert a key in O(logN) time 124 O(N) on the average! O(lgN) on the average! Remove a key in O(1) time Insert a key in O(N) time

28 Exercise 46, p. 545


Download ppt "Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2."

Similar presentations


Ads by Google