Question: Please help. Thank you! My code for the BinarySearchTree is below class BinarySearchTree { class Node { int key; Node left, right; public Node(int item)

Please help. Thank you!

Please help. Thank you! My code for the BinarySearchTree is below class

My code for the BinarySearchTree is below

class BinarySearchTree {

class Node {

int key;

Node left, right;

public Node(int item) {

key = item;

left = right = null;

} }

// Root of BST

Node root;

// Constructor

BinarySearchTree() {

root = null;

}

// This method mainly calls insertRec()

void insert(int key) {

root = insertRec(root, key);

}

/* A recursive function to insert a new key in BST */

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.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 calls InorderRec()

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 Program to test above functions

public static void main(String[] args) {

BinarySearchTree tree = new BinarySearchTree();

tree.insert(50);

tree.insert(30);

tree.insert(20);

tree.insert(40);

tree.insert(70);

tree.insert(60);

tree.insert(80);

// print inorder traversal of the BST

tree.inorder(); } }

RSTNodw class to tom class RFNod olor field value field to each node (let field he narysearuh the ReTre ude the fallon lement Left Rota d Right R hads RRNacka class. Len-Ratate algorithm is given below Following that algorithm, devise the Right. Rotate algorithm and implement LEFT-ROTATE (T set y ight btree intox's right link s parent elseif p left put x on y's left implement ede Red Buck Tr the insert method. arysearchTree algorithm below). In class implement the case for which parent of r is the len hild of ge CS33 Lab Assigt RB-INSERT(T. Troor while nil else x x right elseif z.key y.key else y.rigir z z, left z, color RED RB-INSERT-FIXUPOT, z) while z p, color RED p.p. left z.p.p right if y color case y color BLACK case 2.P.p color RED case case P.P else if z,p, right LEFT-ROTATE case 2 2.p color BLACK z.p.p. color RED RIGHT-ROTATEIT.z.p.p) case 3 else (same as then clause with "right" and "left" exchanged Traor.color CS303 Lab Assigement 4, Write RBTren class. Make

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!