Professional Documents
Culture Documents
Labtest2 2014
Labtest2 2014
Richard Klein
October 2014
2 hours
1 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.
2 Submissions
2.1 Traversals 30 Marks
Write a program in C++ that reads a list of numbers from stdin until it reads a -1. Your program must
construct a binary search tree as each number is read. Once constructed, your program must complete a
pre-order traversal, an in-order traversal and finally a post-order traversal
of the tree. Each traversal should be printed out on consecutive lines.
Sample Input:
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
50
30
1
35
61
24
58
62
32
-1
Sample Output:
24
62
2
2.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
-1
30
-1
SampleOut1:
50 32 24 35 61 58 62