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
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...
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...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
PYTHON CODING 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...
In this assignment you will create a Binary Search Tree to storeand retrieve objects of...
In this assignment you will create a Binary Search Tree to store and retrieve objects of type ItemType. The purpose of this assignment is for you to become familiar with basic tree operations, and understand the efficiency of trees compared to previously studied data structures. Binary Tree nodes have only two children, left and right. Nodes are compared based on their Key instance variable, which in this assignment is of type ItemType. All elements in the left subtree of a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT