Ejemplo n.º 1
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);
  }
Ejemplo n.º 2
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);
  }