Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

class BSTNode

{

E item;

BSTNode left;

BSTNode right;

BSTNode parent;

public BSTNode( E x)

{

item=x;

left=null;

right=null;

parent=null;

int height=0;

}

public BSTNode (E x, BSTNode left, BSTNode right, BSTNode parent)

{

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 root;

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 parent=null;

BSTNode p =root;

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 insertedNode = new BSTNode(x, null, null, parent);

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 p = find(x, root);

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 succ = getSuccessorNode(p);;

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 find(E, x, BSTNode t)

{

BSTNode p =t;

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 t)

{

if (t==root)

root=null;

else

{

BSTNode parent=t.parent;

if( t.item.compareTo(parent.item)

parent.left = null;

else

parent.right = null;

}

size--;

}

private void deleteNodeWithOnlyLeftChild( BSTNode t)

{

if( t == root)

{

root = t.left;

root.parent = null; //WAS WRONG

t.left.parent = root;

}

else

{

BSTNode parent = t.parent;

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 t)

{

if( t == root)

{

root = t.right;

root.parent = null; // WAS WRONG

t.right.parent = root;

}

else

{

BSTNode parent = t.parent;

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 getSuccessorNode(BSTNode t)

{//only called when t.right != null

BSTNode parent = t;

BSTNode p = t.right;

while (p.left != null)

{

parent = p;

p = p.left;

}

return p;

}//private traversal methods

private void preOrder ( BSTNode t)

{

if ( t != null)

{

System.out.print(t + " ");

preOrder(t.left);preOrder(t.right);

}

}

private void inOrder ( BSTNode t)

{

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

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

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions