Question: please do the code for the following 6 functions and add the code where it says implement me . class Node { int value; Node

please do the code for the following 6 functions and add the code where it says implement me. class Node{
int value;
Node left, right;
public Node(int value){
this.value = value;
left = null;
right = null;
}
}
class BinarySearchTree{
Node root;
/*
recursive insert method
*/
/*
inserts a node into the tree
*/
public void insert(int value){
//tree is empty
if(root == null){
root = new Node(value);
return;
}else{
Node current = root;
Node parent = null;
while(true){
parent = current;
if(value < current.value){
current = current.left;
if(current == null){
parent.left = new Node(value);
return;
}
}else{
current = current.right;
if(current == null){
parent.right = new Node(value);
return;
}
}
}//closing while
}//closing main if-else
}
/*
pre-order traversal
Prints the value of every node preorder
*/
public void preOrderTraversal(Node root){
//implement in here
}
/*
in-order traversal
*/
public void inOrderTraversal(Node root){
//implement in here
}
/*
post-order traversal
*/
public void postOrderTraversal(Node root){
//implement in here
}
/*
a method to find the node in the tree
with a specific value
*/
public boolean find(Node root, int key){
//implement in here
}
/*
a method to find the node in the tree
with a smallest key
*/
public int getMin(Node root){
//implement in here
}
/*
a method to find the node in the tree
with a largest key
*/
public int getMax(Node root){
//implement in here
}
/*
this method will not compile until getMax
is implemented
*/
public Node delete(Node root, int key){
if(root == null){
return root;
}else if(key < root.value){
root.left = delete(root.left, key);
}else if(key > root.value){
root.right = delete(root.right, key);
}else{
//node has been found
if(root.left==null && root.right==null){
//case #1: leaf node
root = null;
}else if(root.right == null){
//case #2 : only left child
root = root.left;
}else if(root.left == null){
//case #2 : only right child
root = root.right;
}else{
//case #3 : 2 children
root.value = getMax(root.left);
root.left = delete(root.left, root.value);
}
}
return root;
}
}
public class TreeDemo{
public static void main(String[] args){
BinarySearchTree t1= new BinarySearchTree();
t1.insert(24);
t1.insert(80);
t1.insert(18);
t1.insert(9);
t1.insert(90);
t1.insert(22);
System.out.print("in-order : ");
t1.inOrderTraversal(t1.root);
System.out.println();
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!