Complete the redblacktree in Java.
Make every Node object have a false isBlack field, allnew node is red by default.
In the end of the insert method, set the root node ofyour red black tree to be black.
Implement the rotate() and recolor() functions, andcreate tests for them in a separate class.
Tests, start with this tree in level order 44 (black) 25(red) 72 (black), this is invalid tree. Make it valid afterinserting 17.
import java.util.LinkedList;
public class BinarySearchTree> {
protected static class Node {
public T data;
public Node parent; // null for root node
public Node leftChild;
public Node rightChild;
public boolean isBlack;
public Node(T data) {
this.data = data;
}
public boolean isLeftChild() {
return parent != null && parent.leftChild == this;
}
@Override
public String toString() { // display subtree in ordertraversal
String output = \"[\";
LinkedList> q = new LinkedList<>();
q.add(this);
while (!q.isEmpty()) {
Node next = q.removeFirst();
if (next.leftChild != null)
q.add(next.leftChild);
if (next.rightChild != null)
q.add(next.rightChild);
output += next.data.toString();
if (!q.isEmpty())
output += \", \";
}
return output + \"]\";
}
}
protected Node root; // reference to root node of tree,null when empty
public void insert(T data) throws NullPointerException,IllegalArgumentException {
// null references cannot be stored within this tree
if (data == null)
throw new NullPointerException(\"This RedBlackTree cannot store nullreferences.\");
Node newNode = new Node<>(data);
if (root == null) {
root = newNode;
} // add first node to an empty tree
else
insertHelper(newNode, root); // recursively insert intosubtree
}
private void insertHelper(Node newNode, Nodesubtree) {
int compare = newNode.data.compareTo(subtree.data);
// do not allow duplicate values to be stored within thistree
if (compare == 0)
throw new IllegalArgumentException(\"This RedBlackTree alreadycontains that value.\");
// store newNode within left subtree of subtree
else if (compare < 0) {
if (subtree.leftChild == null) { // left subtree empty, addhere
subtree.leftChild = newNode;
newNode.parent = subtree;
// otherwise continue recursive search for location to insert
} else
insertHelper(newNode, subtree.leftChild);
}
// store newNode within the right subtree of subtree
else {
if (subtree.rightChild == null) { // right subtree empty, addhere
subtree.rightChild = newNode;
newNode.parent = subtree;
// otherwise continue recursive search for location to insert
} else
insertHelper(newNode, subtree.rightChild);
}
}
@Override
public String toString() {
return root.toString();
}
/**
* Performs the rotation operation on the provided nodes within thisBST. When the provided child
* is a leftChild of the provided parent, this method will perform aright rotation (sometimes
* called a left-right rotation). When the provided child is arightChild of the provided parent,
* this method will perform a left rotation (sometimes called aright-left rotation). When the
* provided nodes are not related in one of these ways, this methodwill throw an
* IllegalArgumentException.
*/
private void rotate(Node child, Node parent)throws IllegalArgumentException {
// TODO: Implement this method.
}
/**
* recolor() takes a reference to a newly added red node as its onlyparameter. This method may
* also be called recursively, in which case the input parameter mayreference a different red
* node in the tree that potentially has a red parent node. The jobof this method is to resolve
* red child under red parent red black tree property violationsthat are introduced by inserting
* new nodes into a red black tree. All other red black treeproperties must also be preserved.
* The method should be called from insertHelper after adding a newred node to the tree (in both
* the cases of adding this new node as a left child and as a rightchild). No further changes to
* the insertHelper method should be made.
*/
private void recolor(Node newNode) {
// TODO: Implement this method.
}
}