Question

Removing Nodes from a Binary Tree in Java This section requires you to complete the following...

Removing Nodes from a Binary Tree in Java

This section requires you to complete the following method within BinaryTree.java: public void remove(K key) { } The remove method should remove the key from the binary tree and modify the tree accordingly to maintain the Binary Search Tree Principle. If the key does not exist in the binary tree, no nodes should be removed. In case there is a non-null left child, it should take the place of the removed node. However, if the left child is null, then a non-null right child may take its place. If both children are null, then a null node should take the place of the removed node.

Node.Java

public class Node<K extends Comparable<K>, V> {
    private K key;
    private V value;
    private Node<K, V> left, right;

    public Node(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public Node(K key, V value, Node<K, V> left, Node<K, V> right) {
        this.key = key;
        this.value = value;
        this.left = left;
        this.right = right;
    }

    public boolean equals(Node<K, V> other) {
        if (other == null) return false;
        boolean left, right;
        if (this.left == null) {
            left = other.left == null;
        }
        else {
            left = this.left.equals(other.left);
        }
        if (this.right == null) {
            right = other.right == null;
        }
        else {
            right = this.right.equals(other.right);
        }
        return left && right && this.key.equals(other.key) && this.value.equals(other.value);
    }

    public K getKey() {
        return key;
    }

    public V getValue() {
        return value;
    }

    public void setValue(V value) {
        this.value = value;
    }

    public Node<K, V> getLeft() {
        return left;
    }

    public void setLeft(Node<K, V> left) {
        this.left = left;
    }

    public Node<K, V> getRight() {
        return right;
    }

    public void setRight(Node<K, V> right) {
        this.right = right;
    }
}

BinaryTree.Java

public class BinaryTree<K extends Comparable<K>, V> {
    private Node<K, V> root;

    public BinaryTree(Node root) {
        this.root = root;
    }

    public Node<K, V> getRoot() {
        return this.root;
    }
    public void remove(K key) {

//COMPLETE THIS FUNCTION

}

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

// Node.java

public class Node<K extends Comparable<K>, V> {
  
   private K key;
private V value;
private Node<K, V> left, right;

public Node(K key, V value) {
this.key = key;
this.value = value;
}

public Node(K key, V value, Node<K, V> left, Node<K, V> right) {
this.key = key;
this.value = value;
this.left = left;
this.right = right;
}

   public boolean equals(Node<K, V> other) {
if (other == null) return false;
boolean left, right;
if (this.left == null) {
left = other.left == null;
}
else {
left = this.left.equals(other.left);
}
if (this.right == null) {
right = other.right == null;
}
else {
right = this.right.equals(other.right);
}
return left && right && this.key.equals(other.key) && this.value.equals(other.value);
}

   public K getKey() {
return key;
}

public V getValue() {
return value;
}

public void setValue(V value) {
this.value = value;
}

public Node<K, V> getLeft() {
return left;
}

public void setLeft(Node<K, V> left) {
this.left = left;
}

public Node<K, V> getRight() {
return right;
}

public void setRight(Node<K, V> right) {
this.right = right;
}
}  

//end of Node.java

// BinaryTree.java

public class BinaryTree<K extends Comparable<K>, V> {
private Node<K, V> root;

public BinaryTree(Node root) {
this.root = root;
}

public Node<K, V> getRoot() {
return this.root;
}
public void remove(K key) {
  
       Node<K, V> parent = null; // set parent to null
       Node<K, V> current = root; // set current to root
      
       // loop to get the node with key
       while(current != null)
       {
           // node found, exit the loop
           if(current.getKey().compareTo(key) == 0)
           {
               break;
           }
           else if(current.getKey().compareTo(key) < 0) // if current's key < key, then key if present must be in right subtree since it is a BST
           {
               parent = current;
   current = current.getRight();
           }
           else                // if current's key > key, then key if present must be in left subtree since it is a BST
           {
               parent = current;
   current = current.getLeft();
           }
       }
      
       // key found in tree
       if (current != null)
       {  
           // current is a leaf node i.e no children  
           if(current.getLeft() == null && current.getRight() == null)
           {
               if(parent == null) // if root node, set root to null
               {
                   root = null;
               }
               else // not a root node
               {
                   // if current is left child of parent, set left child of parent to null
                   if(parent.getLeft() == current)
                       parent.setLeft(null);
                   else    // if current is right child of parent, set right child of parent to null
                       parent.setRight(null);
               }
           }
           else if(current.getLeft() == null) // current has no left child, replace the node with right child
           {
               // root node, set root to its right child
               if(parent == null)
               {
                   root = root.getRight();
               }
               else // not a root node
               {
                   // if current is left child of parent, set left child of parent to right child of current
                   if(parent.getLeft() == current)
                       parent.setLeft(current.getRight());
                   else       // if current is right child of parent, set right child of parent to right child of current
                       parent.setRight(current.getRight());
               }
           }
           else
           {
               // has left child
              
               // set rightMost to the left child of current
               Node<K, V> rightMost = current.getLeft();
              
               // loop to get the right most node of the left child of current
               while(rightMost.getRight() != null)
                   rightMost = rightMost.getRight();
              
               // set the right child of right-most node to right child of current
               rightMost.setRight(current.getRight());
               if(parent == null)   // root node, set root to its left child
                   root = root.getLeft();
               else // not a root node
               {
                   // if current is left child of parent, set left child of parent to left child of current
                   if(parent.getLeft() == current)
                       parent.setLeft(current.getLeft());
                   else   // if current is right child of parent, set right child of parent to left child of current  
                       parent.setRight(current.getLeft());
               }          
           }
       }  
   }
}

//end of BinaryTree.java

Add a comment
Know the answer?
Add Answer to:
Removing Nodes from a Binary Tree in Java This section requires you to complete the following...
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
  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • Have to write the tree into a text file? JAVA CODE Binary search tree This is...

