private int internalPathLength(BinaryNode<AnyType> root, int curLevel) {
   if (root == null) {
     return 0;
   }
   if ((root.getLeft() == null) && (root.getRight() == null)) {
     return 0;
   }
   return curLevel
       + internalPathLength(root.getLeft(), curLevel + 1)
       + internalPathLength(root.getRight(), curLevel + 1);
 }
 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 int externalNodeCount(BinaryNode root) {

    // BinaryNode left = root.getLeft();

    // BinaryNode right = root.getRight();

    if (root == null) {
      return 0;
    } else if ((root.getLeft() == null) && (root.getRight() == null)) {
      return 1;
    } else {
      return externalNodeCount(root.getRight()) + externalNodeCount(root.getLeft());
    }
  }
  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());
      }
    }
  }
 protected Value get(final BinaryNode<Key, Value> node, final Key key) {
   if (node == null) return null;
   final int compare = node.compareTo(key);
   if (compare == 0) return node.getValue();
   if (compare > 0) return get(node.getLeft(), key);
   return get(node.getRight(), key);
 }
 public void inorder(BinaryNode root) {
   if (root != null) {
     inorder(root.getLeft());
     System.out.print(root.data + " ");
     inorder(root.getRight());
   }
 }
Beispiel #7
0
  /**
   * Prepare the next level of nodes.
   *
   * @param level the current level
   * @param levelNodes the current level of nodes
   * @return the next level of nodes.
   */
  private BinaryNode<Integer>[] nextLevel(int level, BinaryNode<Integer> levelNodes[]) {
    BinaryNode<Integer> nextLevel[] =
        (BinaryNode<Integer>[]) new BinaryNode[POWERS_OF_2[level + 1]];

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

      // Queue the left child nodes of each non-null parent node.
      nextLevel[2 * i] = (node != null) && (node.getLeft() != null) ? node.getLeft() : null;

      // Queue the right child nodes of each non-null parent node.
      nextLevel[2 * i + 1] = (node != null) && (node.getRight() != null) ? node.getRight() : null;
    }

    return nextLevel;
  }
Beispiel #8
0
  /**
   * Print the connecting dashes between an outgoing pointer and an incoming pointer.
   *
   * @param level the current level
   * @param offset the current offset
   * @param levelNodes the current level of nodes
   */
  private void printConnectingDashes(int level, int offset, BinaryNode<Integer> levelNodes[]) {
    if (offset > 1) printSpaces(offset);

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

      // Has left child: print dashes
      if ((node != null) && (node.getLeft() != null)) {
        printSpaces(3);
        printDashes(offset - 1);
      }

      // No left child: print spaces
      else {
        printSpaces(offset + 2);
      }

      // Has right child: print dashes
      if ((node != null) && (node.getRight() != null)) {
        printSpaces(2);
        printDashes(offset - 1);
      }

      // No right child: print spaces
      else {
        printSpaces(offset + 1);
      }

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

    System.out.println();
  }
Beispiel #9
0
  /**
   * Print incoming pointers / or \ to each non-null child node.
   *
   * @param level the current level
   * @param offset the current offset
   * @param levelNodes the current level of nodes
   */
  private void printIncomingPointers(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];

      // Left child: print /
      if ((node != null) && (node.getLeft() != null)) {
        System.out.print("  /");
      }

      // No left child: print spaces
      else {
        printSpaces(3);
      }

      // Right child: print \
      if ((node != null) && (node.getRight() != null)) {
        printSpaces(2 * offset);
        System.out.print("\\");
      }

      // No right child: print spaces
      else {
        printSpaces(2 * offset + 1);
      }

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

    System.out.println();
  }
  public void postorder(BinaryNode root) {

    if (root == null) {
      return;
    }

    postorder(root.getLeft());
    postorder(root.getRight());
    System.out.print(root.data + " ");
  }
  public int getMax(BinaryNode root, int current) {

    if (root != null) {
      if ((Integer) root.data >= (Integer) current) {
        current = (Integer) root.data;
      }
      current = getMax(root.getRight(), current);
    }

    return current;
  }
  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;
  }
  protected BinaryNode<Key, Value> put(
      final BinaryNode<Key, Value> node, final Key key, final Value value) {
    if (node == null) {
      ++size;
      return new BinaryNode<>(key, value);
    }

    final int compare = node.compareTo(key);
    if (compare > 0) node.setLeft(put(node.getLeft(), key, value));
    else if (compare < 0) node.setRight(put(node.getRight(), key, value));
    else node.setValue(value);

    return node;
  }
  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
    }
  }
  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;
  }
  private int countElements(BinaryNode root) {

    BinaryNode duplicate = root.getDuplicate();
    BinaryNode right = root.getRight();
    BinaryNode left = root.getLeft();
    int c = 1;
    if (duplicate != null) {
      c += countElements(duplicate);
    }
    if (right != null) {
      c += countElements(right);
    }
    if (left != null) {
      c += countElements(left);
    }

    return c;
  }
  protected int height(BinaryNode<T> root) {
    if (root == null) return -1;

    return Math.max(height(root.getLeft()), height(root.getRight())) + 1;
  }