|
|
@ -7,7 +7,7 @@ class RBTree<T extends Comparable<T>> { |
|
|
|
private static final boolean BLACK = false; |
|
|
|
|
|
|
|
private class Node { |
|
|
|
T key; |
|
|
|
T key; // Node<Integer>, generischer Datentyp, der Schlüssel (key) des Knotens vom Typ Integer ist. |
|
|
|
Node left, right, parent; // Elternknoten hinzugefügt |
|
|
|
boolean color; |
|
|
|
|
|
|
@ -28,7 +28,8 @@ class RBTree<T extends Comparable<T>> { |
|
|
|
Node node = root; |
|
|
|
Node parent = null; |
|
|
|
|
|
|
|
// Traverse the tree to the left or right depending on the key |
|
|
|
// Traverse the tree to the left or right depending on the key, |
|
|
|
// finding a correct postion for the insertion of nodes later + keep the structure of the tree |
|
|
|
while (node != null) { |
|
|
|
parent = node; |
|
|
|
if (key.compareTo(node.key) < 0) { // key < node.key |
|
|
@ -47,27 +48,14 @@ class RBTree<T extends Comparable<T>> { |
|
|
|
root = newNode; |
|
|
|
} else if (key.compareTo(parent.key) < 0) { // key < parent.key |
|
|
|
parent.left = newNode; |
|
|
|
} else { // key > parent.key |
|
|
|
} else if (key.compareTo(parent.key) > 0){ // key > parent.key |
|
|
|
parent.right = newNode; |
|
|
|
} |
|
|
|
newNode.parent = parent; |
|
|
|
fixColorafterInsert(newNode); |
|
|
|
newNode.parent = parent; // etablieren die Verbindung des Knotens zu seinem Vater |
|
|
|
fixAfterInsert(newNode); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* Fix any Red-Black tree violations |
|
|
|
* if (isRed(root.right) && !isRed(root.left)) { |
|
|
|
* root = rotateLeft(root); |
|
|
|
* } |
|
|
|
* if (isRed(root.left) && isRed(root.left.left)) { |
|
|
|
* root = rotateRight(root); |
|
|
|
* } |
|
|
|
* if (isRed(root.left) && isRed(root.right)) { |
|
|
|
* flipColors(root); |
|
|
|
* } |
|
|
|
*/ |
|
|
|
|
|
|
|
private Node getUncle(Node parent) { |
|
|
|
private Node getUncle(Node parent) { // für spätere Implementierung fix() |
|
|
|
Node grandparent = parent.parent; |
|
|
|
|
|
|
|
if (grandparent.left == parent) { |
|
|
@ -81,7 +69,7 @@ class RBTree<T extends Comparable<T>> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void fixColorafterInsert(Node node) { |
|
|
|
private void fixAfterInsert(Node node) { |
|
|
|
Node parent = node.parent; |
|
|
|
|
|
|
|
// Case 1: no root, add node as new root, root is always black |
|
|
@ -108,7 +96,7 @@ class RBTree<T extends Comparable<T>> { |
|
|
|
grandparent.color = true; |
|
|
|
|
|
|
|
// Call recursively for grandparent, which is now red. fix recursively |
|
|
|
fixColorafterInsert(grandparent); |
|
|
|
fixAfterInsert(grandparent); |
|
|
|
} |
|
|
|
|
|
|
|
// Parent is left child of grandparent |
|
|
@ -154,13 +142,6 @@ class RBTree<T extends Comparable<T>> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private boolean isRed(Node node) { |
|
|
|
if (node == null) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
return node.color == RED; |
|
|
|
} |
|
|
|
|
|
|
|
private Node rotateLeft(Node node) { |
|
|
|
Node rightChild = node.right; |
|
|
|
Node parent = node.parent; |
|
|
@ -255,6 +236,7 @@ class RBTree<T extends Comparable<T>> { |
|
|
|
*/ |
|
|
|
|
|
|
|
public void printDOTAfterInsert(String filename) { |
|
|
|
|
|
|
|
try (FileWriter writer = new FileWriter(filename)) { |
|
|
|
writer.write("digraph G {\n"); |
|
|
|
writer.write("\tnode [style=filled, color=black, shape=circle, width=.6,\n"); |
|
|
|