diff --git a/TerryModi/Main.java b/TerryModi/Main.java index 4dc5dd9..e1b616c 100644 --- a/TerryModi/Main.java +++ b/TerryModi/Main.java @@ -17,7 +17,7 @@ import java.util.*; for (int i = 0; i < 15; i++) { - int pickedNumber = numbers.remove(0); // Entfernt das Element an dem "Index", return es und speichert es in 'pickedNumber' + int pickedNumber = numbers.remove(0); // Entfernt das Element an dem ersten "Index", return es und speichert es in 'pickedNumber' IntComparable randomNum = new IntComparable(pickedNumber); System.out.println("Einfügen: " + randomNum.getValue()); diff --git a/TerryModi/RBTree.java b/TerryModi/RBTree.java index c53356a..ca05b67 100644 --- a/TerryModi/RBTree.java +++ b/TerryModi/RBTree.java @@ -7,7 +7,7 @@ class RBTree> { private static final boolean BLACK = false; private class Node { - T key; + T key; // Node, 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> { 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> { 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> { } } - 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> { 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> { } } - 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> { */ 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");