hw05solutions

# hw05solutions - CSE 331 Spring 2008, Section 1 Homework 5...

This preview shows pages 1–8. Sign up to view the full content.

CSE 331 Spring 2008, Section 1 Homework 5 Solutions 1. (a) 10 10 12 1 12 10 1 12 10 14 1 6 10 14 12 1 6 5 14 12 10 1 6 5 14 12 10 8 1 6 5 14 12 10 8 15 1 3 5 6 12 10 8 15 14

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
1 3 5 6 9 10 8 15 14 12 1 3 5 6 7 10 8 15 14 12 9 1 3 4 6 7 5 8 15 14 12 9 10 1 3 4 6 7 5 8 15 14 12 9 10 11
(b) We start by throwing all of the elements into the heap structure sequentially: Now we percolate each of the internal nodes down to the correct place in the heap ordering, starting with the 8 on the far right of the second-to-last level. This gradually restores the entire heap into the correct order. In the diagrams below, the node that was percolated down is italicized at each stage. In addition, all of the nodes affected by the percolation are in boldface. 1 3 4 6 7 5 8 15 14 12 9 10 11 13 1 3 2 6 7 5 4 15 14 12 9 10 11 13 8 10 12 1 14 6 5 8 15 3 9 7 4 11 13 2 15 10 12 1 14 6 5 2 3 9 7 4 11 13 8

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
15 10 12 1 14 6 4 2 3 9 7 5 11 13 8 15 10 12 1 14 6 4 2 3 9 7 5 11 13 8 15 10 12 1 3 6 4 2 14 9 7 5 11 13 8 15 10 12 1 3 6 4 2 14 9 7 5 11 13 8
We now have a properly-ordered binary heap. 2. After each deletion, the nodes affected by the deletion are shown in boldface. The node that formerly occupied the last place in the heap is italicized. Working with the heap from #1 (a): 15 10 3 1 12 6 4 2 14 9 7 5 11 13 8 15 1 3 2 12 6 4 8 14 9 7 5 11 13 10 2 3 4 6 7 5 8 15 14 12 9 10 11 13 3 6 4 13 7 5 8 15 14 12 9 10 11

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Working with the heap from #1 (b): 3. (a) In a min-heap, an internal node must have a lower value than any of its descendants. Say that we had a min-heap where the maximum value was not at one of the leaves. It would then be an internal node. Since it is the maximum value in the list, it would have a greater value than its children. This violates the ordering property of a min-heap. From this, we can see that the 4 6 5 13 7 10 8 15 14 12 9 11 15 2 3 4 12 6 5 8 14 9 7 10 11 13 15 3 6 4 12 7 5 8 14 9 13 10 11 15 4 6 5 12 7 10 8 14 9 13 11
maximum value in a min-heap must be at the leaves. (b) A heap is always structured as a full binary tree. If the heap is of height h, new nodes will always be created as leaves at depth h. When depth h is completely full, the next new node will be added at depth (h + 1), increasing the height of the tree to (h + 1). Consider a heap where there are j leaves in the bottom level. Some of the nodes at depth (h – 1) will be internal nodes, used to hold the leaves at depth h in place, while others will still be leaf nodes. The bottom level in a heap fills from left to right, so as few of the nodes at depth (h – 1) will be internal nodes as possible. Since a node can have two children in a binary tree, the

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## hw05solutions - CSE 331 Spring 2008, Section 1 Homework 5...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online