You are on page 1of 3

U NIVERSITY OF THE W ITWATERSRAND , J OHANNESBURG

School of Computer Science

COMS1017: Intro. to Data Structures & Algorithms


Lab Test 2

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

2.2 Min/Max 15 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

1
35
61
24
58
62
32
-1

Sample Output:

24
62

2.3 Contains 10 Marks


This program should build upon your previous submissions. The input is in the same format and you must
construct a binary search tree. Once the first set of numbers has been read and the tree has been built,
your program should enter a loop where you read a number from stdin and print out true or false
depending on whether the number is in the tree. The loop should exit when it reads a -1.

Sample Input: Sample Output:


50 false
30 true
35 false
61 false
24 false
58 false
62 true
32
-1
-5
50
64
59
66
99
24
-1

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

You might also like