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 left, right;

public BSTNode(T data, BSTNode l, BSTNode r)

{

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 root;

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

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 Databases Questions!