You are on page 1of 6

U NIVERSITY OF THE W ITWATERSRAND , J OHANNESBURG

School of Computer Science and Applied Mathematics

COMS1017: Intro. to Data Structures & Algorithms


Lab Test 2

Richard Klein
3 hours

October 15, 2015

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.

1 Binary Search Trees [65 Marks]


Implement the relevant functions from bst.cpp. Main functions that correctly read in the relevant data
and construct the tree have been provided.

1.1 Traversals 15 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

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

1.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
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

Figure 1: Complete Binary Tree with Array Indices

2.1 idxLeft/idxRight/idxParent 10 Marks


Write the three relevant functions, such that each one takes the index of a node in the array and returns the
index to the Left child, Right child, and Parent respectively. Your program must read an index from stdin
and output the parent index, left child index, and right child index respectively.

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

2.4 isBST 10 Marks


Write a function that returns true or false depending on whether the a given tree is a Binary Search Tree or
not.
Hint: Think about the InOrderTraversal.

SampleIn1: SampleOut1:
15 false
12
83
54
68
19
20
18
98
65
-1
SampleIn2: SampleOut2:
42 true
20
60
10
30
-1

You might also like