private E removeEntry(BinaryNode node, E entry, BinaryNode father) {
    E result = null;
    if (entry.compareTo(node.getData()) == 0) {
      result = node.getData();
      decSize();
      if (!hasLeft(node)) { // der er ikke et venstre barn
        if (node.equals(father.getLeft())) {
          father.setLeft(right(node));
        } else {
          father.setRight(right(node));
        }

      } else if (!hasRight(node)) { // der er ikke et højre barn
        if (node.equals(father.getLeft())) {
          father.setLeft(left(node));
        } else {
          father.setRight(left(node));
        }

      } else { // der er både højre og venstre barn}
        removeNodeTwoChildren(node);
      }

    } else if (entry.compareTo(node.getData()) < 0) {
      if (hasLeft(node)) removeEntry((BinaryNode) left(node), entry, node);
    } else {
      if (hasRight(node)) removeEntry((BinaryNode) right(node), entry, node);
    }
    return result;
  }
예제 #2
0
  public T rMax(BinaryNode<T> node, T max) {
    if (!isEmpty()) {
      if (node.hasLeftChild()) max = rMax(node.getLeftChild(), max);
      if (node.hasRightChild()) max = rMax(node.getRightChild(), max);

      if (max == null) max = node.getData();
      else if (max.compareTo(node.getData()) == -1) max = node.getData();
    }

    return max;
  }
 private BinaryNode findEntry(BinaryNode node, E entry) {
   BinaryNode result = null;
   if (node != null) {
     if (node.getData().compareTo(entry) == 0) {
       result = node;
     } else if (node.getData().compareTo(entry) < 0) {
       result = findEntry((BinaryNode) right(node), entry);
     } else {
       result = findEntry((BinaryNode) left(node), entry);
     }
   }
   return result;
 }
 private void inorder(List<E> list, BinaryNode node) {
   if (node != null) {
     inorder(list, (BinaryNode) left(node));
     list.add(node.getData());
     inorder(list, (BinaryNode) right(node));
   }
 }
예제 #5
0
  public boolean bst(BinaryNode<T> node) {
    if (node.getData() == null) {
      return true;
    }

    if (node.getLeftChild() != null
        && node.getData().compareTo(node.getLeftChild().getData()) < 0) {
      return false;
    }

    if (node.getRightChild() != null
        && node.getData().compareTo(node.getRightChild().getData()) < 0) {
      return false;
    }

    return (node.getLeftChild() == null || bst(node.getLeftChild()))
        && (node.getRightChild() == null || bst(node.getRightChild()));
  }
예제 #6
0
  public T rMin(BinaryNode<T> node, T min) {
    if (!isEmpty()) {
      if (node.hasLeftChild()) {
        min = rMin(node.getLeftChild(), min);
      }
      if (node.hasRightChild()) {
        min = rMin(node.getRightChild(), min);
      }

      if (min == null) // if neither side has a child, set min to the node
      {
        min = node.getData();
      } else if (min.compareTo(node.getData()) == 1) {
        min = node.getData();
      }
    }

    return min;
  }
예제 #7
0
  protected BinaryNode<T> remove(T value, BinaryNode<T> root) {
    if (root == null) {
      return null;
    }

    int compareResult = value.compareTo(root.getData());

    if (compareResult < 0) {
      root.setLeft(remove(value, root.getLeft()));
    } else if (compareResult > 0) {
      root.setRight(remove(value, root.getRight()));
    } else if (root.getLeft() != null && root.getRight() != null) {
      root.setData(findMin(root.getRight()));
      root.setRight(remove(root.getData(), root.getRight()));
    } else {
      root = root.getLeft() != null ? root.getLeft() : root.getRight();
    }

    return root;
  }
예제 #8
0
 private T findMax(BinaryNode<T> root) {
   if (isEmpty()) {
     return null;
   } else {
     BinaryNode<T> traverse = root;
     while (true) {
       if (traverse.getRight() == null) break;
       else traverse = traverse.getRight();
     }
     return traverse.getData();
   }
 }
 private E addEntry(BinaryNode node, E entry) {
   E result = null;
   if (node.getData().compareTo(entry) == 0) {
     result = node.getData();
     node.setData(entry);
   } else if (entry.compareTo(node.getData()) < 0) {
     if (hasLeft(node)) {
       result = addEntry((BinaryNode) left(node), entry);
     } else {
       node.setLeft(new BinaryNode(entry));
       incSize();
     }
   } else {
     if (hasRight(node)) {
       result = addEntry((BinaryNode) right(node), entry);
     } else {
       node.setRight(new BinaryNode(entry));
       incSize();
     }
   }
   return result;
 }
 private void removeNodeTwoChildren(BinaryNode node) {
   BinaryNode current = (BinaryNode) right(node);
   BinaryNode behind = node;
   while (hasLeft(current)) {
     behind = current;
     current = (BinaryNode) left(current);
   }
   node.setData(current.getData());
   if (behind != node) {
     behind.setLeft(right(current));
   } else {
     node.setRight(right(current));
   }
 }
  public static void levelorderTraverse(BinaryNode<?> n) {
    Queue<BinaryNode<?>> nodequeue = new LinkedList<BinaryNode<?>>();

    if (n != null) nodequeue.add(n);
    while (!nodequeue.isEmpty()) {
      BinaryNode<?> next = nodequeue.remove();
      System.out.print(next.getData() + " \n");
      if (next.getLeft() != null) {
        nodequeue.add(next.getLeft());
      }
      if (next.getRight() != null) {
        nodequeue.add(next.getRight());
      }
    }
  }
예제 #12
0
  private BinaryNode<T> contains(T value, BinaryNode<T> root) {
    if (root == null) {
      return null;
    }

    int compareResult = value.compareTo(root.getData());

    if (compareResult < 0) {
      return contains(value, root.getLeft()); // Value is less than, look in left subtree
    } else if (compareResult > 0) {
      return contains(value, root.getRight()); // Value is greater than, look in right subtree
    } else {
      return root; // Found a match, return that node
    }
  }
예제 #13
0
  protected BinaryNode<T> insert(T value, BinaryNode<T> root) {
    if (root == null) {
      return new BinaryNode<T>(value, null, null, 0);
    }

    int compareResult = value.compareTo(root.getData());

    if (compareResult < 0) {
      root.setLeft(insert(value, root.getLeft()));
    } else if (compareResult > 0) {
      root.setRight(insert(value, root.getRight()));
    } else {; // Duplicate - do nothing
    }

    return root;
  }
예제 #14
0
  /**
   * Print node data.
   *
   * @param level the current level
   * @param offset the current offset
   * @param levelNodes the current level of nodes
   */
  private void printData(int level, int offset, BinaryNode<Integer> levelNodes[]) {
    printSpaces(offset);

    int k = POWERS_OF_2[level];
    for (int i = 0; i < k; i++) {
      BinaryNode<Integer> node = levelNodes[i];

      if (node != null) {
        System.out.printf("%3d ", node.getData());
      } else {
        System.out.print("    ");
      }

      // Space over to the next node in this level.
      if (i < k - 1) printSpaces(2 * offset - 2);
    }

    System.out.println();
  }