Question

Instructions. You are provided four skeleton program named TreeNode.java and Binary Search Tree.java. The source files are av

TreeNode.java

public class TreeNode {
   public int key;
   public TreeNode p;
   public TreeNode left;
   public TreeNode right;
  
   public TreeNode () {
       p = left = right = null;
   }
  
   public TreeNode (int k) {
       key = k;
       p = left = right = null;
   }
}

BinarySearchTree.java

public class BinarySearchTree {
   public TreeNode root;
  
   public BinarySearchTree () {
       root = null;
   }
  
   public void inorder_tree_walk (TreeNode x) {
      
   }
  
   public TreeNode search (TreeNode x, int k) {
      
   }
  
   public TreeNode iterative_search (int k) {
      
   }
  
   public TreeNode minimum () {
      
   }
  
   public TreeNode maximum () {
      
   }
  
   public TreeNode successor (TreeNode x) {
      
   }
  
   public void insert (int k) {
      
   }
  
   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       int[] array = {15, 6, 18, 3, 7, 17, 20, 2, 4, 13, 9};
       BinarySearchTree bst;
       TreeNode n;
      
       bst = new BinarySearchTree();
       for (int i = 0; i < array.length; i++)
           bst.insert(array[i]);
      
       System.out.println("Inorder_tree_walk starts ------------------");
       bst.inorder_tree_walk(bst.root);
       System.out.println("Inorder_tree_walk ends ------------------");
       System.out.print("\n\n");
      
       System.out.println("Search starts ------------------");
       n = bst.search(bst.root, 13);
       System.out.println("found: " + n.key);
       System.out.println("Search ends ------------------");
       System.out.print("\n\n");

       System.out.println("Iterative search starts ------------------");
       n = bst.iterative_search(4);
       System.out.println("found: " + n.key);
       System.out.println("Iterative search ends ------------------");
       System.out.print("\n\n");
      
       System.out.println("Minimum starts ------------------");
       n = bst.minimum();
       System.out.println("Minimum key is " + n.key);
       System.out.println("Minimum ends ------------------");
       System.out.print("\n\n");
      
       System.out.println("Maximum starts ------------------");
       n = bst.maximum();
       System.out.println("Maximum key is " + n.key);
       System.out.println("Maximum ends ------------------");
       System.out.print("\n\n");

       System.out.println("Successor starts ------------------");
       n = bst.successor(bst.root.left.right.right);
       System.out.println("Successor key is " + n.key);
       System.out.println("Successor ends ------------------");
       System.out.print("\n\n");
   }

}

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

156 157 158 159 160 161 bst new BinarySearchTree ( ) ; for (int i 0; i < array.length; i+) bst.insert(array [i]) <terminated>

public class BinarySearchTree {

        class TreeNode {
                public int key;
                public TreeNode p;
                public TreeNode left;
                public TreeNode right;

                public TreeNode() {
                        p = left = right = null;
                }

                public TreeNode(int k) {
                        key = k;
                        p = left = right = null;
                }
        }

        public TreeNode root;

        public BinarySearchTree() {
                root = null;
        }

        public void inorder_tree_walk(TreeNode x) {
                if(x != null) {
                        inorder_tree_walk(x.left);
                        System.out.println(x.key);
                        inorder_tree_walk(x.right);
                }
        }

        public TreeNode search(TreeNode x, int k) {
                if(x == null) {
                        return null;
                }
                if(x.key == k) {
                        return x;
                }
                if(x.key > k) {
                        return search(x.left, k);
                } else {
                        return search(x.right, k);
                }
        }

        public TreeNode iterative_search(int k) {
                TreeNode start = root;
                
                while(start != null) {
                        if(start.key == k) {
                                return start;
                        }
                        if(start.key > k) {
                                start = start.left;
                        } else {
                                start = start.right;
                        }
                }
                
                return start;
        }

        public TreeNode minimum() {
                TreeNode start = root;
                if(root == null) {
                        return null;
                }
                
                while(start.left != null) {
                        start = start.left;
                }
                
                return start;
        }

