Question

In: Computer Science

Create at least 25 random ISBN keys and insert all random ISBN keys into a Binary...

Create at least 25 random ISBN keys and insert all random ISBN keys into a Binary Tree. Write a function to determine if the Binary Tree is a BST or not a BST. In addition validate AVL balance condition.

Hint: Should you proceed to validate AVL if the binary tree did not produce BST?

Report the problems. You don’t need to fix anything. Also, do not hard code the tree inside the code.

This needs to be done in Java.

Solutions

Expert Solution

//JAVA CODE TO COPY//


import java.util.Random;

//tree node class
class TreeNode
{
    int            value;
    TreeNode Left;
    TreeNode Right;

    TreeNode(int k)
    {
        value = k;
    }
}

class BST_AVL
{
    //checking if tree is BST
    //if its not an bst then it cant be AVL tree
    public static boolean isBST(TreeNode node)
    {
        return (isBSTUtil(node, 0, 100));
    }

    public static boolean isBSTUtil(TreeNode node, int min, int max)
    {

        if (node == null)
            return true;

        if (node.value < min || node.value > max)
            return false;

        return (isBSTUtil(node.Left, min, node.value - 1) && isBSTUtil(
                node.Right, node.value + 1, max));
    }

    public static boolean isBalanced(TreeNode root)
    {
        int left_side;
        int right_side;

        if (root == null)
            return true;

        left_side = height(root.Left);
        right_side = height(root.Right);

        if (Math.abs(left_side - right_side) <= 1 && isBalanced(root.Left)
                && isBalanced(root.Right))
            return true;

        return false;
    }

    public static int max(int a, int b)
    {
        return (a >= b) ? a : b;
    }

    public static int height(TreeNode node)
    {
        if (node == null)
            return 0;

        return 1 + max(height(node.Left), height(node.Right));
    }

    //main driver method
    public static void main(String args[])
    {
        //array to hold random ISBN keys
        int isbn[]=new int[25];

        //Random class object
        Random rand = new Random();
        //to hold random isbn
        int num;
        //generating 25 random ISBN keys
        for(int i=0; i<isbn.length; i++)
        {
            num = rand.nextInt(1000000000);
            isbn[i] = num;
        }
        //inserting data(ISBN) for two different BST
        TreeNode t1 = new TreeNode(isbn[0]);
        t1.Left = new TreeNode(isbn[1]);
        t1.Right = new TreeNode(isbn[2]);
        t1.Right.Left = new TreeNode(isbn[3]);
        t1.Right.Right = new TreeNode(isbn[4]);

        TreeNode t2 = new TreeNode(isbn[5]);
        t2.Left = new TreeNode(isbn[6]);
        t2.Right = new TreeNode(isbn[7]);
        t2.Right.Left = new TreeNode(isbn[8]);
        t2.Right.Right = new TreeNode(isbn[9]);
        t2.Left.Left = new TreeNode(isbn[10]);
        t2.Left.Right = new TreeNode(isbn[11]);

        //checking if bst is avl or not
        if (isBST(t1) && isBalanced(t1))
            System.out.println("Tree t1 is AVL tree");
        else
            System.out.println("Tree t1 is not AVL tree");

        if (isBST(t2) && isBalanced(t2))
            System.out.println("Tree t1 is AVL tree");
        else
            System.out.println("Tree t1 is not AVL tree");
    }
}

//OUTPUT//

Comment down for any queries!
Please give a thumbs up if you are satisfied and helped with the answer :)


Related Solutions

python Create a dictionary and insert several English words as keys and the Pig Latin (or...
python Create a dictionary and insert several English words as keys and the Pig Latin (or any other language) translations as values. Write a function called bonus that takes as a parameter a dictionary that has names as keys and salaries as values. Your function should increase everyone’s salary in the dictionary by 5%. Write a function called updateAge that takes as parameters a list of names of people whose birthday it is today, and a dictionary that has names...
Create a Word document and title it “College Expenses”. In the Word document, insert a table with at least 5 rows and 5 columns. Insert>Table.
Assignment 3 – Incorporating a Table into a Document.Create a Word document and title it “College Expenses”. In the Word document, insert a table with at least 5 rows and 5 columns. Insert>Table.Tell me about your college expenses you have by filling this table with subjects and data. Then write two paragraphs telling me about the information you provided in the table. Bold and color table heading.  Example of table:College ExpensesTuitionBooksComputer/InternetOther suppliesScience ClassMath classC.I.S. ClassEnglish ClassGive the page a proper title....
a) The keys 20, 15, 25, 12 and 18 are inserted (totally 5 keys, in that...
a) The keys 20, 15, 25, 12 and 18 are inserted (totally 5 keys, in that order) into an initially empty AVL tree. Show the final AVL tree (just 1 tree) after all the insertions. b) Delete the root node from the final tree in a). Take the root’s successor (we used predecessor in project 2) as the replacement. Show the AVL tree after the deletion. c) Delete the key 12 from the resulted tree in b). Show the AVL...
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table as...
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table as if it were being added to MySQL (please give explanations for each line of SQL code and a copy of the code as it would be entered into the query by itself: Customer PK Customer ID Text Phone Number int name text address ID int email text FK vendor ID int Vendor is the name of the table the FK comes from.
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table: Customer...
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table: Customer PK Customer ID Text Phone Number int name text address ID int email text FK vendor ID int Vendor is the name of the table the FK comes from.
Create a Database Schema for a hotel reservation system. indicate the Primary Keys, Foreign Keys, and...
Create a Database Schema for a hotel reservation system. indicate the Primary Keys, Foreign Keys, and the one-to-one or one-to-many relationships between the tables. Also describe in a small paragraph the scope of the back-end database, by explaining the different tables and their relations in your schema.
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order...
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order traversal, preorder traversal, and find. Please separate the code in the four parts and explain in detail what is happening. Also, if you can please basic C language. If not, then I understand. Thank you for your time. The test cases are 'm', 'd', 'g', 'r', 'p', 'b', and 'x'. Output: Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: i Enter...
/** * This class implements a basic Binary Search Tree that supports insert and get operations....
/** * This class implements a basic Binary Search Tree that supports insert and get operations. In * addition, the implementation supports traversing the tree in pre-, post-, and in-order. Each node * of the tree stores a key, value pair. * * @param <K> The key type for this tree. Instances of this type are used to look up key, value * pairs in the tree. * @param <V> The value type for this tree. An instance of this...
Create a random binary tree and verify BST property and AVL balance condition. Do not hard...
Create a random binary tree and verify BST property and AVL balance condition. Do not hard code the tree inside the code. Java or C++ are the options.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT