Question
This is my code and I need to add this in. class BSTNode { E item; BSTNode left; BSTNode right; BSTNode parent; public BSTNode( E
This is my code and I need to add this in.
class BSTNode
{
E item;
BSTNode
BSTNode
BSTNode
public BSTNode( E x)
{
item=x;
left=null;
right=null;
parent=null;
int height=0;
}
public BSTNode (E x, BSTNode
{
item=x;
this.left=left;
this.right=right;
this.parent=parent;
}
public String toString(E x)
{
String valHeight=("i:"+x+", h:"+x.height);
return valHeight;
}
}
/*----------------class BST-------------------------*/
public class BST
{
private BSTNode
private int size;
public BST()
{
root=null;
size=0;
}
/*----------------public operations-------------------------*/
public int getSize()
{
return size;
}
public boolean find (E x)
{
if(find(x,root)==null)
return false;
else
return true;
}
public void preOrderTraversal()
{
preOrder(root);
System.out.println();
}
public void inOrderTraversal()
{
inOrder(root);
System.out.println();
}
public boolean insert (E x)
{
if (root==null)
{
root=new BSTNode (x, null, null, root);
size++;
return true;
}
BSTNode
BSTNode
while(p!=null)
{
if(x.compareTo(p.item)
{
parent=p;
p=p.left;
}
else if (x.compareTo(p.item)>0)
{
parent=p;
p=p.right;
}
else //duplicate value
return false;
}
//attach new node to parent
BSTNode
if (x.compareTo(parent.item)
parent.left=insertedNode;
else
parent.right=insertedNode;
size++;
return true;
//height
if x == null
x.height = -1;
else
x.height = 1 + max(x.left.height , x.right.height);
}//insert
public boolean remove (E x)
{
if(root==null)
return false;//x is not in tree
//find x
BSTNode
if (p==null)
return false;//x is not in free
//Case: p has a right child child and no left child
if( p.left == null && p.right != null)
deleteNodeWithOnlyRightChild(p);
//Case: p has a left child and has no right childelse
if( p.left !=null && p.right == null)
deleteNodeWithOnlyLeftChild(p);
//case: p has no children
else if (p.left ==null && p.right == null)
deleteLeaf(p);
else //case : p has two children. Delete successor node
{
BSTNode
p.item = succ.item;
//delete succ node
if(succ.right == null)
deleteLeaf(succ);
else
deleteNodeWithOnlyRightChild(succ);
}
return true;
//height
if x == null
x.height = -1;
else
x.height = 1 + max(x.left.height , x.right.height);
} //remove
/********************private methods ******************************/
private BSTNode
{
BSTNode
while (p!=null)
{
if (x.compareTo(p.item)
p=p.left;
else if (x.compareTo(p.item)>0)
p=p.right;
else //found x
return p;
}
return null; //x is not found
}
/***************** private remove helper methods ***************************************/
private void deleteLeaf (BSTNode
{
if (t==root)
root=null;
else
{
BSTNode
if( t.item.compareTo(parent.item)
parent.left = null;
else
parent.right = null;
}
size--;
}
private void deleteNodeWithOnlyLeftChild( BSTNode
{
if( t == root)
{
root = t.left;
root.parent = null; //WAS WRONG
t.left.parent = root;
}
else
{
BSTNode
if( t.item.compareTo( parent.item)
{
parent.left = t.left;
t.left.parent = parent;
}
else
{
parent.right = t.left;
t.left.parent = parent;
}
}
size--;
}
private void deleteNodeWithOnlyRightChild( BSTNode
{
if( t == root)
{
root = t.right;
root.parent = null; // WAS WRONG
t.right.parent = root;
}
else
{
BSTNode
if( t.item.compareTo(parent.item)
{
parent.left = t.right;
t.right.parent = parent;
}
else
{
parent.right = t.right;
t.right.parent = parent;
}
}
size--;
}
private BSTNode
{//only called when t.right != null
BSTNode
BSTNode
while (p.left != null)
{
parent = p;
p = p.left;
}
return p;
}//private traversal methods
private void preOrder ( BSTNode
{
if ( t != null)
{
System.out.print(t + " ");
preOrder(t.left);preOrder(t.right);
}
}
private void inOrder ( BSTNode
{
if ( t != null)
{
inOrder(t.left);
System.out.print(t + " " );
inOrder(t.right);
}
}
}
Add the following public methods to your BST class public EfindMin) public E findMax0 public E removeMing lireturn the item removed. Maintain height data in all node public E removeMax?//return the item removed. Maintain height data in all node public int getHeigh0 etunootheight This must be a constant time operati 3. 4. Create a separate BSTDriver class (not a main method inside of BST java) to test code. BST public methods in different orders. Check that your answers are corre printing the trees in inorder and in preorder. See class discussion. b. Create a BSTeStrings object. Insert 10-20 random strings and test all of t public methods in different orders. Check that your answers are correct by printing the trees in inorder and in preorder. Add the following public methods to your BST class public EfindMin) public E findMax0 public E removeMing lireturn the item removed. Maintain height data in all node public E removeMax?//return the item removed. Maintain height data in all node public int getHeigh0 etunootheight This must be a constant time operati 3. 4. Create a separate BSTDriver class (not a main method inside of BST java) to test code. BST public methods in different orders. Check that your answers are corre printing the trees in inorder and in preorder. See class discussion. b. Create a BSTeStrings object. Insert 10-20 random strings and test all of t public methods in different orders. Check that your answers are correct by printing the trees in inorder and in preorder
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started