At this lab section, we will learn
implementation of Binary Search Trees
and make search, insertion, deletion
operations on them.
Binary Search
Trees
Asst. Prof. Dr. Feriştah DALKILIÇ
Res. Asst. Fatih DİCLE
PART 1 – Binary Search Tree
A Binary Search Tree (BST) is a binary tree whose nodes contain Comparable objects and are
organized as follows:
For each node in a binary search tree,
The node’s data is greater than all the data in the node’s left subtree,
The node’s data is less than all the data in the node’s right subtree.
Exercise - 1
At this section, we will do some operations on a binary search tree according to BST rules.
Step – 1
Insert items to the tree and show the inserted version of the BST.
Items = [11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 30]
11
6 19
4 8 17 43
5 10 30 49
Step – 2
Search some items in the BST, write the visited node sequence below.
Search 5 -> 11-6-4-5
Search 30 -> 11-19-17-43-30
Step – 3
Show the final state of the BST after deleting 11, 4 and 49.
10
6 19
5 8 17 43
30
Exercise - 2
At this section, we will experiment insertion, deletion, and search operations on Binary Search Tree
in Java.
Step – 1
You are given some ready to use classes in src folder. The class hierarchy is given below:
TreeInterface TreeIteratorInterface
extends extends
BinaryTreeInterface BinaryTree
implements
extends extends
SearchTreeInterface BinarySearchTree
implements
Create a new Java project and add the given classes into your project.
Step – 2
Fill in the missing parts in findEntry, addEntry, and removeEntry methods of the BinarySearchTree.java.
Step – 3
Add a new class with the name Test.java. Create a BinarySearchTree instance. Read the given input
file StudentNumbers.txt and insert all the items into BST.
Step – 4
Test whether the BST is empty or not.
Step – 5
Print the data in the root node.
Step – 6
Print the height of the BST.
Step – 7
Print the number of nodes in the BST.
Step – 8
Get the entry equals to your student number.
Step – 9
Test whether the BST contains your student number.
Step – 10
Remove your student number from the BTS.
Step – 11
Iterate and print the BTS by using in-order traversal technique.
Step – 12
Paste your final content of Test.java and your final output.
Your Test.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
BinarySearchTree<Integer> bst = new BinarySearchTree<>();
//Step-3
String fileName = "C:\\Users\\hp\\OneDrive\\Masaüstü\\Yeni klasör
(2)\\Java\\DS_W10\\src\\StudentNumbers.txt";
try (BufferedReader br = new BufferedReader(new
FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
try {
int studentNumber = Integer.parseInt(line.trim());
bst.add(studentNumber);
} catch (NumberFormatException e) {
System.out.println("Invalid number format in file: "
+ line);
}
}
} catch (IOException e) {
System.out.println("An error occurred while reading the file:
" + e.getMessage());
}
//Step-4
System.out.println("Is it Empty? " + bst.isEmpty());
//Step-5
System.out.println("Root Data: " + bst.getRootNode().getData());
//Step-6
System.out.println("Height of BST: " +
bst.getRootNode().getHeight());
//Step-7
System.out.println("Number of Nodes: " +
bst.getRootNode().getNumberOfNodes());
//Step-8
int studentNumber = 2022510142;
Integer entry = bst.getEntry(studentNumber);
if (entry != null)
System.out.println("Entry: " + entry);
else
System.out.println("Student number wasn't found in BST.");
//Step-9
System.out.println("Does the BST contain student number? " +
bst.contains(studentNumber));
//Step-10
if (bst.contains(studentNumber))
bst.remove(studentNumber);
//Step-11
System.out.println("Inorder Traversal of the BST:");
inorderTraversal(bst.getRootNode());
}
public static void inorderTraversal(BinaryNode<Integer> node) {
if (node != null) {
inorderTraversal(node.getLeftChild());
System.out.println(node.getData());
inorderTraversal(node.getRightChild());
}
}
}
Your Output
Is it Empty? false
Root Data: 2021510001
Height of BST: 12
Number of Nodes: 137
Student number wasn't found in BST.
Does the BST contain student number? false
Inorder Traversal of the BST:
2009510112
2011510115
2012510128
2016510038
2016510077
2017510015
2017510022
2018510038
2018510040
2018510089
2018510155
2019510004
2019510013
2019510024
2019510091
2019510092
2019510113
2020510001
2020510011
2020510013
2020510015
2020510032
2020510037
2020510042
2020510055
2020510057
2020510059
2020510063
2020510069
2020510073
2020510077
2020510080
2020510093
2020510094
2020510158
2020510163
2020510169
2020510171
2020510177
2020510199
2020510201
2021510001
2021510002
2021510003
2021510004
2021510005
2021510006
2021510010
2021510011
2021510016
2021510017
2021510018
2021510019
2021510023
2021510024
2021510025
2021510026
2021510028
2021510030
2021510033
2021510034
2021510035
2021510037
2021510038
2021510041
2021510043
2021510044
2021510047
2021510051
2021510053
2021510054
2021510058
2021510060
2021510061
2021510062
2021510063
2021510066
2021510070
2021510071
2021510072
2021510073
2021510077
2021510080
2021510082
2021510083
2021510086
2021510088
2021510089
2021510090
2021510093
2021510094
2021510095
2021510096
2021510102
2021510108
2021510109
2021510110
2021510118
2021510119
2021510122
2021510133
2021510136
2022510008
2022510013
2022510022
2022510029
2022510032
2022510046
2022510070
2022510071
2022510084
2022510085
2022510098
2022510104
2022510105
2022510106
2022510111
2022510112
2022510113
2022510115
2022510125
2022510127
2022510129
2022510133
2022510140
2022510143
2022510144
2022510145
2022510149
2022510150
2022510151
2022510157
2022510158
2022510159
2022510166
2022510167
2022510183