Presentation is loading. Please wait.

Presentation is loading. Please wait.

Heaps A heap is a binary tree that satisfies the following properties: Structure property: It is a complete binary tree Heap-order property: Each node.

Similar presentations


Presentation on theme: "Heaps A heap is a binary tree that satisfies the following properties: Structure property: It is a complete binary tree Heap-order property: Each node."— Presentation transcript:

1 Heaps A heap is a binary tree that satisfies the following properties: Structure property: It is a complete binary tree Heap-order property: Each node satisfies the heap condition: –The key of every node must be smaller than (or equal to) the keys of its children, i.e. n.info  n.left.info and n.info  n.right.info, for all nodes n Note: this is called a min-heap – max-heaps are possible too 1COSC 2P03 Week 6

2 removeMin Algorithm – min-heap temp = root; root = last node; current = root; while current has at least 1 child { smaller = smallest child; if (current < smaller) break; else swap(current, smaller); } return temp; What is the complexity? 2COSC 2P03 Week 6

3 insert Algorithm – min-heap put new node at bottom right of tree and call it current; // trickle up while current < its parent swap (current, parent); What is the complexity? 3COSC 2P03 Week 6

4 Array implementation of Heap (see figs. 6.8 and 6.12) array[0..capacity]: the heap array array[1..currentsize]: elements currently in heap currentsize: number of nodes in heap removeMin: –The smallest element is the root, which is array[1] –The last element (rightmost on lowest level) is array[currentsize] –During trickle-down, the children of the node in array[t] are: Left child: array[2t] Right child: array[2t+1] insert: –Insert in array[currentsize+1] –During trickle-up, the parent of the node in array[t] is array[  t/2  ] 4COSC 2P03 Week 6

5 Transforming a complete binary tree into a heap In a heap, all subtrees are also heaps. Idea: transform all subtrees into heaps, starting from smallest. Note that leaves are already heaps. array[  currentsize/2  ] is the parent of the last node in the heap, i.e. it is the rightmost node that is not a leaf. buildHeap algorithm: for (i =  currentsize/2  ; i >= 1; i--) apply trickle-down to node [i]; COSC 2P03 Week 65

6 buildHeap example COSC 2P03 Week 66 i=559365821419731162653 i=459365821419731162653 i=359365816419731212653 i=259363116419758212653 i=159163121419758362653 End16213126419758365953

7 Heapsort – min heap The results are stored in B[1..n]. buildHeap(A); for(i = 1; i <= n; i++) B[i] = removeMin(A); COSC 2P03 Week 67 (etc)

8 Heapsort – max heap Avoid the use of a second array by storing sorted elements at the end of the array Use a max-heap: –At each step, swap root with last element, then apply trickle down to new root Example – after conversion to max heap: After 1 st swap: After 2 nd swap: COSC 2P03 Week 68


Download ppt "Heaps A heap is a binary tree that satisfies the following properties: Structure property: It is a complete binary tree Heap-order property: Each node."

Similar presentations


Ads by Google