/** * 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(); }
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()); } }
/** * Internal method to insert into a subtree. * * @param x the item to insert. * @param t the node that roots the tree. * @return the new root. * @throws DuplicateItemException if x is already present. */ protected BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t) { if (t == null) t = new BinaryNode<AnyType>(x); else if (x.compareTo(t.element) < 0) t.left = insert(x, t.left); else if (x.compareTo(t.element) > 0) t.right = insert(x, t.right); else throw new DuplicateItemException(x.toString()); // Duplicate return t; }
/** * 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(); }
private BinaryNode singleLeftRotation() { BinaryNode temp = this.right; if (temp != null) { this.right = temp.left; } temp.left = this; rotationCount++; return temp; }
/** * Return a reference to a node that is the root of a duplicate of the binary tree rooted at the * current node. */ public BinaryNode<AnyType> duplicate() { BinaryNode<AnyType> root = new BinaryNode<AnyType>(element, null, null); if (left != null) // If there's a left subtree root.left = left.duplicate(); // Duplicate; attach if (right != null) // If there's a right subtree root.right = right.duplicate(); // Duplicate; attach return root; // Return resulting tree }
public void postorder(BinaryNode root) { if (root == null) { return; } postorder(root.getLeft()); postorder(root.getRight()); System.out.print(root.data + " "); }
public BinaryNode add(BinaryNode current, int data) { if (current == null) { current = new BinaryNode(data); } else { if (data >= (Integer) current.data) current.right = add(current.right, data); else current.left = add(current.left, data); } return current; }
/** * Internal method to insert into a subtree. * * @param x the item to insert. * @param t the node that roots the tree. * @return the new root. * @throws DuplicateItemException if x is already present. */ protected BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t) { if (t == null) t = new BinaryNode<AnyType>(x); else if (x.compareTo(t.element) < 0) t.left = insert(x, t.left); else if (x.compareTo(t.element) > 0) t.right = insert(x, t.right); else { t.increaseDuplicateCount(); t.hasDuplicate = true; t.duplicate = duplicate(x, t.duplicate); } return t; }
/** * Internal method to remove from a subtree. * * @param x the item to remove. * @param t the node that roots the tree. * @return the new root. * @throws ItemNotFoundException if x is not found. */ protected BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> t) { if (t == null) throw new ItemNotFoundException(x.toString()); if (x.compareTo(t.element) < 0) t.left = remove(x, t.left); else if (x.compareTo(t.element) > 0) t.right = remove(x, t.right); else if (t.left != null && t.right != null) // Two children { t.element = findMin(t.right).element; t.right = removeMin(t.right); } else t = (t.left != null) ? t.left : t.right; return t; }
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); }
/** * Recursive helper method to insert a value in the tree * * @param x the value to be inserted * @param t the node to insert value in * @return the node with the inserted value */ protected BinaryNode<Integer> insert(Integer x, BinaryNode<Integer> t) { if (t == null) { return new BinaryNode<Integer>(x, null, null); } int compareResults = x.compareTo((Integer) t.element); if (compareResults < 0) { t.left = insert(x, t.left); } else if (compareResults > 0) { t.right = insert(x, t.right); } return t; }
public String buildAString(ArrayList<BinaryNode> nodes) { StringBuilder temp = new StringBuilder(); for (BinaryNode t : nodes) { temp.append(" " + t.getElement()); if (t.duplicate != null) { temp.append("*"); } } return temp.toString(); }
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 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; }
public ArrayList<Object> toArrayList(ArrayList<Object> list) { if (left == null && right == null) { list.add(this); return list; } list.add(this); if (left != null) { left.toArrayList(list); } if (right != null) { right.toArrayList(list); } return list; }
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()); } }
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 } }
/** * Internal method to remove minimum item from a subtree. * * @param t the node that roots the tree. * @return the new root. * @throws ItemNotFoundException if t is empty. */ protected BinaryNode<AnyType> removeMin(BinaryNode<AnyType> t) { if (t == null) throw new ItemNotFoundException(); else if (t.left != null) { t.left = removeMin(t.left); return t; } else return t.right; }
public ArrayList<Object> toArrayList() { ArrayList<Object> list = new ArrayList<Object>(); if (root == null) { return list; } return root.toArrayList(list); }
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)); } }
/** * 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; }
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; }
/** * Recursive helper method to remove a value from the tree * * @param x the value to be removed * @param t the node to remove value from * @return the node with removed value */ protected BinaryNode<Integer> remove(Integer x, BinaryNode<Integer> t) { if (t == null) { return t; } int compareResult = x.compareTo(t.element); if (compareResult < 0) { t.left = remove(x, t.left); } else if (compareResult > 0) { t.right = remove(x, t.right); } else if (t.left != null && t.right != null) { t.element = findMin(t.right).element; t.right = remove(t.element, t.right); } else { t = (t.left != null) ? t.left : t.right; } return t; }
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; }
/** * Internal method to handle duplicates into a subtree. * * @param x the item to insert. * @param t the node that roots the tree. * @return the new root. * @throws DuplicateItemException if x is already present. */ protected BinaryNode<AnyType> duplicate(AnyType x, BinaryNode<AnyType> t) { if (t == null) { t = new BinaryNode<AnyType>(x); } else { t.duplicate = duplicate(x, t.duplicate); } return t; }
/** * 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(); }
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; }
public BinaryNode toTree(BinaryNode root, int[] array, int length, int counter) { if (counter == length) { counter = 0; return null; } else { BinaryNode node = new BinaryNode(array[counter]); if (array[counter] <= (Integer) root.data) { root.right = node; counter++; toTree(root.right, array, length, counter); } else { root.left = node; counter++; toTree(root.left, array, length, counter); } } return root; }