Question

Needed in Java Code Create a Binary Search Tree with the following elements in the order...

Needed in Java Code

Create a Binary Search Tree with the following elements in the order mentioned below: 5, 85, 89, 3, 2, 8, 65, 92

1.Print the Pre-order of this tree

2. Print the height and the balance factor of the nodes in the order they were inserted (5, 85, 89, 3, 2, 8, 65, 92) in the form of a table with three columns and 9 rows. Use column headers “Node”, “Height”, and “Balance Factor” for the three columns respectively. Use the following table as reference for your output. Don't worry about printing the table borders. Note: You are not balancing the tree. You are just computing the heights and balance factors of a BST.

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

If you have doubts please comment don't dislike.

public class BinarySearchTree {
   public static Node root;
   public BinarySearchTree(){
       this.root = null;
   }
  
   public void insert(int id){
       Node newNode = new Node(id);
       if(root==null){
           root = newNode;
           return;
       }
       Node current = root;
       Node parent = null;
       while(true){
           parent = current;
           if(id<current.data){              
               current = current.left;
               if(current==null){
                   parent.left = newNode;
                   return;
               }
           }else{
               current = current.right;
               if(current==null){
                   parent.right = newNode;
                   return;
               }
           }
       }
   }
   public void preorder(Node root){
       if(root!=null){
           System.out.print(" " + root.data);
           preorder(root.left);
           preorder(root.right);
       }
   }
   public int getNodeHeight(Node root, int x, int height){
       if(root==null) return 0;
       if(root.data==x) return height;
      
       //check if the node is present in the left sub tree
       int level = getNodeHeight(root.left,x,height+1);
       //System.out.println(level);
       if(level!=0) return level;
      
       //check if the node is present in the right sub tree
       return getNodeHeight(root.right,x,height+1);
   }
   public int balancefactor(int x){
       if(root==null) return 0;
       return getNodeHeight(root.left,x,0)-getNodeHeight(root.right,x,0);
   }
   public static void main(String arg[]){
       BinarySearchTree b = new BinarySearchTree();
       b.insert(5);b.insert(85);
       b.insert(89);b.insert(3);b.insert(2);b.insert(8);b.insert(65);b.insert(92);
       System.out.println("Preorder Tree Traversal: ");
       b.preorder(b.root);      
       System.out.println("");
       System.out.println("Node Height balancefactor");
       System.out.println("5     "+b.getNodeHeight(root,5,0)+"   "+b.balancefactor(5));
       System.out.println("85    "+b.getNodeHeight(root,85,0)+" "+b.balancefactor(85));
       System.out.println("89    "+b.getNodeHeight(root,89,0)+" "+b.balancefactor(89));
       System.out.println("3     "+b.getNodeHeight(root,3,0)+"   "+b.balancefactor(3));
       System.out.println("2     "+b.getNodeHeight(root,2,0)+"   "+b.balancefactor(2));
       System.out.println("8     "+b.getNodeHeight(root,8,0)+" "+b.balancefactor(8));
       System.out.println("65    "+b.getNodeHeight(root,65,0)+" "+b.balancefactor(65));
       System.out.println("92    "+b.getNodeHeight(root,92,0)+" "+b.balancefactor(92));
   }
}
class Node{
   int data;
   Node left;
   Node right;  
   public Node(int data){
       this.data = data;
       left = null;
       right = null;
   }
}

Add a comment
Know the answer?
Add Answer to:
Needed in Java Code Create a Binary Search Tree with the following elements in the order...
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
  • PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a...

    PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a class called MyTree with the methods __init__(x), getLeft(), getRight(), getData(), insert(x) and getHeight(). Each child should itself be a MyTree object. The height of a leaf node should be zero. The insert(x) method should return the node that occupies the original node's position in the tree. Create a class called MyBST that extends MyTree. Override the method insert(x) to meet the definitions of a...

  • Insert the following values in the given order into a Binary Search Tree and use the...

    Insert the following values in the given order into a Binary Search Tree and use the resulting BST in the next 5 questions. 15 8 3 6 23 9 11 10 20 13 5 9. What is the height of the resulting Binary Search Tree? 10. What is the depth of the node that stores the value 11? 11. Is there a path from the node storing the value 15 to the node storing the value 5? If so, show...

  • Create a new Java Application that has the following methods: A method that generate a binary...

    Create a new Java Application that has the following methods: A method that generate a binary search tree (BST) where the number of nodes and the range of the values are from a file. A recursive method to print the BST in preorder traversal A recursive method to print the BST in post-order traversal A recursive method to print the BST in in-order traversal A recursive method to count the number of all nodes in BST A method to find...

  • Write a program in C fro Binary Search tree using following functions 1. Insertion operation using...

    Write a program in C fro Binary Search tree using following functions 1. Insertion operation using recursion 2. Deletion operation 3. Minimum/Maximum of a BST 6. Reorganize the tree so that the tree height is minimum 7. Print all the nodes from the node to the path to the root 8. Find the lowest common shared node between two given nodes

  • Draw the tree resulting from inserting the following values into a binary search tree in order...

    Draw the tree resulting from inserting the following values into a binary search tree in order without re-balancing: 40, 10, 60, 30, 20, 90, 70, 50 Null pointers can be omitted as long as it is clear whether a single child is a left or right child. THEN For every node in the tree, the values that can be in the subtree rooted at that node are constrained by ancestors to be in some range of integers. The root (the...

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

  • help 2. Do the following problems: Create a binary search tree (BST), with the following words...

    help 2. Do the following problems: Create a binary search tree (BST), with the following words inserted: Int, Char, Return, Break, Float, While, Short, Sort, Double, For, Continue. a. b. Insert the following words into the BST built in (a): Tree, Table, Binary, Network, Visit, Seekk, Traversal c. Where is the minimum key value in a BST? (Give a concrete example) d. Where is the maximum key value in a BST? (Give a concrete example) e. How many comparisons are...

  • Beginning with an empty binary search tree, what binary search tree is formed when you add the following letters in the order given?

    Trees-related questionsBeginning with an empty binary search tree, what binary search tree is formed when you add the following letters in the order given? J, N, B, A, W, E, TRepresent the following binary tree with an array  What is the result of adding 3 and 4 to the 2-3 tree shown below?Why does a node in a red-black tree require less memory than a node in a 2-3-4 tree?Why can’t a Red-Black Tree have a black child node with exactly...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

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