Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions

Question

1. Who will you assemble on the team?

Answered: 1 week ago

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago