Question

In: Computer Science

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 to create the following methods:

 leftRotate(Node n) : performs a single left rotation on the specified Node

 fullLeft(Node n): performs a full left rotation on the specified Node

 rightRotate(Node n) : performs a single right rotation on the specified Node

 fullRight(Node n): performs a full right rotation on the specifid Node

 int balance() : returns whether a tree is balanced or not. If it is balanced, it returns a 0. If the left subtree is unbalanced, it returns a negative value, and the actual value will be the node number (counting from the root) of the unbalanced node. If the right subtree is unbalanced, it returns a positive value, calculated the same way.

Solutions

Expert Solution

//JAVA CODE TO COPY//


import java.util.Scanner;

class Node
{
  
Node left, right;
int data;
int height;

public Node root;
public Node()
{
left = null;
right = null;
data = 0;
height = 0;
}

public Node(int n)
{

left = null;
right = null;
data = n;
height = 0;
}
  
public void inorder()
{
inorder(root);
}

private void inorder(Node r)
{
if (r != null)
{
inorder(r.left);
System.out.print(r.data + " ");
inorder(r.right);
}
}

public void preorder()
{

preorder(root);
}

private void preorder(Node r)
{
if (r != null)
{
System.out.print(r.data + " ");
preorder(r.left);
preorder(r.right);
}
}

public void postorder()
{
postorder(root);
}

private void postorder(Node r)
{
if (r != null)
{
postorder(r.left);
postorder(r.right);
System.out.print(r.data + " ");
}
}
  
  
}

//BinarySrcTree class inherited from Node
class BinarySrcTree extends Node
{
  

public BinarySrcTree()
{
root = null;
}
  
private int height(Node t)
{

return t == null ? -1 : t.height;
}

private int max(int lhs, int rhs)
{
return lhs > rhs ? lhs : rhs;
}

void insert(int key) {
root = insertRec(root, key);
}
  
/* A recursive function to insert a new key in Node */
Node insertRec(Node root, int key) {
  
/* If the tree is empty, return a new node */
if (root == null) {
root = new Node(key);
return root;
}
  
/* Otherwise, recur down the tree */
if (key < root.data)
root.left = insertRec(root.left, key);
else if (key > root.data)
root.right = insertRec(root.right, key);
  
/* return the (unchanged) node pointer */
return root;
}

//method leftRotate
Node leftRotate(Node k2)
{
System.out.println("Left Rotation Performed");
Node k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = max(height(k2.left), height(k2.right)) + 1;
k1.height = max(height(k1.left), k2.height) + 1;
return k1;
}

//method rightRotate
Node rightRotate(Node k1)
{
//System.out.println("Right Rotation Performed");
Node k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
k1.height = max(height(k1.left), height(k1.right)) + 1;
k2.height = max(height(k2.right), k1.height) + 1;
return k2;
}

//method fullLeftRotate
Node fullLeftRotate(Node k3)
{
System.out.println("Left Rotation Performed");
k3.left = rightRotate(k3.left);
return leftRotate(k3);
}

//method fullRightRotate
private Node fullRightRotate(Node k1)
{
//System.out.println("Right Rotation Performed");
k1.right = leftRotate(k1.right);
return rightRotate(k1);
}

/* Returns 0 if binary tree with root as root is height-balanced */
int balance(Node node)
{
int lh; /* for height of left subtree */
  
int rh; /* for height of right subtree */
  
/* If tree is empty then return 0 */
if (node == null)
return 0;
  
/* Get the height of left and right sub trees */
lh = Nodeheight(node.left);
rh = Nodeheight(node.right);
  
if (Math.abs(lh - rh) <= 1
&& balance(node.left)==0
&& balance(node.right)==0)
return 0;
  
/* If we reach here then tree is not height-balanced */
if(lh>1)
return -lh;
else
return rh;
}
  
/* UTILITY FUNCTIONS TO TEST isBalanced() FUNCTION */
/* The function Compute the "height" of a tree. Height is the
number of nodes along the longest path from the root node
down to the farthest leaf node.*/
int Nodeheight(Node n)
{
/* base case tree is empty */
if (n == null)
return 0;
  
/* If tree is not empty then height = 1 + max of left
height and right heights */
return 1 + Math.max(Nodeheight(n.left), Nodeheight(n.right));
}
  
  
  
}

//test class
class Test
{
//main driver method
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);

BinarySrcTree sbbst = new BinarySrcTree();
System.out.println("Node Testing\n");

System.out.println("Insert first 5 Elements");
int N = 5;
for (int i = 0; i < N; i++)
{
System.out.println("\nEnter a Number(integer): ");
sbbst.insert(scan.nextInt());

System.out.println("\nPre-order :");
sbbst.preorder();
System.out.println("\nIn-order :");
sbbst.inorder();
System.out.println("\nPost-order :");
sbbst.postorder();

System.out.println();
}
  
int res = sbbst.balance(sbbst.root);
if(res==0)
System.out.println("\nBalance Tree.");
if(res<0)
System.out.println("\nNot a Balance Tree\nLeft Unbalance node height: "+res);
if(res>0)
System.out.println("\nNot a Balance Tree\nRight Unbalance node height: "+res);
System.out.println("\n\nLeft Rotate: "+sbbst.leftRotate(sbbst.root));
sbbst.preorder();
System.out.println("\n\nRight Rotate: "+sbbst.rightRotate(sbbst.root));
sbbst.preorder();
}
}

//PROGRAM OUTPUTS//

//COMMENT DOWN FOR ANY QUERIES...
//HIT A THUMBS UP IF YOU DO LIKE IT!!!


Related Solutions

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
​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.
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
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
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...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key...
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key and then takes it out of the tree. The program can be C++/Java or C-style pseudocode. Do not use function call to either Min or delete done in the class. Write this from scratch. (No need to ensure AVL properties, just show it for a generic BST) Node * extract min(Node * x) { }
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,...
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 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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT