Question

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 anything in the main method.

code provided


import java.util.Scanner;

public class BST<Key extends Comparable<Key>, Value> {

   private Node root; // root of binary search tree

   private class Node {
       private Key key; // sorted by key
       private Value value; // data stored in the node
       private Node left, right; // pointers to left and right subtrees
       private int size; // number of nodes in subtree

       public Node(Key k, Value v) {
           this.key = k;
           this.value = v;          
       }
      
       public Node(Key k, Value v, int size){
           this.key = k;
           this.value = v;
           this.size = size;
       }
   }

   // initialize an empty BST
   public BST() {

   }

   // returns true if tree is empty
   public boolean isEmpty() {
       return size() == 0;
   }

   // returns the number of key-value pairs in the tree
   public int size() {
       return size(root);
   }

   // return the number of key-value pairs in BST rooted at x
   public int size(Node n) {
       // Implement this method
   }

   // Insert key-value pairs in BST, if a key already exists update the value
   public void insert(Key key, Value value){
   // Use your implementation from the lab here
   }
  
   // Search BST for a given key
   public Value get(Key key) {
// Use your implementation from the lab here
   }
  
   // Prints the tree nodes using inorder traversal
   public void inorder(Node root){
       // Use your implementation from the lab here
   }
  
   // remove the node with the given key
   public void remove(Node root, Key key){
       // Implement this method
   }
  

// Returns the minimum key in the tree
   public Key getMin(Node n){
       // Implement this method
   }
  
   // Returns the maximum key in the tree
   public Key getMax(Node n){
// Implement this method
   }
  
   // Returns the height of the tree
   public int height(Node n){
       // Implement this method
   }
  
   public static void main(String[] args){
       Scanner input = new Scanner(System.in);
       BST<Integer, String> bst = new BST<Integer, String>();      
       System.out.println("Enter key value pairs for the BST (-1 to end):");
       while(input.hasNext()){
           int key = input.nextInt();
           if(key == -1) break;              

           String value = input.next();
           bst.insert(key, value);
       }
      
       System.out.println("The size of the BST is:" + bst.size());
       System.out.println("The height of the tree is:" + bst.height(bst.root));
       System.out.println("The node with the minimum key is:" + bst.getMin(bst.root));
       System.out.println("The value stored at the minimum key is: " + bst.get(bst.getMin(bst.root)));
       System.out.println("The node with the maximum key is:" + bst.getMax(bst.root));
       System.out.println("Removing the maximum key: ");
       bst.remove(bst.root, bst.getMax(bst.root));
       System.out.println("The height of the tree is:" + bst.height(bst.root));
       System.out.println("Printing the tree nodes using inorder traversal");
       bst.inorder(bst.root);
       System.out.println("The size of the BST after removal is:" + bst.size());
       System.out.println("Removing the root node: ");
       bst.remove(bst.root, bst.root.key);
       System.out.println("The size of the BST after removal is:" + bst.size());
       System.out.println("The height of the tree after removing the root is:" + bst.height(bst.root));
       bst.inorder(bst.root);
       System.out.println("Removing the right child of the root node: ");
       bst.remove(bst.root, bst.root.right.key);
       bst.inorder(bst.root);
       System.out.println("Size of the BST after removal is:" + bst.size());
   }
}

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



import java.util.NoSuchElementException;
import java.util.Scanner;

