Question: Implement the tree - balancing algorithm discussed in the lecture. package BST; import interfaces.BSTInterface; import nodes.BSTNode; import queues. * ; public class BinarySearchTree > implements
Implement the treebalancing algorithm discussed in the lecture.
package BST;
import interfaces.BSTInterface;
import nodes.BSTNode;
import queues.;
public class BinarySearchTree
implements BSTInterface
protected BSTNode root;
boolean found;
protected LinkedQueue inOrderQueue;
protected LinkedQueue preOrderQueue;
protected LinkedQueue postOrderQueue;
public BinarySearchTree
root null;
public boolean isEmpty
returnroot null;
@Override
public int size
return recSizeroot;
private int recSizeBSTNode tree
if tree null
return ;
else
return recSizetreegetLeft recSizetreegetRight;
@Override
public boolean containsT element
return recContainselement root;
private boolean recContainsT element, BSTNode tree
if tree null
return false;
else if elementcompareTotreegetInfo
return recContainselement tree.getLeft;
else if elementcompareTotreegetInfo
return recContainselement tree.getRight;
else
return true;
private T recGetT element, BSTNode tree
if tree null
return null;
else if elementcompareTotreegetInfo
return recGetelement tree.getLeft;
else if elementcompareTotreegetInfo
return recGetelement tree.getRight;
else
return tree.getInfo;
public T getT element
return recGetelement root;
private BSTNode recAddT element, BSTNode tree
if tree null
tree new BSTNodeelement;
else if elementcompareTotreegetInfo
tree.setLeftrecAddelement tree.getLeft;
else
tree.setRightrecAddelement tree.getRight;
return tree;
public void addT element
root recAddelement root;
public boolean removeT element
root recRemoveelement root;
return found;
private BSTNode recRemoveT element, BSTNode tree
if tree null
found false;
else if elementcompareTotreegetInfo
tree.setLeftrecRemoveelement tree.getLeft;
else if elementcompareTotreegetInfo
tree.setRightrecRemoveelement tree.getRight;
else
tree removeNodetree;
found true;
return tree;
private BSTNode removeNodeBSTNode tree
T data;
if treegetLeft null
return tree.getRight;
else if treegetRight null
return tree.getLeft;
else
data getPredecessortreegetLeft;
tree.setInfodata;
tree.setLeftrecRemovedata tree.getLeft;
return tree;
private T getPredecessorBSTNode tree
while treegetRight null
tree tree.getRight;
return tree.getInfo;
public int resetint orderType
int numNodes size;
if orderType INORDER
inOrderQueue new LinkedQueue;
inOrderroot;
else if orderType PREORDER
preOrderQueue new LinkedQueue;
preOrderroot;
if orderType POSTORDER
postOrderQueue new LinkedQueue;
postOrderroot;
return numNodes;
public T getNextint orderType
if orderType INORDER
return inOrderQueue.dequeue;
else if orderType PREORDER
return preOrderQueue.dequeue;
else if orderType POSTORDER
return postOrderQueue.dequeue;
else
return null;
private void inOrderBSTNode tree
if tree null
inOrdertreegetLeft;
inOrderQueue.enqueuetreegetInfo;
inOrdertreegetRight;
private void preOrderBSTNode tree
if tree null
preOrderQueue.enqueuetreegetInfo;
preOrdertreegetLeft;
preOrdertreegetRight;
private void postOrderBSTNode tree
if tree null
postOrdertreegetLeft;
postOrdertreegetRight;
postOrderQueue.enqueuetreegetInfo;
package nodes;
public class BSTNode
private T info;
private BSTNode left;
private BSTNode right;
public BSTNodeT info
this.info info;
left null;
right null;
public void setInfoT info
this.info info;
public T getInfo
return info;
public void setLeftBSTNode link
left link;
public void setRightBSTNode link
right link;
public BSTNode getLeft
return left;
public BSTNode getRight
return right;
package interfaces;
public interface BSTInterface
static final int INORDER ;
static final int PREORDER ;
static final int POSTORDER ;
boolean isEmpty;
int size;
boolean containsT element;
boolean removeT element;
T getT element;
void addT element;
int resetint orderType;
T getNext
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
