Professional Documents
Culture Documents
Richard Klein
3 hours
Introduction
This test requires that you write a program to perform various options on a binary search tree. You may not
print out anything other that what is explicitly required. You MUST use the tree algorithms covered in class.
50
30
35
61
24
58
62
32
-1
Sample Output:
50 30 24 35 32 61 58 62
24 30 32 35 50 58 61 62
24 32 35 30 58 62 61 50
1
1.2 Min/Max 10 Marks
This program should build upon your previous submission. The input is in the same format and you must
construct a binary search tree. Your program should output the minimum and maximum values in the tree.
Sample Input:
50
30
35
61
24
58
62
32
-1
Sample Output:
24
62
2
1.4 Deletion 30 Marks
This program should build upon your previous submissions. Your program should read in and construct
the tree. Now enter another loop. Read a number from stdin. Find the number in the tree and delete
it. It should then perform a pre-order traversal and print it in the same manner as above. This loop should
continue until a -1 is read.
You must use the algorithm covered in class. The 3 cases are marked separately.
SampleIn1:
50
30
35
61
24
58
62
32
59
-1
24
35
50
-1
SampleOut1:
50 30 35 32 61 58 59 62
50 30 32 61 58 59 62
58 30 32 61 59 62
In the example above, we first delete a leaf node, then we delete a node with only one child, then one
with 2 children. These three cases are tested separately on Moodle. When deleting from a node with 2
children, you are required to work from the right.
3
2 Complete Binary Trees [35 Marks]
A complete binary tree can be elegantly represented as an array, such that each node in the tree maps to a
specific index in the array as shown in the figure below.
1 2
3 4 5 6
7 8 9 10
SampleIn1: SampleOut1:
0 Parent of 0: -1
1 Left of 0: 1
2 Right of 0: 2
3 Parent of 1: 0
4 Left of 1: 3
5 Right of 1: 4
-1 Parent of 2: 0
Left of 2: 5
Right of 2: 6
Parent of 3: 1
Left of 3: 7
Right of 3: 8
Parent of 5: 2
Left of 5: 11
Right of 5: 12
4
2.2 Exists 5 Marks
Write a function that returns true/false depending on whether the given index exists in the tree. The program
should read in values to construct a vector for the underlying tree. After reading a -1, it should then start
returning true/false depending on whether there is an item in the tree at that index.
SampleIn1: SampleOut1:
15 true
12 true
83 true
54 true
68 true
19 true
20 true
18 true
98 true
65 true
-1 false
0 false
1 false
2 false
3
4
5
6
7
8
9
10
11
12
13
-1
5
2.3 Traversals 10 Marks
Write the 3 binary tree traversal functions. These functions should fill up a vector<int> so that the
final vector has the items in the order that they’d be printed out. Do this by passing a vector by reference to
each of the functions as illustrated in complete.cpp. The provided main code will print out the traversals.
SampleIn1: SampleOut1:
15 15 12 54 18 98 68 65 83 19 20
12 18 54 98 12 65 68 15 19 83 20
83 18 98 54 65 68 12 19 20 83 15
54
68
19
20
18
98
65
-1
SampleIn1: SampleOut1:
15 false
12
83
54
68
19
20
18
98
65
-1
SampleIn2: SampleOut2:
42 true
20
60
10
30
-1