Presentation is loading. Please wait.

Presentation is loading. Please wait.

Heaps A heap is a binary tree that satisfies the following properties:

Similar presentations


Presentation on theme: "Heaps A heap is a binary tree that satisfies the following properties:"— 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 COSC 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? COSC 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? COSC 2P03 Week 6

4 Array implementation of Heap
array[0..capacity]: the heap array array[1..currentsize]: elements currently in heap currentsize: number of nodes in heap removeMin (see fig for full details): The smallest element is the root, which is array[1] The “last” element (rightmost on lowest level) is array[currentsize] In trickle-down, we need to find the children of a node: Left child of array[t] is array[2t] Right child of array[t] is array[2t+1] COSC 2P03 Week 6

5 Array implementation of Heap – removeMin Example
1 2 3 4 5 6 7 35 10 50 60 40 20 1 2 3 4 5 6 20 35 10 50 60 40 1 2 3 4 5 6 10 35 20 50 60 40 COSC 2P03 Week 6

6 Array implementation of Heap (insert – see fig. 6.8 for full details)
Insert in array[currentsize+1] During trickle-up, we need to find the parent of a node: Parent of array[t] is array[t/2] Example: insert 15 1 2 3 4 5 6 7 10 35 20 50 60 40 15 1 2 3 4 5 6 7 10 35 15 50 60 40 20 COSC 2P03 Week 6

7 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. buildHeap algorithm: for (i = currentsize/2; i >= 1; i--) apply trickle-down to node [i]; COSC 2P03 Week 6

8 buildHeap example i=5 59 36 58 21 41 97 31 16 26 53 i=4 59 36 58 21 41
End 16 21 31 26 41 97 58 36 59 53 COSC 2P03 Week 6

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

10 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 1st swap: After 2nd swap: COSC 2P03 Week 6


Download ppt "Heaps A heap is a binary tree that satisfies the following properties:"

Similar presentations


Ads by Google