        public TreeNode maximum() {
                TreeNode start = root;
                if(root == null) {
                        return null;
                }
                
                while(start.right != null) {
                        start = start.right;
                }
                
                return start;
        }

        public TreeNode successor(TreeNode x) {
                if (x == null) {
                        return null;
                } else if (x.right != null) {
                        // successor is leftmost Entry in right subtree of e
                        TreeNode p = x.right;
                        while (p.left != null) {
                                p = p.left;
                        }
                        return p;

                } // e has a right child
                else {

                        // go up the tree to the left as far as possible, then go up
                        // to the right.
                        TreeNode temp = x.p;
                        TreeNode ch = x;
                        while ((temp != null) && (ch == temp.right)) {
                                ch = temp;
                                temp = temp.p;
                        } // while
                        return temp;
                } // e has no right child
        }

        public void insert(int k) {
                TreeNode newNode = new TreeNode(k);
                
                if(root == null) {
                        root = newNode;
                } else {
                        TreeNode parent = root;
                        while(true) {
                                if(parent.key == k) {
                                        return;
                                }
                                if(parent.key > k) {
                                        if(parent.left == null) {
                                                parent.left = newNode;
                                                newNode.p = parent;
                                                return;
                                        } else {
                                                parent = parent.left;
                                        }
                                } else {
                                        if(parent.right == null) {
                                                parent.right = newNode;
                                                newNode.p = parent;
                                                return;
                                        } else {
                                                parent = parent.right;
                                        }
                                }
                        }
                }
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
                int[] array = { 15, 6, 18, 3, 7, 17, 20, 2, 4, 13, 9 };
                BinarySearchTree bst;
                TreeNode n;

                bst = new BinarySearchTree();
                for (int i = 0; i < array.length; i++)
                        bst.insert(array[i]);

                System.out.println("Inorder_tree_walk starts ------------------");
                bst.inorder_tree_walk(bst.root);
                System.out.println("Inorder_tree_walk ends ------------------");
                System.out.print("\n\n");

                System.out.println("Search starts ------------------");
                n = bst.search(bst.root, 13);
                System.out.println("found: " + n.key);
                System.out.println("Search ends ------------------");
                System.out.print("\n\n");

                System.out.println("Iterative search starts ------------------");
                n = bst.iterative_search(4);
                System.out.println("found: " + n.key);
                System.out.println("Iterative search ends ------------------");
                System.out.print("\n\n");

                System.out.println("Minimum starts ------------------");
                n = bst.minimum();
                System.out.println("Minimum key is " + n.key);
                System.out.println("Minimum ends ------------------");
                System.out.print("\n\n");

                System.out.println("Maximum starts ------------------");
                n = bst.maximum();
                System.out.println("Maximum key is " + n.key);
                System.out.println("Maximum ends ------------------");
                System.out.print("\n\n");

                System.out.println("Successor starts ------------------");
                n = bst.successor(bst.root.left.right.right);
                System.out.println("Successor key is " + n.key);
                System.out.println("Successor ends ------------------");
                System.out.print("\n\n");
        }

}

please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
TreeNode.java public class TreeNode {    public int key;    public TreeNode p;    public TreeNode...
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
  • The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode...

    The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...

  • BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> {    private TreeNode<E>...

    BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> {    private TreeNode<E> overallRoot;    public BST() {        overallRoot = null;    }    // ************ ADD ************ //    public void add(E addThis) {        if (overallRoot == null) {            overallRoot = new TreeNode<>(addThis);        } else {            add(overallRoot, addThis);        }    }    private TreeNode<E> add(TreeNode<E> node, E addThis) {        if...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

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

  • public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary...

    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; } public Buildbst getRight() { return right; } public void setRight(Buildbst right) { this.right = right; } }...

  • Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...

    Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>       T data       TreeNode<T> left       TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • Write a function int levelSearch(Node* root, int key) that takes as input the root node of...

    Write a function int levelSearch(Node* root, int key) that takes as input the root node of a Binary Search tree and a key. The function searches the key in the BST and returns the level of the found node. If the key is not found in the tree, return -1. The level starts at 0 for the root node and increases from top to bottom. We have defined the following node C++ Node class for you: class Node { public:         ...

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

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

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