Code in java please, thank you
All the previous answers for this question are wrong please justdont copy and paste them. Thank you.
Create the following methods in java:
leftRotate(Node n) : performs a single left rotation on thespecified Node
fullLeft(Node n): performs a full left rotation on the specifiedNode
rightRotate(Node n) : performs a single right rotation on thespecified Node
fullRight(Node n): performs a full right rotation on thespecifid Node
int balance() : returns whether a tree is balanced or not. If itis balanced, it returns a 0. If the left subtree is unbalanced, itreturns a negative value, and the actual value will be the nodenumber (counting from the root) of the unbalanced node. If theright subtree is unbalanced, it returns a positive value,calculated the same way.
Here is my Binary Tree Code! and please feel free totest out the code and show the results of it after the methods arecreated please and thank you ill make sure to leave alike!
class BinarySearchTree {
/* Class containing left
and right child ofcurrent node
* and key value*/
class Node {
int key;
Node left,right;
public Node(intitem)
{
key= item;
left= right = null;
}
}
// Root of BST
Node root;
// Constructor
BinarySearchTree() { root = null; }
BinarySearchTree(int value) { root = newNode(value); }
// This method mainly callsinsertRec()
void insert(int key) { root =insertRec(root, key); }
/* A recursive function to
insert a new key inBST */
Node insertRec(Node root, int key)
{
/* If the treeis empty,
returna new node */
if (root ==null) {
root= new Node(key);
returnroot;
}
/* Otherwise,recur down the tree */
if (key <root.key)
root.left= insertRec(root.left, key);
else if (key> root.key)
root.right= insertRec(root.right, key);
/* return the(unchanged) node pointer */
return root;
}
// This method mainly callsInorderRec()
void inorder() { inorderRec(root); }
// A utility function to
// do inorder traversal of BST
void inorderRec(Node root)
{
if (root !=null) {
inorderRec(root.left);
System.out.println(root.key);
inorderRec(root.right);
}
}
// Driver Code
Code in java please, thank you All the previous answers for this question are wrong please just dont copy and paste them
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am