    Have to write the tree into a text file? JAVA CODE Binary search tree This is the tree public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left;...

  • I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\...

    I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\ package Tree; public class BinaryTree { private TreeNode root; // head of the list    //constructor - create an empty binary tree public BinaryTree() { root = null;    }    //isEmpty() - return true if tree is empty, false otherwise public boolean isEmpty() { return (root == null); }    //deleteTree() - remove all items from tree public void deleteList() { root =...

  • Java : This function is to search through a binary tree left and right and return...

    Java : This function is to search through a binary tree left and right and return a count of the nodes above depth k. This is what I have so far, but I'm getting a Null pointer exception. public class MyIntSET {    private Node root;    private static class Node {        public final int key;        public Node left, right;        public Node(int key) { this.key = key; }    }    public int sizeAboveDepth(int...

  • Add printRang method to BST.java that, given a low key value, and high key value, print...

    Add printRang method to BST.java that, given a low key value, and high key value, print all records in a sorted order whose values fall between the two given keys. (Both low key and high key do not have to be a key on the list). BST.java import java.lang.Comparable; /** Binary Search Tree implementation for Dictionary ADT */ class BST<Key extends Comparable<? super Key>, E> implements Dictionary<Key, E> { private BSTNode<Key,E> root; // Root of the BST int nodecount; //...

  • Complete HeapPriorityQueue (7 points). In lecture we implemented HeapPriorityQueue using an array-based representation of a heap...

    Complete HeapPriorityQueue (7 points). In lecture we implemented HeapPriorityQueue using an array-based representation of a heap (a complete binary tree whose entries satisfy the heap-order property). For this problem, complete the included HeapPriorityQueue class by using the LinkedBinaryTree class to represent a heap. Hint: the most challenging part of this problem is identifying the last Position in the heap and the next available Position in the heap. It is suggested that you review the array-based heap to better understand how...

  • Need help for three BinaryTree class public class BinaryTree { //Implements a Binary Tree of Strings...

    Need help for three BinaryTree class public class BinaryTree { //Implements a Binary Tree of Strings    private class Node {        private Node left;        private String data;        private Node right;        private Node parent; // reference to the parent node        // the parent is null for the root node        private Node(Node L, String d, Node r, Node p) {            left = L;            data...

  • Can you take a look at my code that why the maxDepth function is not working?...

    Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree {          class Node{        int key;        Node left,right;               public Node(int item) {            key = item;            left = right = null;        }    }       Node root;       public void BinaryTree(){        root = null;    }           void...

  • JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth    *    * Returns the number of nodes with...

    JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth    *    * Returns the number of nodes with depth == d    * Precondition: none    *    * param: d the depth to search for    *    * hint: use a recursive helper function    *        * ToDo 4    */    public int numNodesAtDepth(int d) {        return -1;    } **********USEFUL CODE FROM THE ASSIGNMENT:*********** public class simpleBST<Key extends Comparable<Key>, Value> {    private Node root;            ...

  • A binary tree is constructed of nodes that are instances of the following class: public class...

    A binary tree is constructed of nodes that are instances of the following class: public class Node public int val public Node left public Node right) Consider the following method public static Node mystery Node root) rootghtanul return root else return mystery root ) You consult Professor Kennedy and hegves an opinion about what the method does when passed a reference to the root node of a binary tree. Assuming he is correct what does the mystery function do? it...

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