Example #1
0
  public void right_rotate(Node curr) {
    if (curr != null) {
      if (curr.getLeftChild() == null) {
        System.out.println("Cannot rotate right since left child is not present");
        return;
      }

      Node rotateWith = curr.getLeftChild();
      rotateWith.setParent(curr.getParent());

      if (curr.getParent() != null) {
        Node currParent = curr.getParent();
        if (curr == currParent.getLeftChild()) currParent.setLeftChild(rotateWith);
        else currParent.setRightChild(rotateWith);
      } else {
        root = rotateWith;
      }

      curr.setLeftChild(rotateWith.getRightChild());

      if (rotateWith.getRightChild() != null) rotateWith.getRightChild().setParent(curr);

      rotateWith.setRightChild(curr);
      curr.setParent(rotateWith);
    }
  }
Example #2
0
  private void add(NodeValue value) {
    Node r_node = new Node(last_node.getKey());

    nodes.remove(r_node.getKey());
    nodes.put(r_node.getKey().getType(), r_node);

    last_node.setLeftChild(r_node);
    last_node.setKey(new NodeValue(AND));
    Node new_node = new Node(value);

    nodes.put(new_node.getKey().getType(), new_node);

    last_node.setRightChild(new_node);
    last_node = new_node;
  }
Example #3
0
 // encapsulated/private working method
 private void insert(Node newNode, Node currRoot) {
   if (newNode.getKey() <= currRoot.getKey()) {
     if (currRoot.getLeftChild() != null) {
       insert(newNode, currRoot.getLeftChild());
     } else {
       currRoot.setLeftChild(newNode);
       newNode.setParent(currRoot);
     }
   } else {
     if (currRoot.getRightChild() != null) {
       insert(newNode, currRoot.getRightChild());
     } else {
       currRoot.setRightChild(newNode);
       newNode.setParent(currRoot);
     }
   }
 }
Example #4
0
 private static Node parseAndCreateTree() {
   if (treeString.charAt(position) == '(') {
     if (treeString.charAt(position + 1) == ')') {
       position += 2;
       return null;
     }
     char val = treeString.charAt(position + 1);
     Node curNode = new Node(String.valueOf(val));
     position += 3;
     if (treeString.charAt(position - 1) == ')') return curNode;
     Node leftRoot = parseAndCreateTree();
     position += 1;
     Node rightRoot = parseAndCreateTree();
     position += 1;
     curNode.setLeftChild(leftRoot);
     curNode.setRightChild(rightRoot);
     return curNode;
   }
   return null;
 }
Example #5
0
  public void delete(int key) {
    Node curr = search(key);
    Node restore = null;

    if (curr != null) {
      if (curr.getLeftChild() == null && curr.getRightChild() == null) {
        if (curr.getParent().getLeftChild() == curr) curr.getParent().setLeftChild(null);
        else curr.getParent().setRightChild(null);
        delete_restore_AVL_prop(curr.getParent());
      } else if (curr.getLeftChild() == null) {
        transplant(curr, curr.getRightChild());
        delete_restore_AVL_prop(curr.getParent());
      } else if (curr.getRightChild() == null) {
        transplant(curr, curr.getLeftChild());
        delete_restore_AVL_prop(curr.getParent());
      } else {
        Node successor = successor(curr.getKey());
        // since we have already handled the case of node to be deleted being leaf, the successor
        // that we get know will be the minimum of right subtree and hence won't have any left child
        if (successor.getParent() != curr) {
          transplant(
              successor,
              successor
                  .getRightChild()); // since successor doesn't have left child, we delete it and
          // replace it by its right child
          successor.setRightChild(curr.getRightChild());
          successor.getRightChild().setParent(successor);
          restore = successor.getParent();
        } else {
          restore = successor;
        }
        transplant(curr, successor);
        successor.setLeftChild(curr.getLeftChild());
        successor.getLeftChild().setParent(successor);
        delete_restore_AVL_prop(restore);
      }
    }
  }