Assignment 5 Solution

Assignment 5 Solution

1.   (100 pts) Write a program to construct a binary search tree and print the tree on its side. Read the input from the user as a sequence of integers and output the tree indented based on depth and with one value on each line.

 

Consider the following input from user

 

Enter the numbers

 

10 6 14 4 8 12 16

 

The binary search tree using above numbers is given below



 

10

 

 

6                            14

 

 

4
 
8
 
12
 
16
 
 
 
 
 
 
 
 

Figure 1: Binary Search Tree

 

Note that the order of the numbers entered changes the tree. First number is always the root of the tree and last number is a leaf of the tree.

 

The output for the above set of numbers is the tree printed on its side as shown below.

 

16

 

14

 

12

 

10

 

8

 

6

 

4

 

The text for a node should be indented 4 times the depth of the node. Root (depth 0) should not be indented and a node at depth 2 should be indented 8 spaces.

 

 
Powered by