public class BST<Key extends Comparable<Key>, Value> {

private Node root; // root of binary search tree

private class Node {
private Key key; // sorted by key
private Value value; // data stored in the node
private Node left, right; // pointers to left and right subtrees
private int size; // number of nodes in subtree

public Node(Key k, Value v) {
this.key = k;
this.value = v;
}
  
public Node(Key k, Value v, int size){
this.key = k;
this.value = v;
this.size = size;
}
}

// initialize an empty BST
public BST() {

}

// returns true if tree is empty
public boolean isEmpty() {
return size() == 0;
}

// returns the number of key-value pairs in the tree
public int size() {
return size(root);
}

// return the number of key-value pairs in BST rooted at x
public int size(Node n) {
if(n == null) return 0;
else return n.size;
}

// Insert key-value pairs in BST, if a key already exists update the value
public void insert(Key key, Value value){
if (key == null) throw new IllegalArgumentException("calls put() with a null key");
if (value == null) {
remove(root,key);
return;
}
root = insert(root, key, value);
  
}
private Node insert(Node x, Key key, Value val) {
if (x == null) return new Node(key, val, 1);
int cmp = key.compareTo(x.key);
if (cmp < 0) x.left = insert(x.left, key, val);
else if (cmp > 0) x.right = insert(x.right, key, val);
else x.value = val;
x.size = 1 + size(x.left) + size(x.right);
return x;
}
  
// Search BST for a given key
public Value get(Key key) {
return get(root,key);
}
private Value get(Node x, Key key) {
if (key == null) throw new IllegalArgumentException("calls get() with a null key");
if (x == null) return null;
int cmp = key.compareTo(x.key);
if (cmp < 0) return get(x.left, key);
else if (cmp > 0) return get(x.right, key);
else return x.value;
}

// Prints the tree nodes using inorder traversal
public void inorder(Node root){
if(root==null) return;
inorder(root.left);
System.out.println(root.value + " ");
inorder(root.right);
}
  
// remove the node with the given key
public void remove(Node root, Key key){
if (key == null) throw new IllegalArgumentException("calls delete() with a null key");
root = delete(root,key);
}
private Node delete(Node n,Key key){
if(n == null) return null;
int comp = key.compareTo(n.key);
if(comp < 0) n.left = delete(n.left, key);
else if(comp > 0) n.right = delete(n.right, key);
else{
if(n.right == null) return n.left;
if(n.left == null) return n.right;
Node t = n;
n.right = deleteMin(t.right);
n.left = t.left;
}
n.size = size(n.left) + size(n.right) + 1;
return n;
}
public void deleteMin() {
if (isEmpty()) throw new NoSuchElementException("Symbol table underflow");
root = deleteMin(root);
  
}

private Node deleteMin(Node x) {
if (x.left == null) return x.right;
x.left = deleteMin(x.left);
x.size = size(x.left) + size(x.right) + 1;
return x;
}


// Returns the minimum key in the tree
public Key getMin(Node n){
if(isEmpty()) System.out.println("Empty tree");
return min(root).key;

}
  
public Node min(Node n){
if(n.left == null) return n;
else return min(n.left);
}
// Returns the maximum key in the tree
public Key getMax(Node n){
if (isEmpty()) throw new NoSuchElementException("calls max() with empty symbol table");
return max(root).key;
}
private Node max(Node x) {
if (x.right == null) return x;
else return max(x.right);
}
// Returns the height of the tree
public int height(Node n){
if(n == null) return -1;
return 1 + Math.max(height(n.left), height(n.right));
}
  
public static void main(String[] args){
Scanner input = new Scanner(System.in);
BST<Integer, String> bst = new BST<Integer, String>();
System.out.println("Enter key value pairs for the BST (-1 to end):");
while(input.hasNext()){
int key = input.nextInt();
if(key == -1) break;

String value = input.next();
bst.insert(key, value);
}
  
System.out.println("The size of the BST is:" + bst.size());
System.out.println("The height of the tree is:" + bst.height(bst.root));
System.out.println("The node with the minimum key is:" + bst.getMin(bst.root));
System.out.println("The value stored at the minimum key is: " + bst.get(bst.getMin(bst.root)));
System.out.println("The node with the maximum key is:" + bst.getMax(bst.root));
System.out.println("Removing the maximum key: ");
bst.remove(bst.root, bst.getMax(bst.root));
System.out.println("The height of the tree is:" + bst.height(bst.root));
System.out.println("Printing the tree nodes using inorder traversal");
bst.inorder(bst.root);
System.out.println("The size of the BST after removal is:" + bst.size());
System.out.println("Removing the root node: ");
bst.remove(bst.root, bst.root.key);
System.out.println("The size of the BST after removal is:" + bst.size());
System.out.println("The height of the tree after removing the root is:" + bst.height(bst.root));
bst.inorder(bst.root);
System.out.println("Removing the right child of the root node: ");
bst.remove(bst.root, bst.root.right.key);
bst.inorder(bst.root);
System.out.println("Size of the BST after removal is:" + bst.size());
}
}

------------------OUTPUT SCREEN------------------

Enter key value pairs for the BST (1 to end): The size of the BST is 3 The height of the tree is:2 The node with the minimum

/* PLEASE UPVOTE (THANK YOU IN ADVANCE ) IF YOU SATISFY WITH THE ANSWER. IF ANY QUERY ASK ME IN COMMENT SECTION I WILL RE-SOLVE THE QUESTION FOR YOU */

Add a comment
Know the answer?
Add Answer to:
In this assignment, you will add several methods to the Binary Search Tree. You should have compl...
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
  • Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores...

    Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the largest absolute value in the tree. The language is C++ Want the height #4 Coding [6 points] Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the height of the...

  • 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....

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • ​​​​​ You should now be able to edit the IntTree class. Implement each of the functions labeled...

    ​​​​​ You should now be able to edit the IntTree class. Implement each of the functions labeled with You are not allowed to use any kind of loop in your solutions. You may not modify the Node class in any way You may not modify the function headers of any of the functions already present in the file. You may not add any fields to the IntTree class. You may not change or remove the line that reads “package hw2;”...

  • Question B1 You are given the following Java classes: public class Queue { private static class...

    Question B1 You are given the following Java classes: public class Queue { private static class Node { Object object; Node next; Node () { object = null; next = null; } Node (Object object, Node next) { this.object = object; this.next = next; private Node header; private int size = 0; // size shows the no of elements in queue public Object dequeue () { if (size == 0 ) { return null; else { Object remove_object = header.object;...

  • Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import...

    Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

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