Question
Implement the following methods in class BinarySearchTree. For all these methods you just need to implement the body of the methods already present in the
Implement the following methods in class BinarySearchTree. For all these methods you just need to implement the body of the methods already present in the class,
a. Insert: This method inserts a node in the binary search tree. This method must increment size variable after successful insertion.
b. Delete: this method deletes a node in the binary search tree. This method must decrement size variable after successful deletion.
c. Height: this method returns the height of the binary search tree
d. Traversal methods: should print the values of the node traversed
i. Inorder: for in-order traversal
ii. Preorder: for pre-order traversal
iii. Postorder: for post-order traversal
/** class BSTNode.java **/
package SearchTrees;
public class BSTNode
{
public T data;
public BSTNode
public BSTNode(T data, BSTNode
{
left = l; right = r;
this.data = data;
}
public BSTNode(T data)
{
this(data, null, null);
}
@Override
public String toString()
{
return data.toString();
}
}
/** class BinarySearchTree.java **/
package SearchTrees;
import java.util.*;
public class BinarySearchTree
{
private BSTNode
private int size;
public BinarySearchTree()
{
root = null;
size = 0;
}
public int Size()
{
return size;
}
/* ***************************************************************
* Insert a new node
* Returns true on successful insert otherwise false (when already present)
*****************************************************************/
public boolean insert(T data)
{
//TODO -> implement here
return false;
}
/*****************************************************************
* Delete a node
* Returns true on successful deletion and false otherwise
*****************************************************************/
public boolean delete(T data)
{
boolean found = false;
//TODO -> Implement here
return found;
}
/****************************************************************
*
* Reset Tree
*
***************************************************************/
public void reset()
{
root = null;
size=0;
}
/*****************************************************************
*
* Height of tree
*
****************************************************************/
public int height()
{
//TODO -> Implement here
return 0;
}
/*****************************************************************
*
* Traversal
*
******************************************************************/
public void inorder()
{
System.out.print("inorder BST: ");
//TODO -> Implement here (print all nodes)
System.out.println();
}
public void preorder()
{
System.out.print("preorder BST: ");
//TODO -> Implement here
System.out.println();
}
public void postorder()
{
System.out.print("postorder BST: ");
//TODO -> Implement here
System.out.println();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started