Question

In: Computer Science

Part II: Create a random binary tree and verify BST order property and AVL balance condition....

Part II: Create a random binary tree and verify BST order property and AVL balance condition. Report the problems. You don’t need to fix anything. Also, do not hard code the tree inside the code to force the creation of order.

5-10 items in random binary tree

code in java

Solutions

Expert Solution

mport java.util.*;

import java.io.*;

public class Main

{

static class Node{

int key;

Node left,right;

public Node(int key){

this.key = key;

left = null;

right = null;

}

}

public static void insert(Node root,int key){

Random rand = new Random();

int num = rand.nextInt(2);

if (num==0){ //insert in left sub tree with 0.5 probability

if (root.left!=null){

insert(root.left,key);

return;

}

else{

Node temp = new Node(key);

root.left = temp;

return;

}

}

else{ //insert in right sub tree with 0.5 probability

if (root.right!=null){

insert(root.right,key);

return;

}

else{

Node temp = new Node(key);

root.right = temp;

return;

}

}

}

/*Check if max key in left subtree is less than current key and min key in right subtree is greater than current key*/

static boolean checkBSTOrder(Node root) {

return isBST(root, Integer.MIN_VALUE,

Integer.MAX_VALUE);

}

/* Returns true if the given tree is a BST*/

static boolean isBST(Node node, int min, int max)

{

/ an empty tree is BST /

if (node == null)

return true;

/ false if this node violates the min/max constraints /

if (node.key < min || node.key > max)

return false;

/* otherwise check the subtrees recursively

tightening the min/max constraints */

// Allow only distinct values

return (isBST(node.left, min, node.key-1) &&

isBST(node.right, node.key+1, max));

}

static class Height{

int height=0;

}

static int height(Node node)

{

/ base case tree is empty /

if (node == null)

return 0;

/* If tree is not empty then height = 1 + max of left

height and right heights */

return 1 + Math.max(height(node.left), height(node.right));

}

static boolean checkAVLProperty(Node root,Height height){

/ If tree is empty then return true /

if (root == null)

{

height.height = 0;

return true;

}

/ Get heights of left and right sub trees /

Height lheight = new Height();

Height rheight = new Height();

boolean l = checkAVLProperty(root.left, lheight);

boolean r = checkAVLProperty(root.right, rheight);

int lh = lheight.height;

int rh = rheight.height;

/* Height of current node is max of heights of

left and right subtrees plus 1*/

height.height = (lh > rh? lh: rh) + 1;

/* If difference between heights of left and right

subtrees is more than 2 then this node is not balanced

so return 0 */

if (Math.abs(lh - rh)>=2)

return false;

/* If this node is balanced and left and right subtrees

are balanced then return true */

else

return l && r;

}

public static void main(String[] args) {

Random rand = new Random();

int num = rand.nextInt(50);

Node root = new Node(num);

int array[] = new int[50]; //make an array of length 50 to check whether the random number generated is unique or has been generated earlier

array[num]=1;

for (int i=0;i<10;i++){ //insert 10 more keys in the BST

while (true){ //generate a random number that has not been geerated earlier

num = rand.nextInt(50);

if (array[num]==0){

array[num] = 1; //mark that this number is generated now

break;

}

}

insert(root,num);

}

if (checkBSTOrder(root)){

System.out.println("It is following BST order property.");

}

else{

System.out.println("It is not following BST order property.");

}

Height height = new Height();

if (checkAVLProperty(root,height)){

System.out.println("It is following AVL balance condition.");

}

else{

System.out.println("It is not following AVL balance condition.");

}

}

}


Related Solutions

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.
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
• This assignment calls for generating a Binary Search Tree (BST) and scanning it in a...
• This assignment calls for generating a Binary Search Tree (BST) and scanning it in a preorder and a Breadth First Search (BFS) way. • The program gets a parameter ? from the command line and stores ? randomly generated integers in a dynamic array. To generate a random integer between 1 and Max-Rand (a system constant): ▪ Use srand() to seed the rand() function. Then, call rand() again and again (? times) to generate ? random integers. Next, printout...
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print...
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print out the data value for each node it encounters in the insert operation. Remember that the module AVL tree code gets part of its functionality from the BST type, since an AVL tree is a binary search tree, but adds some additional functionality. 2. Add code to the main method to meet the following requirements: (a) Create an AVL tree to hold names stored...
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]
What is a binary search tree or BST? Discuss its characteristics. What are other types of...
What is a binary search tree or BST? Discuss its characteristics. What are other types of binary trees as well?
I was trying to implement a simple binary search tree using this given class of bst...
I was trying to implement a simple binary search tree using this given class of bst in c++ public: BST(); ~BST(); void insertKey(int newKey); bool hasKey(int searchKey); std::vector<int> inOrder(); int getHeight(); however; i am still required to use another class for the nodes as a pointer and i need to manage memory leak. in main we should ask for the numbers we need to insert in the binary search tree and also let the user end it with a letter...
Create a (partial) BST class and a driver program to test it. The tree node will...
Create a (partial) BST class and a driver program to test it. The tree node will store integers as the data/key field (single field). Note that you will need to guarantee there are no duplicates in your insert function (the tree should refuse to insert a duplicate key). Call your files “tree.h”, “tree.cpp” and “main.cpp”. In addition, draw a picture of your tree (see note about random values below) Public methods to include: Constructor Copy Constructor Overloaded Assignment Operator Destructor...
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...
Show how to augment the ordinary Binary Search Tree (BST) data structure so that it supports...
Show how to augment the ordinary Binary Search Tree (BST) data structure so that it supports an efficient procedure which, on input (x, k) where x is the root of a BST and k an integer, output the k-th smallest number store in the BST. Let n denote the total number of elements stored in the BST, what is the running time of your efficient procedure? How does your modification of the BST data structure affect the performance of other...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT