Exemple #1
0
  public void add(int x) {
    BSTNode current = root;
    if (x < 0) {
      return;
    }
    if (root == null) {
      root = new BSTNode(x);
      return;
    }

    while (current.value != x) {
      if (x < current.value) {
        if (current.left == null) {
          current.left = new BSTNode(x);
        } else {
          current = current.left;
        }
      } else {
        if (current.right == null) {
          current.right = new BSTNode(x);
        } else {
          current = current.right;
        }
      }
    }
  }
Exemple #2
0
  public boolean remove(int x) {
    BSTNode current = root;
    BSTNode parent = null;
    boolean branch = true; // true =left, false =right

    while (current != null) {

      if (current.value == x) {
        BSTNode bigson = current;
        while (bigson.left != null || bigson.right != null) {
          parent = bigson;
          if (bigson.right != null) {
            bigson = bigson.right;
            branch = false;
          } else {
            bigson = bigson.left;
            branch = true;
          }
        }

        //		System.out.println("Remove: current "+current.value+" parent "+parent.value+" bigson
        // "+bigson.value);
        if (parent != null) {
          if (branch) {
            parent.left = null;
          } else {
            parent.right = null;
          }
        }

        if (bigson != current) {
          current.value = bigson.value;
        } else {;
        }

        return true;
      }

      parent = current;
      //	    if (current.value <x ) { // THERE WAS ERROR
      if (current.value > x) {
        current = current.left;
        branch = true;
      } else {
        current = current.right;
        branch = false;
      }
    }

    return false;
  }