Question

In: Computer Science

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.

Solutions

Expert Solution

Ans:

A BST is a node based binary tree which has the following properties:
• The left subtree of a parent node contains only nodes which is less than the Parent node key.
• The right subtree of a parent node contains only nodes which is greater than the Parent node key.
• Both the left and right subtrees must be a binary search trees.

An AVL tree is a self-balancing BST.In an AVL tree, the heights of the child subtrees of any node differ by atmost one position; if at any time they differ by more than one position, rebalancing is needed to be done to obtain the AVL tree properties.

CODE:

//This is a java program to check whether a tree is AVL tree or not
class BSTAVLTreeNode
{
int value;
BSTAVLTreeNode Left;
BSTAVLTreeNode Right;

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

public class BST_AVL
{
public static boolean isBalanced(BSTAVLTreeNode root)
{
int Lh; /* left subtree height */
int Rh; /* right subtree height */

if (root == null)
return true;

Lh = height(root.Left);
Rh = height(root.Right);

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

return false;
}
public static boolean isBST(BSTAVLTreeNode node)
{
return (isBSTUtil(node, 0, 100));
}

public static boolean isBSTUtil(BSTAVLTreeNode 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 int height(BSTAVLTreeNode node)
{
if (node == null)
return 0;

return 1 + max(height(node.Left), height(node.Right));
}
  
public static int max(int a, int b)
{
return (a >= b) ? a : b;
}


public static void main(String args[])
{
BSTAVLTreeNode t1 = new BSTAVLTreeNode(3);
t1.Left = new BSTAVLTreeNode(5);
t1.Right = new BSTAVLTreeNode(6);
t1.Right.Left = new BSTAVLTreeNode(7);
t1.Right.Right = new BSTAVLTreeNode(8);

BSTAVLTreeNode t2 = new BSTAVLTreeNode(17);
t2.Left = new BSTAVLTreeNode(7);
t2.Right = new BSTAVLTreeNode(22);
t2.Right.Left = new BSTAVLTreeNode(20);
t2.Right.Right = new BSTAVLTreeNode(25);
t2.Left.Left = new BSTAVLTreeNode(6);
t2.Left.Right = new BSTAVLTreeNode(8);

if (isBST(t1) && isBalanced(t1))
System.out.println("t1 TREE is an AVL tree");
else
System.out.println("t1 TREE is not an AVL tree");

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

OUTPUT:


Related Solutions

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
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...
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...
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
In C++, Implement the heapafication operation. Do not re-implement the binary tree class. Simply create a...
In C++, Implement the heapafication operation. Do not re-implement the binary tree class. Simply create a funcntion that takes in a binary tree and heapafies it. Meaning it takes in a pointer to a binary tree and converts it into a heap. (You may choose max or min heap).
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]
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT