You are on page 1of 4

DSA - Assignment 2

Binary Search Tree


I. Problems

For each of the following key sequences create the binary search tree obtained when the keys are
inserted one-by-one in the order given into an initially empty tree:
+12, 7, 1, 3, 2, 5, 10, 8, 6, 9.
Give the preorder, inorder, postorder, and level-order traversals of the created binary trees.
Delete keys 5, 6 and 7. After each time of deleting, give the above traversals or draw tree.

II. Practical exercises

Write a Java program to implement a binary search tree of integer values with the following operations:
1. boolean isEmpty() - return true if a tree is empty, return false otherwise.
2. void clear() - clear a tree.
3. Node search(int x) - Search a node having value x. Return a reference to that node if found, return
null otherwise.
4. void insert(int x) - check if the key x does not exists in a tree then insert new node with value x into
the tree.
5. void breadth() - traverse a tree.
6. void preorder(Node p) - recursive preorder traverse of a tree.
7. void inorder(Node p) - recursive inorder traverse of a tree.
8. void postorder(Node p) - recursive postorder traverse of a tree.
9. int count() - count and return number of nodes in the tree.
10. void dele(int x) - delete a node having value x.
11. Node min() - find and return the node with minimum value in the tree.
12. Node max() - find and return the node with maximum value in the tree.
13. int sum() - return the sum of all values in the tree.
14. int avg() - return the average of all values in the tree.
15. The height of a tree is the maximum number of nodes on a path from the root to a leaf node. Write
a function that returns the height of a binary tree.

Submission Requirements
Create the directory with a name like <class>-<name>-<roll number>-ASS1, e.g.
SE1711-QuangTV-HE170045-AS1 (1)
The (1) directory contains your JAVA project

Compress the folder (1) to .zip (or .rar) file (with the same name) and upload to cms.
Assignment assessment
You will be asked to modify immediately and to explain your assignment in lab room to be sure
that you are really the author of the assignment you submitted.

II. Test cases

Main() function should be as bellow.

public static void main(String[] args) {


BSTree t = new BSTree();
int a[] = {4, 3, 1, 11, 5, 9, 2, 6, 15, 12};
for(int x : a)
t.insert(x);

t.inorder();
System.out.println("\n");

t.preorder();
System.out.println("\n");

t.posorder();
System.out.println("\n");

System.out.println("Search 20 result: " + (t.search(20)==null?"NOT Found":"FOUND") );


System.out.println("Search 15 result: " + (t.search(15)==null?"NOT Found":"FOUND") );

System.out.println("");
t.breadth();

System.out.println("\nCount = "+t.count());
System.out.println("SUM = "+t.sum());
System.out.println("MAX = "+t.max());
System.out.println("MIN = "+t.min());
System.out.println("AVG = "+t.avg());
System.out.println("HEIGHT = "+t.height() +"\n");

t.dele(2);
t.breadth();
t.inorder();
System.out.println("\n");
t.dele(3);
t.breadth();
t.inorder();
System.out.println("\n");

t.dele(11);
t.breadth();
t.inorder();
System.out.println("\n");
}

The Output is something as follows.

In-Order traverse:
1, 2, 3, 4, 5, 6, 9, 11, 12, 15,

Pre-Order traverse:
4, 3, 1, 2, 11, 5, 9, 6, 15, 12,

Pos-Order traverse:
2, 1, 3, 6, 9, 5, 12, 15, 11, 4,

Search 20 result: NOT Found


Search 15 result: FOUND

Breadth - traverse:
4, 3, 11, 1, 5, 15, 2, 9, 12, 6,

Count = 10
SUM = 68
MAX = 15
MIN = 1
AVG = 6.8
HEIGHT = 5
DELETED key: 2
Breadth - traverse:
4, 3, 11, 1, 5, 15, 9, 12, 6,
In-Order traverse:
1, 3, 4, 5, 6, 9, 11, 12, 15,

DELETED key: 3
Breadth - traverse:
4, 1, 11, 5, 15, 9, 12, 6,
In-Order traverse:
1, 4, 5, 6, 9, 11, 12, 15,

DELETED key: 11
Breadth - traverse:
4, 1, 9, 5, 15, 6, 12,
In-Order traverse:
1, 4, 5, 6, 9, 12, 15,

You might also like