/** * An implemention of a linked binary tree * @author gtowell * * Methods with an @override annotation are documented in BinaryTreeInterface * * Methods named xxxAlt are alternative implementations. The alternatives * have exactly the same effect, they just achieve it slightly differently. * @param */ public class LinkedBinaryTree> implements BinaryTreeInterface { /** * A private class implementing the tree node */ private class Node { /** The data in the node */ E payload; /** The right child */ Node right; /** The left child */ Node left; /** Node constructor. Just takes the data element. Sets the right and left to * null * @param e the data element to be held in the node */ public Node(E e) { payload=e; right=null; left=null; } /** A print representation of the node. This just relies on the print rep * of the payload */ public String toString() { return payload.toString(); } } /** The number of elements in the tree */ private int size; /** The root of the tree */ private Node root; /** * Create an empty LinnkedBinaryTree */ public LinkedBinaryTree() { root=null; size=0; } @Override public int size() { return size; } /** * Alternate implementation of size() that does not use the size instance variable * @return the number of nodes in the tree */ public int sizeAlt() { return iSize(root); } /** * Private recursive helper method for size. * Returns the size of the subtree rooted at treepart * @param treepart the root of a subtree * @return the size of the subtree */ private int iSize(Node treepart) { if (treepart==null) return 0; return 1 + iSize(treepart.left) + iSize(treepart.right); } @Override public boolean isEmpty() { return size==0; } /** * Alternate implementation of isEmpty that does not use the size instance variable * Without size just check is the root is null * @return true iff the tree has no nodes. */ public boolean isEmptyAlt() { return root==null; } @Override public boolean contains(E element) { if (root==null) return false; return iContains(root, element)!=null; } /** * Recursive helper function for determining if an element is in the tree. * @param treepart the root of the current subtree to examine * @param toBeFound the element being searched for * @return true iff the element is in the tree. */ private Node iContains(Node treepart, E toBeFound) { int cmp = treepart.payload.compareTo(toBeFound); if (cmp==0) return treepart; if (cmp<0) { if (treepart.left==null) return null; else return iContains(treepart.left, toBeFound); } else { if (treepart.right==null) return null; else { return iContains(treepart.right, toBeFound); } } } /** * A version of iContains that more closely follows the algorithm and pseudocode * in class. * @param treepart the root of a subtree * @param toBeFound the value to be looked for * @return if found, the node containing the value, otherwise null. */ private Node iContainsAlt(Node treepart, E toBeFound) { if (treepart==null) return null; int cmp = treepart.payload.compareTo(toBeFound); if (cmp==0) return treepart; if (cmp<0) { return iContains(treepart.left, toBeFound); } else { return iContains(treepart.right, toBeFound); } } @Override public void insert(E element) { root = iInsert(root, element); } private Node iInsert(Node treepart, E element) { if (treepart == null) { size++; return new Node(element); } int cmp = treepart.payload.compareTo(element); if (cmp==0) return treepart; if (cmp<0) { treepart.left = iInsert(treepart.left, element); return treepart; } else { treepart.right = iInsert(treepart.right, element); return treepart; } } public void insertAlt(E element) { size++; if (root==null) { root=new Node(element); return; } iInsert(root, element); } /** * Recursive helper function for insertion of an element into a tree. * @param treepart the root of the current subtree * @param toBeAdded the element to be added to the tree */ private void iInsertAlt(Node treepart, E toBeAdded) { int cmp = treepart.payload.compareTo(toBeAdded); if (cmp==0) { return; // the item is in the tree } else if (cmp<0) { if (treepart.left==null) { treepart.left=new Node(toBeAdded); } else { iInsertAlt(treepart.left, toBeAdded); } } else // cmp>0 { if (treepart.right==null) { treepart.right=new Node(toBeAdded); } else { iInsertAlt(treepart.right, toBeAdded); } } } @Override public boolean remove(E element) { int oSize=size; root = iRemove(root, element); return oSize!=size; } /** * Find the value stored in the leftmost node of the tree * @param sRoot the subtree root * @return the data leement in the left most node of the subtree */ private E iMinKey(Node sRoot) { if (sRoot.left==null) return sRoot.payload; else return iMinKey(sRoot.left); } /** * Recursive helper function for removing a node with the given element * @param sRoot the root of the subtree being examined. * @param element the element to be removed. * @return the root of the subtree after element deletion */ private Node iRemove(Node sRoot, E element) { if (sRoot==null) return null; int cmpr = sRoot.payload.compareTo(element); if (cmpr<0) { sRoot.left = iRemove(sRoot.left, element); return sRoot; } else if (cmpr > 0) { sRoot.right = iRemove(sRoot.right, element); return sRoot; } else // == 0 { if (sRoot.right==null) // this will also get a node with no children. { size--; return sRoot.left; } else if (sRoot.left==null) { size--; return sRoot.right; } else { // two children // no size-- because this node is not being removed! E pred = iMinKey(sRoot.right); sRoot.payload= pred; sRoot.right=iRemove(sRoot.right, pred); return sRoot; } } } /** * An internal recursive helper function to calculate the height of the tree * with the given root. * @param node the root of the subtree for which the height is desired * @return */ int iMaxDepth(Node node) { if (node==null) return 0; int rd=iMaxDepth(node.right)+1; int ld=iMaxDepth(node.left)+1; if (rd>ld) return rd; else return ld; } @Override public int height() { return iMaxDepth(root)-1; } public static void main(String args[]) { LinkedBinaryTree lbt = new LinkedBinaryTree<>(); lbt.insert(100); lbt.insert(200); lbt.insert(50); lbt.insert(25); lbt.insert(75); lbt.insert(12); System.out.println(lbt); System.out.println("size="+lbt.size()); System.out.println(lbt.remove(100)); System.out.println("size=" + lbt.size()); System.out.println("height " + lbt.height()); System.out.println(lbt); } @Override public String printNaturalOrder() { return ""; } }