private BSTNode<T> removeNode(BSTNode<T> tree) { T data; if (tree.getLeft() == null) return tree.getRight(); else if (tree.getRight() == null) return tree.getLeft(); else { data = getPredecessor(tree.getLeft()); tree.setInfo(data); tree.setLeft(recRemove(data, tree.getLeft())); return tree; } }
public void printBreadthFirstTraversal() { BSTNode b; LinkedUnbndQueue<BSTNode> q = new LinkedUnbndQueue<BSTNode>(); q.enqueue(root); while (!q.isEmpty()) { b = q.dequeue(); System.out.println(b.getInfo()); if (b.getLeft() != null) q.enqueue(b.getLeft()); if (b.getRight() != null) q.enqueue(b.getRight()); } }
public void printDepthFirstTraversal() { BSTNode b; LinkedStack<BSTNode> s = new LinkedStack<BSTNode>(); s.push(root); while (!s.isEmpty()) { b = s.top(); s.pop(); System.out.println(b.getInfo()); if (b.getRight() != null) s.push(b.getRight()); if (b.getLeft() != null) s.push(b.getLeft()); } }
private void printDepthFirstTraversalRec(BSTNode t) { if (t != null) { System.out.println(t.getInfo()); printDepthFirstTraversalRec(t.getLeft()); printDepthFirstTraversalRec(t.getRight()); } }
public int size2() // Returns the number of elements in this BST. { int count = 0; if (root != null) { LinkedStack<BSTNode<T>> hold = new LinkedStack<BSTNode<T>>(); BSTNode<T> currNode; hold.push(root); while (!hold.isEmpty()) { currNode = hold.top(); hold.pop(); count++; if (currNode.getLeft() != null) hold.push(currNode.getLeft()); if (currNode.getRight() != null) hold.push(currNode.getRight()); } } return count; }
private void postOrder(BSTNode<T> tree) // Initializes postOrderQueue with tree elements in postOrder order. { if (tree != null) { postOrder(tree.getLeft()); postOrder(tree.getRight()); postOrderQueue.enqueue(tree.getInfo()); } }
private BSTNode<T> recAdd(T element, BSTNode<T> tree) // Adds element to tree; tree retains its BST property. { if (tree == null) // Addition place found tree = new BSTNode<T>(element); else if (element.compareTo(tree.getInfo()) <= 0) tree.setLeft(recAdd(element, tree.getLeft())); // Add in left subtree else tree.setRight(recAdd(element, tree.getRight())); // Add in right subtree return tree; }
private T recGet(T element, BSTNode<T> tree) // Returns an element e from tree such that e.compareTo(element) == 0; // if no such element exists, returns null. { if (tree == null) return null; // element is not found else if (element.compareTo(tree.getInfo()) < 0) return recGet(element, tree.getLeft()); // get from left subtree else if (element.compareTo(tree.getInfo()) > 0) return recGet(element, tree.getRight()); // get from right subtree else return tree.getInfo(); // element is found }
private boolean recContains(T element, BSTNode<T> tree) // Returns true if tree contains an element e such that // e.compareTo(element) == 0; otherwise, returns false. { if (tree == null) return false; // element is not found else if (element.compareTo(tree.getInfo()) < 0) return recContains(element, tree.getLeft()); // Search left subtree else if (element.compareTo(tree.getInfo()) > 0) return recContains(element, tree.getRight()); // Search right subtree else return true; // element is found }
private BSTNode<T> recRemove(T element, BSTNode<T> tree) { if (tree == null) found = false; else if (element.compareTo(tree.getInfo()) < 0) tree.setLeft(recRemove(element, tree.getLeft())); else if (element.compareTo(tree.getInfo()) > 0) tree.setRight(recRemove(element, tree.getRight())); else { tree = removeNode(tree); found = true; } return tree; }
public BSTNode(BSTNode root) { this(root.getData(), root.getLeft(), root.getRight()); } // copy root constructor
private int recSize(BSTNode<T> tree) // Returns the number of elements in tree. { if (tree == null) return 0; else return recSize(tree.getLeft()) + recSize(tree.getRight()) + 1; }