Question

2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implpublic MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for thea) find the leftmost member of the rightChild. b) Replace the nodes data item with the data item found c) Delete the node thIN JAVA

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Main.java


public class Main {
   public static void main(String [] args){
   MyBST b=new MyBST();
   b.insert(5);
   b.insert(18);
   b.insert(0);
   b.insert(16);
   b.insert(29);
   b.insert(10);
   b.insert(17);
   b.insert(23);
   b.insert(28);
   b.insert(21);
   b.insert(22);
   b.insert(3);
   b.insert(2);
   b.insert(4);
   b.insert(1);
   b.insert(-4);
   b.insert(-3);
   System.out.println("Printing inOrder:");
   b.printInOrder();
   System.out.println("Printing preOrder:");
   b.printPreOrder();
   System.out.println("Printing postOrder:");
   b.printPostOrder();
   System.out.println(b.lookup(2));
   b.delete(5);
   b.delete(0);
   b.delete(-4);
   System.out.println("Printing inOrder:");
   b.printInOrder();
  
  
   }
  

}


MyBST.java


public class MyBST implements BST{
   MyTreeNode root;
   public MyBST(){
       root=null;
   }
  

   public void insert(Comparable x) {
       if(root!=null){
           root.insert(x);
       }
       else{
           root=new MyTreeNode(x);
       }
      
      
   }

  
  

   public void printPreOrder() {
       root.printPreOrder();
          
   }

   public void printInOrder() {
       root.printInOrder();
              
   }

   public void printPostOrder() {
       root.printPostOrder();
      
   }


   @Override
   public boolean lookup(Comparable x) {
       if(root==null){
           return false;
       }
       return root.lookup(x);
   }


   @Override
   public void delete(Comparable x) {
       if(root==null){
           return;
       }
      
       root.delete(x);
   }

  

  
  

  
}


BST.java


public interface BST<T extends Comparable <T>> {
   public void insert(T x);
   public void delete(T x);
   public boolean lookup(T x);
   public void printPreOrder();
   public void printInOrder();
   public void printPostOrder();

}

MyTreeNode.java


public class MyTreeNode<T extends Comparable<T>> {
   public T data;
   public MyTreeNode<T> leftChild;
   public MyTreeNode<T> rightChild;
   public MyTreeNode<T> parent;
  
   public MyTreeNode(T data){
       this.data=data;
       leftChild=null;
       rightChild=null;
       parent=null;
      
   }
  
   public void insert(T x){
       MyTreeNode newnode=new MyTreeNode(x);
       if(x.compareTo(data)>0){
           if(rightChild==null){
               rightChild=newnode;
               newnode.parent=this;  
           }
           if(rightChild!=null){
               rightChild.insert(x);
           }
          
          
       }
       if(x.compareTo(data)<0){
           if(leftChild==null){
               leftChild=newnode;
               newnode.parent=this;
           }
           if(leftChild!=null){
               leftChild.insert(x);
           }
          
       }
   }
   public boolean lookup(T x){
       if(data.compareTo(x)==0){
           return true;
       }
       else if((x.compareTo(data)>0)&&(rightChild!=null)){
           return rightChild.lookup(x);
       }
       else if((x.compareTo(data)<0)&&(leftChild!=null)){
           return leftChild.lookup(x);
       }
       else{
          
       return false;
       }
      
   }
   public void delete(T x){
       //System.out.println("I am called!");
       if(!lookup(x)){
           //System.out.println("The item you want to delete is not in the tree.");
           return;
          
       }
       //System.out.println("In between");
       if(lookup(x)){
           if(data.compareTo(x)==0){
               if(parent==null){
                   if((leftChild==null)&&(rightChild==null)){
                   this.data=null;
                   }
                   if(((leftChild==null)&&(rightChild!=null))){
                   rightChild.parent=null;
                   }
                   if(((leftChild!=null)&&(rightChild==null))){
                   leftChild.parent=null;
                   }
                   if(((leftChild!=null)&&(rightChild!=null))){  
                       MyTreeNode tempnode=rightChild;
                       if(rightChild.leftChild!=null){
                           tempnode=rightChild.leftChild;
                           while(tempnode.leftChild!=null){
                           tempnode=tempnode.leftChild;
                           }  
                       }
                       Comparable theswitch=tempnode.data;
                       delete((T) theswitch);
                       this.data=(T) theswitch;
                   }
                  
               }
               //if i have no children
               else if((leftChild==null)&&(rightChild==null)){
                   if(data.compareTo(parent.data)<0){
                   parent.leftChild=null;
                   //System.out.println("parent of left child to null!");
                   }
                  
                  
                   if(data.compareTo(parent.data)>0){
                       parent.rightChild=null;
                       //System.out.println("parent of right child to null!");
                      
                   }
               }
              
               //if I am the left baby
               else if(parent.leftChild.data.compareTo(x)==0){
                   if((leftChild!=null)&&(rightChild==null)){
                       parent.leftChild=leftChild;
                      
                   }
                   if((leftChild==null)&&(rightChild!=null)){
                       parent.leftChild=rightChild;
                      
                   }
                   if((leftChild!=null)&&(rightChild!=null)){
                       MyTreeNode tempnode=rightChild;
                       if(rightChild.leftChild!=null){
                           tempnode=rightChild.leftChild;
                           while(tempnode.leftChild!=null){
                           tempnode=tempnode.leftChild;
                           }  
                       }
                       Comparable theswitch=tempnode.data;
                       delete((T) theswitch);
                       this.data=(T) theswitch;
                      
                   }
                  
                  
                  
               }
               //i am the right baby
               else if(parent.rightChild.data.compareTo(x)==0){
                   if(((leftChild==null)&&(rightChild!=null))){
                   parent.rightChild=rightChild;  
                  
               }
                   if((leftChild!=null)&&(rightChild==null)){
                       parent.rightChild=leftChild;
                      
                   }
                   if((leftChild!=null)&&(rightChild!=null)){
                       MyTreeNode tempnode=rightChild;
                       if(rightChild.leftChild!=null){
                           tempnode=rightChild.leftChild;
                           while(tempnode.leftChild!=null){
                           tempnode=tempnode.leftChild;
                           }  
                       }
                       Comparable theswitch=tempnode.data;
                       delete((T) theswitch);
                       this.data=(T) theswitch;
                      
                   }
               }
           }
           if(data.compareTo(x)<0){
               //System.out.println("Right deleting");
               rightChild.delete(x);
           }
           if(data.compareTo(x)>0){
               //System.out.println("Left deleting");
               leftChild.delete(x);
           }
              
           }
          
          
          
       }
      
      
      
      
  
  
  
  
  
  
  
  
   public void printInOrder(){
       if(leftChild !=null){
           leftChild.printInOrder();
       }
       System.out.println(data);
       if(rightChild!=null){
           rightChild.printInOrder();  
       }
  
   }
   public void printPreOrder(){
       System.out.println(data);
       if(leftChild !=null){
           leftChild.printPreOrder();
       }
      
       if(rightChild!=null){
           rightChild.printPreOrder();  
       }
  
   }
   public void printPostOrder(){
      
       if(leftChild !=null){
           leftChild.printPostOrder();
       }
      
       if(rightChild!=null){
           rightChild.printPostOrder();  
       }
       System.out.println(data);
  
   }
  

}


El Problems @ Javadoc Declaration Console X <terminated> Main [Java Application] C:\Program Files Vava\jrel.8.0_201\bin\javaw

Add a comment
Know the answer?
Add Answer to:
IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT