Complete the redblacktree in Java. Make every Node object have a false isBlack field, all new node...

60.1K

Verified Solution

Question

Programming

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.
}


}

Answer & Explanation Solved by verified expert
4.2 Ratings (582 Votes)
I have Written complete code just change the function name according to your need RedBlackBSTjava Below is the syntax highlighted version of RedBlackBSTjava from 33 Balanced Search Trees public class RedBlackBST private static final boolean RED true private static final boolean BLACK false private Node root root of the BST BST helper node data type private class Node private Key key key private Value val associated data private Node left right links to left and right subtrees private boolean color color of parent link private int size subtree count public NodeKey key Value val boolean color int size thiskey key thisval val thiscolor color thissize size Initializes an empty symbol table public RedBlackBST Node helper methods is node x red false if x is null private boolean isRedNode x if x null return false return xcolor RED number of node in subtree rooted at x 0 if x is null private int sizeNode x if x null return 0 return xsize Returns the number of keyvalue pairs in this symbol table return the number of keyvalue pairs in this symbol table public int size return sizeroot Is this symbol table empty return code true if this symbol table is empty and code false otherwise public boolean isEmpty return root null Standard BST search Returns the value associated with the given key param key the key return the value associated with the given key if the key is in the symbol table and code null if the key is not in the symbol table throws IllegalArgumentException if code key is code null public Value getKey key if key null throw new IllegalArgumentExceptionargument to get is null return getroot key value associated with the given key in subtree rooted at x null if no such key private Value getNode x Key key while x null int cmp keycompareToxkey if cmp 0 x xleft else if cmp 0 x xright else return xval return null Does this symbol table contain the given key param key the key return code true if this symbol table contains code key and code false otherwise throws IllegalArgumentException if code key is code null public boolean containsKey key return getkey null Redblack tree insertion Inserts the specified keyvalue pair into the symbol table overwriting the old value with the new value if the symbol table already contains the specified key Deletes the specified key and its associated value from this symbol table if the specified value is code null param key the key param val the value throws IllegalArgumentException if code key is code null public void putKey key Value val if key null throw new IllegalArgumentExceptionfirst argument to put is null if val null deletekey return root putroot key val rootcolor BLACK assert check insert the keyvalue pair in the subtree rooted at h private Node putNode h Key key Value val if h null return new Nodekey val RED 1 int cmp keycompareTohkey if cmp 0 hleft puthleft key val else if cmp 0 hright puthright key val else hval val fixup any rightleaning links if isRedhright isRedhleft h rotateLefth if    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students