Example #1
0
  public void print() {

    Node marker = new Node();
    Node emptyNode = new Node();

    Queue<Node> queue = new ArrayBlockingQueue<Node>(1000);

    queue.offer(root);
    queue.offer(marker);
    while (!queue.isEmpty()) {
      Node currentNode = queue.poll();
      if (currentNode == marker && !queue.isEmpty()) {
        queue.offer(marker);
        System.out.println("");
      } else {
        if (currentNode == emptyNode) {
          System.out.print(" - ");
        } else {
          if (currentNode != marker) {
            String print = "  " + currentNode.getKey() + "  ";
            if (currentNode.isRed()) print = " <" + currentNode.getKey() + "> ";
            System.out.print(print);
          }
          if (currentNode.getLeft() == null) queue.offer(emptyNode);
          else queue.offer(currentNode.getLeft());

          if (currentNode.getRight() == null) queue.offer(emptyNode);
          else queue.offer(currentNode.getRight());
        }
      }
    }
  }
Example #2
0
  private void afterInsert(Node node) {

    Node parent = node.getParent();
    Node uncle = node.getUncle();
    Node grandParent = node.getGrandParent();

    if (node != root) {

      if (parent.isRed()) {

        if (parent.isRed() && uncle.isRed()) {
          grandParent.setColor(RED);
          parent.setColor(BLACK);
          uncle.setColor(BLACK);
          afterInsert(grandParent);

        } else if (grandParent != null) {

          if (parent.isLeft()) {
            if (node.isRight()) parent = rotateLeft(node);

            parent = rotateRight(parent);
            parent.setColor(BLACK);
            parent.getRight().setColor(RED);
          } else {
            if (node.isLeft()) parent = rotateRight(node);

            parent = rotateLeft(parent);
            parent.setColor(BLACK);
            parent.getLeft().setColor(RED);
          }
        }
      }
    }

    root.setColor(BLACK);
  }
Example #3
0
  private void afterDelete(Node node) {

    while (node != root && node.isBlack()) {
      if (node.isLeft()) {

        Node sibling = node.getParent().getRight();
        if (sibling.isRed()) {
          Node parent;
          sibling.setColor(BLACK);
          node.getParent().setColor(RED);
          parent =
              // rotateLeft(node.getParent());
              rotateLeft(node.getParent());
          // sibling = node.getParent().getRight();
          sibling = parent.getRight();
        }
        if (sibling.getLeft() != null
            && sibling.getRight() != null
            && sibling.getLeft().isBlack()
            && sibling.getRight().isBlack()) {
          sibling.setColor(RED);
          node = node.getParent();
        } else if (sibling.getRight() != null && sibling.getRight().isBlack()) {
          sibling.getLeft().setColor(BLACK);
          sibling.setColor(RED);
          rotateRight(sibling);
          // sibling = node.getParent().getRight();
          sibling = node.getRight();
        }

        sibling.setColor(node.getParent().getColor());
        node.getParent().setColor(BLACK);
        if (sibling.getRight() != null) sibling.getRight().setColor(BLACK);
        rotateLeft(node.getParent());
        node = root;

      } else {
        Node parent = null;
        Node sibling = node.getParent().getLeft();
        if (sibling.isRed()) {

          sibling.setColor(BLACK);
          node.getParent().setColor(RED);
          parent = rotateRight(node.getParent());
          // rotateRight(node.getParent());
          // sibling = node.getParent().getLeft();
          sibling = parent.getLeft();
        }
        if (sibling.getLeft() != null
            && sibling.getRight() != null
            && sibling.getLeft().isBlack()
            && sibling.getRight().isBlack()) {
          sibling.setColor(RED);
          // node = node.getParent();
          node = parent;
        } else if (sibling.getLeft() != null && sibling.getLeft().isBlack()) {

          sibling.getRight().setColor(BLACK);
          sibling.setColor(RED);
          // sibling =
          rotateLeft(sibling);
          // sibling = node.getParent().getLeft();
          sibling = parent.getLeft();
        }

        if (node.getParent() == null) sibling.setColor(BLACK);
        else sibling.setColor(node.getParent().getColor());
        if (node.getParent() != null) node.getParent().setColor(BLACK);
        if (sibling.getLeft() != null) sibling.getLeft().setColor(BLACK);
        rotateRight(node.getParent());
        node = root;
      }
    }
    node.setColor(BLACK);
  }