In this project, our objective is to understand how to build the binary search tree.
Create a binary search tree that stores persons based on their ages.
- Persons list:
- "Ahmed" age: 53
- "Mohammed" age: 23
- "Abeer" age: 28
- "Rashed" age: 34
- "Fahad" age: 30
- "Maha" age: 21
- "Hanouf" age: 41
- "Fatima" age: 32
- "Ali" age: 29
We will build the below binary search tree:
We will use three classes:
PersonclassNodeclassBinarySearhTreeclass
Using Java programming language:
- The
Personclass is already implemented and added to the BinarySearchTree java file as the below:
class Person {
String name;
int age;
public Person(String name, int age){
this.name = name;
this.age = age;
}
}- The
Nodeclass is already implemented and added to the BinarySearchTree java file as below:
class Node {
Person person;
Node right;
Node left;
public Node(Person person){
this.person = person;
this.right = null;
this.left = null;
}
}- The
BinarySearchTreeclass is already implemented and added to the BinarySearchTree java file as below:
import java.util.Stack;
class BinarySearchTree {
private Node root;
public BinarySearchTree() {
this.root = null;
}
public void print() {
if (this.root == null) {
System.out.println("Tree is empty");
return;
}
Stack<Node> stack = new Stack<>();
stack.push(this.root);
while (!stack.isEmpty()) {
Node currentNode = stack.pop();
System.out.print(" " + currentNode.person.age);
if (currentNode.right != null) {
stack.push(currentNode.right);
}
if (currentNode.left != null) {
stack.push(currentNode.left);
}
}
System.out.println();
return;
}
public void insert(Person newPerson) {
Node newNode = new Node(newPerson);
if (root == null) {
root = newNode;
return;
}
Stack<Node> stack = new Stack<>();
Node curr = root;
while (true) {
stack.push(curr);
if (newNode.person.age == curr.person.age) {
return;
} else if (newNode.person.age < curr.person.age) {
if (curr.left == null) {
curr.left = newNode;
return;
}
curr = curr.left;
} else {
if (curr.right == null) {
curr.right = newNode;
return;
}
curr = curr.right;
}
}
}
public boolean delete(int deleteAge) {
if (this.root == null) {
return false;
}
Stack<Node> stack = new Stack<>();
Node curr = this.root;
Node prev = null;
boolean isLeftChild = false;
// Search for the taget (curr) and it's parent (prev)
while (curr != null) {
stack.push(curr);
if (deleteAge == curr.person.age) {
break;
}
prev = curr;
if ( deleteAge < curr.person.age) {
curr = curr.left;
isLeftChild = true;
} else {
curr = curr.right;
isLeftChild = false;
}
}
// Case: the target was not found
if (curr == null) {
System.out.println("The target was not found");
return false;
}
// Case: the target has no children (leaf node)
if (curr.left == null && curr.right == null) {
if (curr == root) {
root = null;
} else if (isLeftChild) {
prev.left = null;
} else {
prev.right = null;
}
// Case: the target is not a leaf node
} else {
System.out.println("The target { " + deleteAge + " } is not a leaf node");
return false;
}
return true;
}
public boolean search(int age) {
if (this.root == null) {
return false;
}
Stack<Node> stack = new Stack<>();
stack.push(this.root);
while (!stack.isEmpty()) {
Node current = stack.pop();
if (current.person.age == age) {
return true;
}
if (age < current.person.age && current.left != null) {
stack.push(current.left);
} else if (age > current.person.age && current.right != null) {
stack.push(current.right);
}
}
return false;
}
}
In the main method perform the following actions:
- Create a BinarySearchTree object.
- Call the insert function to build/add the tree in Figure 1.
- Call the print function to print the tree values.
- Output should be the same as below:
print tree values:
53 23 21 28 34 30 29 32 41
public static void main(String[] args) {
/* Add your code here */
// 1 - Create a BinarySearchTree object.
// 2 - Call the insert function to build/add the tree in figure 1.
// 3 - Call the print function to print the tree values.
}
