Question: / / Implement a BinarySearchTree class using the linked structure. / / Hint: The implementation of binary tree with linked structure are provided ( LinkedBinaryTree
Implement a BinarySearchTree class using the linked structure.
Hint: The implementation of binary tree with linked structure are provided LinkedBinaryTreejava and BTNode.java
The output should be:
Test BinarySearchTree
Size of the tree:
Height of the tree:
inOrder:
Test search
with parent and leftChild: null and rightChild: null
Test insert
Test delete
before delete
Test delete
before delete
after delete
with parent and leftChild: and rightChild:
with parent and leftChild: null and rightChild: null
with parent null and leftChild: null and rightChild:
Note
DO NOT DELETE ANY COMMENT!!!
Modify the file name to "BinarySearchTree.java" before compiling it
public class BinarySearchTree extends LinkedBinaryTree
public BinarySearchTreeBTNode root
superroot;
It is the search method.
public BTNode searchE key
BTNode current this.getRoot;
while current null
ifkeycompareTocurrentgetElement
return current;
else ifkeycompareTocurrentgetElement
current current.getLeftChild;
else
current current.getRightChild;
System.out.printlnDid not find the key!";
return null;
It is the insert method.
public void insertBTNode node
ifgetRoot null
setRootnode;
return;
BTNode current getRoot;
BTNode parent;
whiletrue
parent current;
if nodegetElementcompareTocurrentgetElement
do nothing
System.out.printlnNode node.getElementtoString "exists!";
break;
else if nodegetElementcompareTocurrentgetElement
current current.getLeftChild;
ifcurrent null
parent.setLeftChildnode;
break;
else
current current.getRightChild;
ifcurrent null
parent.setRightChildnode;
break;
It is the delete method.
public void deleteBTNode node
BTNode target this.searchnodegetElement;
if target null
System.out.printlnDid not find the target!";
return;
realDeletedNode is the node we actually delete.
BTNode realDeletedNode this.getRealDeletedNodetarget;
System.out.printlntargettoString;
System.out.printlnrealDeletedNodetoString;
if realDeletedNode getRoot
setRootnull;
return;
delete the realDeletedNode
COMPLETE THIS BLOCK
replace the target with the realDeletedNode
COMPLETE THIS BLOCK
public BTNode getRealDeletedNodeBTNode deleleNode
BTNode realDeletedNode deleleNode;
find the smallest key in the right subtree
if deleleNodegetRightChild null
COMPLETE THIS BLOCK
else if deleleNodegetLeftChild null
find the largest key in the left subtree
COMPLETE THIS BLOCK
else
do nothing
return realDeletedNode;
public static void mainString args
test BinarySearchTree
System.out.println Test BinarySearchTree";
null
BTNode node new BTNode null, null;
BTNode node new BTNode null, null;
BTNode node new BTNode null, null;
BTNode node new BTNode node node;
BTNode node new BTNode node null;
BTNode node new BTNode node node;
nodesetParentnode;
nodesetParentnode;
nodesetParentnode;
nodesetParentnode;
nodesetParentnode;
BinarySearchTree tree new BinarySearchTreenode;
System.out.printlnSize of the tree: tree.sizetreegetRoot;
System.out.printlnHeight of the tree: tree.heighttreegetRoot;
System.out.print
inOrder: ;
tree.inOrdertreegetRoot;
System.out.println
;
test search
System.out.println Test search";
System.out.printlntreesearchtoString;
System.out.println
;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
