Presentation is loading. Please wait.

Presentation is loading. Please wait.

1a) Create a max-heap by inserting the following integers into an initially empty heap (one by one): 6, 2, 4, 1, 8, 5, 3, 7, 9 We will refer to the resulting.

Similar presentations


Presentation on theme: "1a) Create a max-heap by inserting the following integers into an initially empty heap (one by one): 6, 2, 4, 1, 8, 5, 3, 7, 9 We will refer to the resulting."— Presentation transcript:

1 1a) Create a max-heap by inserting the following integers into an initially empty
heap (one by one): 6, 2, 4, 1, 8, 5, 3, 7, 9 We will refer to the resulting heap as ”insertion heap”. void heapInsert(int newItem) { items[size] = newItem; int place = size; int parent = (place -1)/2; while ( (parent >= 0) && (items[place] > items[parent]) { swap(items[place], items[parent]) place = parent; parent = (place -1)/2; } size++; size: number of items in the array place: index of inserted item parent: index of the parent of the inserted item

2 Insert 6: Insert 2: Insert 4: Insert 1: Index 1 2 3 4 5 6 7 8 Key
1 2 3 4 5 6 7 8 Key Insert 2: Index 1 2 3 4 5 6 7 8 Key Insert 4: Index 1 2 3 4 5 6 7 8 Key Insert 1: Index 1 2 3 4 5 6 7 8 Key

3 6 2 4 1 8 6 2 4 1 8 5 8 6 4 1 2 Insert 8: Insert 5: Insert 3: Index 1
1 2 3 4 5 6 7 8 Key 6 2 4 1 8 6 2 4 1 8 Index 1 2 3 4 5 6 7 8 Key Insert 5: 5 8 6 4 1 2 Index 1 2 3 4 5 6 7 8 Key Insert 3:

4 Insert 7: Index 1 2 3 4 5 6 7 8 Key 1 7 8 6 5 2 4 3 6 1 7 8 5 2 4 3 Insert 9: Index 1 2 3 4 5 6 7 8 Key 9 8 7 5 6 2 4 3 1 9 8 7 5 6 2 4 3 1 9 8 7 5 6 2 4 3 1 9

5 “Insertion Heap” Index 1 2 3 4 5 6 7 8 Key 9 9 8 7 1 6 2 5 4 3

6 1b) Next, assume that the same set of integers above {6, 2, 4, 1, 8, 5, 3, 7, 9} are
already inside an array in that order, now call ”heapify” to transform this array into max-heap. We will refer to the resulting heap as ”heapify heap”. void heapify() { for (int i = size/2 -1 ; i >= 0; i--) //last internal node is at (size/2 – 1) heapRebuild(i); } void heapRebuild(int root){ int child = 2 * root + 1; // left child if (child < size) { // there is a left child int rightChild= child + 1; // right child if ( (rightChild< size) && (items[rightChild] > items[child]) ) //if right exists child = rightChild; // choose bigger child if ( items[root] < items[child] ) { // bubble down swap(items[root], items[child]; // swap heapRebuild(child); // recursive call

7 We build the heap from bottom up
We build the heap from bottom up. Since nodes from index 4 – 8 are all leaf nodes (which are considered heaps by on their own), we start from the internal nodes – from (size/2-1) to 0, and bubble down each node. Hence, heapify() starting with index 3. Index 1 2 3 4 5 6 7 8 Key 9 Rebuild(3): 1 9 6 2 4 8 5 3 7 Rebuild(2): Index 1 2 3 4 5 6 7 8 Key 9 1 9 6 2 8 3 7 4 5

8 Rebuild(1): Index 1 2 3 4 5 6 7 8 Key 9 9 6 2 5 8 4 3 7 1 9 6 2 5 8 4 3 7 1 Rebuild(0): Index 1 2 3 4 5 6 7 8 Key 9 6 9 5 7 8 4 3 2 1 6 9 5 7 8 4 3 2 1

9 “Heapify Heap” Index 1 2 3 4 5 6 7 8 Key 9 9 8 7 2 1 6 5 4 3

10 1(c). What is your conclusion regarding “insertion” versus “heapify” heaps?
Insertion – Less efficient. Processes N items, and bubble up to maintain heap for each item. Heapify – More efficient. Processes only N/2 – 1 items, and bubble down to maintain heap for each item. 1(d). Next, perform two deleteMax() operations from the “heapify heap”. Show the final results. Note that performing deleteMax() n times is simply the heap sort algorithm. Index 1 2 3 4 5 6 7 8 Key 9 deleteMax() – first time: swap first index 0 with last index 8. 1 9 8 5 7 6 4 3 2 Call heapRebuild() “deleted” by decreasing heapsize by 1 8 7 5 2 6 4 3 1

11 deleteMax() – second time: swap first index 0 with last index 7
deleteMax() – second time: swap first index 0 with last index 7. size--; Index 1 2 3 4 5 6 7 8 Key 9 1 9 7 5 2 6 4 3 8 Call heapRebuild() “deleted” 7 6 5 2 1 4 3 7 6 2 1 5 4 3


Download ppt "1a) Create a max-heap by inserting the following integers into an initially empty heap (one by one): 6, 2, 4, 1, 8, 5, 3, 7, 9 We will refer to the resulting."

Similar presentations


Ads by Google