Question

In: Computer Science

Create a Binary Search Tree with the following elements in the order mentioned below: 5, 85,...

Create a Binary Search Tree with the following elements in the order mentioned below:

5, 85, 89, 3, 2, 8, 65, 92


Print the Pre-order of this tree

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.


Typed JAVA Program!
Java language

Solutions

Expert Solution

The Code for the following question is :

import java.util.Scanner;

//Creating class for the node
class Node{
       int data;
       Node left,right;
      
      Node(int data){
         this.data = data;
         left = right = null;
     }
}

public class BstBalanceFactor {

    Node root;

    // Constructor
    BstBalanceFactor() {
        root = null;
    }

    // This method mainly calls insertRec()
    void insert(int key) {
        root = insertRec(root, key);
    }

    /* A recursive function to insert a new key in BST */
    Node insertRec(Node root, int key) {

        /* If the tree is empty, return a new node */
        if (root == null) {
            root = new Node(key);
            return root;
        }

        /* Otherwise, recur down the tree */
        if (key < root.data)
            root.left = insertRec(root.left, key);
        else if (key > root.data)
            root.right = insertRec(root.right, key);

        /* return the (unchanged) node pointer */
        return root;
    }

    // This method mainly calls PreorderRec()
    void Preorder()  {
        PreorderRec(root);
    }
    void PreorderRec(Node node)
    {
        if (node == null)
            return;

        /* first print data of node */
        System.out.print(node.data + " ");


        PreorderRec(node.left);


        PreorderRec(node.right);
    }


    public int getHeight(Node n){
        if(n == null) {
            return -1;
        }

        return Math.max(getHeight(n.left), getHeight(n.right))+1;
    }
    // Find the BalanceFactor of a Node
    public int BalanceFactor( Node node){
         int balanceFactor = 0;
         if (node == null)
            return 0;

        if (node.left == null && node.right == null)
            return 1;

        int heightL = BalanceFactor(node.left);
        int heightR = BalanceFactor(node.right);


        balanceFactor = heightL - heightR;
        return balanceFactor;

    }

    //Printing the output
    public  void print(){
        printRec(root);
    }

    //Recursive call to print()
    public  void  printRec(Node node){

        if(node != null) {
            System.out.println(node.data + " " + getHeight(node) + " " + BalanceFactor(node));

            printRec(node.left);
            printRec(node.right);
        }

    }
    public static void main(String args[]){
        BstBalanceFactor tree = new BstBalanceFactor();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int array[] = new int[n];
        for (int i = 0; i <n ; i++) {
            array[i] = sc.nextInt();
        }

        for (int i = 0; i <n ; i++) {
            tree.insert(array[i]);
            //tree.print();
        }

        tree.Preorder();
        System.out.println();
        System.out.println(" Node " + "Height " + "Balance Factor " );
        tree.print();


    }
}

/*****************************************************************************************************************************************/

The screenshot of the output is attached here

"C:Program Files\Java dk1.8.0 191\bin Gava.exe". 5 85 89 3 2 8 65 92 5 3 2 85 8 65 89 92 Node Height Balance Factor 5 3 1 3 1 1 201 85 20 8 1-1 65 0 1 89 1 -1 92 0 1 Process finished with exit code 0


Related Solutions

JAVA PROGRAM Create a Binary Search Tree with the following elements in the order mentioned below:...
JAVA PROGRAM Create a Binary Search Tree with the following elements in the order mentioned below: 5, 85, 89, 3, 2, 8, 65, 92 Print the Pre-order of this tree 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...
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
C++ Instantiate a binary search tree object and create such tree using elements of the sequence...
C++ Instantiate a binary search tree object and create such tree using elements of the sequence 8,3,10, 1,6,9, 14, 4,7, 13 with 8 as root of the tree. Find maximum and minimum elements of the tree, successor(10) and predecessor(13), print the inorder, postorder and preorder traversal of the tree.
Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal...
Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal of the tree. 50, 60, 25, 40, 30, 70, 35, 10, 55, 65, 5 Write an algorithm to delete a node in Singly Linked List                            [12 Write an algorithm of Binary Search                                                              [10] Write a program in ‘C’ to generate Fibonacci series using recursion            [8]
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
Write the binary tree representation for the Binary Search for also 17 elements and give the...
Write the binary tree representation for the Binary Search for also 17 elements and give the worst-case
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
Build a binary search tree with the following words. Insert them in an order so that...
Build a binary search tree with the following words. Insert them in an order so that the tree has as small a depth as possible. (Consider the insertion order of the words) Print the tree after,also, any, back, because, come, day, even, first, give, how, its, look, most, new, now, only, other, our, over, than, then, these, think, two, us, use, want, way, well, work. C++
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Question 2: Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
Question 2: Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT