//
// LLRB — L(eft)-L(eaning) R(ed)-B(lack) BST
//
// This class stores a set of integer keys using a left-leaning red-black BST
//
// HOMEWORK in this file is to implement:
//
// 1) public void insert()
// 2) public boolean containsRightRedEdge()
// 3) public boolean containsConsecutiveLeftRedEdges()
// 4) public int countBlackEdgesOnLeftmostPath()
// 5) public boolean sameBlackEdgesCountOnAllPaths(int count)
//
// As BONUS, there is one additional method to implement
//
// 1) public void fixLLRB()
//
package hw4;
public class LLRB {
private static final boolean RED = true;
private static final boolean BLACK = false;
public Node root;
public class Node {
public int key;
public boolean color;
public Node left, right;
public Node(int key, boolean color) {
this.key = key;
this.color = color;
}
}
// Constructor for LLRB
public LLRB() {
}
// Is parent link for node x red? false if x is null
private boolean isRed(Node x) {
if (x == null) return false;
return x.color == RED;
}
// Inserts a key without fixing the tree
public void bstInsert(int key) {
root = bstInsert(root, key);
}
// Recursive helper method for bstInsert
private Node bstInsert(Node x, int key) {
if (x == null) return new Node(key, RED);
if (key < x.key) x.left = bstInsert(x.left, key);
else if (key > x.key) x.right = bstInsert(x.right, key);
return x;
}
// Inserts a key fixing the red-black tree property
public void insert(int key) {
// TODO : complete this method
}
// Checks whether the tree contains a red right edge
public boolean containsRightRedEdge() {
// TODO : complete this method
return false;
}
// Checks whether the tree contains two left red edges in a row
public boolean containsConsecutiveLeftRedEdges() {
// TODO : complete this method
return false;
}
// Returns the maximum number of black edges (nodes) on any path from root to null
public int maxBlackEdgesDepth() {
// TODO : complete this method
return 0;
}
// Returns the minimum number of black edges (nodes) on any path from root to null
public int minBlackEdgesDepth() {
// TODO : complete this method
return 0;
}
// Checks whether the BST is a valid left leaning red-black tree
public boolean isValidLLRB() {
return (maxBlackEdgesDepth() == minBlackEdgesDepth() &&
!containsRightRedEdge() &&
!containsConsecutiveLeftRedEdges());
}
// Fixes the red-black tree if there is something to fix
public void fixLLRB() {
// TODO : complete this method
}
}
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more