Question: public class BST > implements BSTInterface { protected BSTNode root; / / reference to the root of this BST boolean found; / / used by
public class BST implements BSTInterface
protected BSTNode root; reference to the root of this BST
boolean found; used by remove
public BST
Creates an empty BST object.
root null;
public boolean isEmpty
Returns true if this BST is empty; otherwise, returns false.
return root null;
private int recSizeBSTNode node
Returns the number of elements in tree.
if node null
return ;
else
return recSizenodegetLeft recSizenodegetRight;
public int size
Returns the number of elements in this BST
return recSizeroot;
private boolean recContainsT element, BSTNode node
Returns true if tree contains an element e such that
ecompareToelement; otherwise, returns false.
if node null
return false; element is not found
else if elementcompareTonodegetInfo
return recContainselement node.getLeft; Search left subtree
else if elementcompareTonodegetInfo
return recContainselement node.getRight; Search right subtree
else
return true; element is found
public boolean contains T element
Returns true if this BST contains an element e such that
ecompareToelement; otherwise, returns false.
return recContainselement root;
private T recGetT element, BSTNode node
Returns an element e from tree such that ecompareToelement;
if no such element exists, returns null.
if node null
return null; element is not found
else if elementcompareTonodegetInfo
return recGetelement node.getLeft; get from left subtree
else
if elementcompareTonodegetInfo
return recGetelement node.getRight; get from right subtree
else
return node.getInfo; element is found
public T getT element
Returns an element e from this BST such that ecompareToelement;
if no such element exists, returns null.
return recGetelement root;
private BSTNode recAddT element, BSTNode node
Adds element to tree; tree retains its BST property.
if node null
Addition place found
node new BSTNodeelement;
else if elementcompareTonodegetInfo
node.setLeftrecAddelement node.getLeft; Add in left subtree
else
node.setRightrecAddelement node.getRight; Add in right subtree
return node;
public void add T element
Adds element to this BST The tree retains its BST property.
root recAddelement root;
private T getPredecessorBSTNode node
Returns the information held in the rightmost node in tree
while nodegetRight null
node node.getRight;
return node.getInfo;
private BSTNode removeNodeBSTNode node
Removes the information at the node referenced by tree.
The user's data in the node referenced by tree is no
longer in the tree. If tree is a leaf node or has only
a nonnull child pointer, the node pointed to by tree is
removed; otherwise, the user's data is replaced by its
logical predecessor and the predecessor's node is removed.
T data;
if nodegetLeft null
return node.getRight;
else if nodegetRight null
return node.getLeft;
else
data getPredecessornodegetLeft;
node.setInfodata;
node.setLeftrecRemovedata node.getLeft;
return node;
private BSTNode recRemoveT element, BSTNode node
Removes an element e from tree such that ecompareToelement
and returns true; if no such element exists, returns false.
if node null
found false;
else if elementcompareTonodegetInfo
node.setLeftrecRemoveelement node.getLeft;
else if elementcompareTonodegetInfo
node.setRightrecRemoveelement node.getRight;
else
node removeNodenode;
found true;
return node;
public boolean remove T element
Removes an element e from this BST such that ecompareToelement
and returns true; if no such element exists, returns false.
root recRemoveelement root;
return found;
Preorder traversal
public void preorder
System.out.println
Preorder Traversal: ;
ifrootnull
System.out.printlnTree is empty";
else
recPreorderroot;
private void recPreorderBSTNode node
ifnode null
System.out.printnodegetInfo;
recPreordernodegetLeft;
recPreordernodegetRight;
Inorder traversal
public void inorder
System.out.println
Inorder Traversal: ;
ifrootnull
System.out.printlnTree is empty";
else
recInorderroot;
private void recInorderBSTNode node
ifnode null
recIno
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
