Build a binary search tree by starting with an empty tree and then adding, in order, the eight values 25, 14, 20, 11, 99, 77, 33, and 50.
The following diagram still wrong. The node at the top should be contain 25, not 13. When I find the Illustrator source file for this thing, I'll change it!
Now show the binary search tree that results from removing, in order, the values 77 and 25 from the tree you constructed in Problem 2.
In the tabular display of the running of the heap algorithms, elements that are being compared are shown with a light red background while elements that are being moved are shown with a light green background.
Start with an array containing the six values 13, 14, 20, 11, 99, 77, and 33 stored sequentially in this order. Now "run" the efficient heap building procedure described in Section 5.5 and illustrated in Figure 5.21(b) on this array to build a minheap stored within an array.
13 | 14 | 20 | 11 | 99 | 77 | 33 |
13 | 14 | 20 | 11 | 99 | 77 | 33 |
13 | 11 | 20 | 14 | 99 | 77 | 33 |
13 | 11 | 20 | 14 | 99 | 77 | 33 |
11 | 13 | 20 | 14 | 99 | 77 | 33 |
11 | 13 | 20 | 14 | 99 | 77 | 33 |
Remove the two smallest elements from the heap created in Problem 3. Be sure to restore the heap to the proper format.
11 | 13 | 20 | 14 | 99 | 77 | 33 |
33 | 13 | 20 | 14 | 99 | 77 | |
33 | 13 | 20 | 14 | 99 | 77 | |
13 | 33 | 20 | 14 | 99 | 77 | |
13 | 33 | 20 | 14 | 99 | 77 | |
13 | 14 | 20 | 33 | 99 | 77 | |
77 | 14 | 20 | 33 | 99 | ||
77 | 14 | 20 | 33 | 99 | ||
14 | 77 | 20 | 33 | 99 | ||
14 | 77 | 20 | 33 | 99 | ||
14 | 33 | 20 | 77 | 99 |
Add the elements 88 and 12 to the heap created in Problem 3. Again, be sure to restore the heap to the proper format.
11 | 13 | 20 | 14 | 99 | 77 | 33 | 88 | |
11 | 13 | 20 | 14 | 99 | 77 | 33 | 88 | |
11 | 13 | 20 | 14 | 99 | 77 | 33 | 88 | 12 |
11 | 13 | 20 | 14 | 99 | 77 | 33 | 88 | 12 |
11 | 13 | 20 | 12 | 99 | 77 | 33 | 88 | 14 |
11 | 13 | 20 | 12 | 99 | 77 | 33 | 88 | 14 |
11 | 12 | 20 | 13 | 99 | 77 | 33 | 88 | 14 |
11 | 12 | 20 | 13 | 99 | 77 | 33 | 88 | 14 |
Using the general tree node class of Chapter 6,
write a small section of code that sets GTNode MC
to the middle child of GTNode PA
.
This is the straightforward solution. Count the number of children and then move through half of all those kids.
int numchildren = 0 ;
GTNode<T> *TC = PA->leftmost_child() ;
while (TC != (GTNode<T> *)NULL) {
++numchildren ;
T = T->right_sibling() ;
}
GTNode<T> *MC = PA->leftmost_child() ;
for (int i = 0 ; i < (numchildren+1)/2 ; ++i)
MC = MC->right_sibling() ;
This is the one loop solution in which one pointer moves at twice the speed of another.
GTNode<T> *MC = PA->leftmost_child() ;
GTNode<T> *TC = MC ;
while (TC != (GTNode<T> *)NULL) {
TC = TC->right_sibling() ;
if (TC != (GTNode<T> *)NULL) {
TC = TC->right_sibling() ;
MC = MC->right_sibling() ;
}
}