Fall 2002 CSCI 333 Homework 3 Solution

Problem 1

Show how to construct the following two lists using a class IntList that implements List<int>, as described in the textbook.

Problem 2

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 is still wrong. The node at the top should be contain 25, not 13.

Problem 2 graph

Problem 3

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.

Problem 3 graph

Heap answers

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.

Problem 4

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.

13142011997733
13142011997733
13112014997733
13112014997733
13112014997733
11132014997733
11132014997733

Problem 5

Remove the two smallest elements from the heap created in Problem 3. Be sure to restore the heap to the proper format.

11132014997733
331320149977
331320149977
133320149977
133320149977
131420339977
7714203399
7714203399
1477203399
1477203399
1433207099

Problem 6

Add the elements 88 and 12 to the heap created in Problem 4. Again, be sure to restore the heap to the proper format.

1113201499773388
1113201499773388
111320149977338812
111320149977338812
111320129977338814
111320129977338814
111220139977338814
111220139977338814