Question

In: Computer Science

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

Node Height Balance Factor

2          0

65    0

92    0

8          1

89        1

85         2

3           1

5           3

Solutions

Expert Solution

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 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;
                                        }
                                }
                        }
                }
        }
        
        public void printReport() {
                System.out.printf("%-5s%-8s%-5s\n", "Key", "Height", "Balance Factor");
                printReport(root, 1);
        }
        
        private void printReport(TreeNode start, int height) {
                if(start == null) {
                        return;
                }
                System.out.printf("%-5d%-8d%-5d\n", start.key, height, balanceFactor(start));
                printReport(start.left, height + 1);
                printReport(start.right, height + 1);
        }
        private int depth(TreeNode start) {
                if(start == null) {
                        return 0;
                }
                return 1 + Math.max(depth(start.left), depth(start.right));
        }
        private int balanceFactor(TreeNode start) {
                return depth(start.left) - depth(start.right);
        }
        

        /**
         * @param args
         */
        public static void main(String[] args) {
                int[] array = { 5, 85, 89, 3, 2, 8, 65, 92 };
                BinarySearchTree bst = new BinarySearchTree();
                for (int i = 0; i < array.length; i++)
                        bst.insert(array[i]);
                
                bst.printReport();
        }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

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]
Problem 1: Write a Java program for traversing a Binary Search Tree in following ways: (use...
Problem 1: Write a Java program for traversing a Binary Search Tree in following ways: (use any data structure, library functions) ---------- 20 points i) Depth First Traversal: Inorder, LVR ii) Depth First Traversal: Inorder, RVL iii) Depth First Traversal: Preorder, VLR iv) Depth First Traversal: Preorder, VRL v) Depth First Traversal: Postorder, LRV vi) Depth First Traversal: Postorder, RLV No choice menu required. Sample Input (taken from keyboard, single space separated nodes as found in a complete BFS: top-down,...
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
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
In java, create a binary search tree (without using additional libraries) of stacks where each node...
In java, create a binary search tree (without using additional libraries) of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key,...
There are plenty of examples of Binary Search Tree classes written in Java available on the...
There are plenty of examples of Binary Search Tree classes written in Java available on the Internet. Find one. Either download or copy and paste the code into an appropriately named Java class file. Compile that file. Then, create the class BinarySrcTree, which will inherit from the class you downloaded from the Internet. Place a comment in the class that states where you obtained your base class (URL or Web page or site name). You will then overwrite or overload...
​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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT