/** * ArrayBinaryTree implements the BinaryTreeADT interface using an array * * @author Dr. Lewis * @author Dr. Chase * @version 1.0, 8/19/08 */ import java.util.Iterator; public class ArrayBinaryTree implements BinaryTreeADT { protected int count; protected T[] tree; private final int capacity = 50; /** * Creates an empty binary tree. */ public ArrayBinaryTree() { count = 0; tree = (T[]) new Object[capacity]; } /** * Creates a binary tree with the specified element as its root. * * @param element the element which will become the root of the new tree */ public ArrayBinaryTree (T element) { count = 1; tree = (T[]) new Object[capacity]; tree[0] = element; } /** * Private method to expand capacity if full. */ protected void expandCapacity() { T[] temp = (T[]) new Object[tree.length * 2]; for (int ct=0; ct < tree.length; ct++) temp[ct] = tree[ct]; tree = temp; } /** * Returns the root element of the tree * * @return element stored at the root * @throws EmptyCollectionException if the tree is empty */ public T getRoot() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("binary tree"); return tree[0]; } /** * Returns true if this binary tree is empty and false otherwise. * * @return true if this binary tree is empty */ public boolean isEmpty() { return (count == 0); } /** * Returns the integer size of this binary tree. * * @return the integer size of this binary tree */ public int size() { return count; } /** * Returns true if this tree contains an element that matches the * specified target element and false otherwise. * * @param targetElement the element being sought in the tree * @return true if the element is in this tree */ public boolean contains (T targetElement) { boolean found = false; for (int ct=0; ct templist = new ArrayUnorderedList(); inorder (0, templist); return templist.toString(); } /** * Performs an inorder traversal on this binary tree by calling an * overloaded, recursive inorder method that starts with * the root. * * @return an iterator over the binary tree */ public Iterator iteratorInOrder() { ArrayUnorderedList templist = new ArrayUnorderedList(); inorder (0, templist); return templist.iterator(); } /** * Performs a recursive inorder traversal. * * @param node the node used in the traversal * @param templist the temporary list used in the traversal */ protected void inorder (int node, ArrayUnorderedList templist) { if (node < tree.length) if (tree[node] != null) { inorder (node*2+1, templist); templist.addToRear(tree[node]); inorder ((node+1)*2, templist); } } /** * Performs an preorder traversal on this binary tree by calling an * overloaded, recursive preorder method that starts with * the root. * * @return an iterator over the binary tree */ public Iterator iteratorPreOrder() { ArrayUnorderedList templist = new ArrayUnorderedList(); preorder (0, templist); return templist.iterator(); } /** * Performs a recursive preorder traversal. * * @param node the node used in the traversal * @param templist the temporary list used in the traversal */ protected void preorder (int node, ArrayUnorderedList templist) { if (node < tree.length) if (tree[node] != null) { templist.addToRear(tree[node]); inorder (node*2+1, templist); inorder ((node+1)*2, templist); } } /** * Performs an postorder traversal on the binary tree by calling * an overloaded, recursive postorder method that starts * with the root. * * @return an iterator over the binary tree */ public Iterator iteratorPostOrder() { ArrayUnorderedList templist = new ArrayUnorderedList(); postorder (0, templist); return templist.iterator(); } /** * Performs a recursive postorder traversal. * * @param node the node used in the traversal * @param templist the temporary list used in the traversal */ protected void postorder (int node, ArrayUnorderedList templist) { if (node < tree.length) if (tree[node] != null) { inorder (node*2+1, templist); inorder ((node+1)*2, templist); templist.addToRear(tree[node]); } } /** * Performs a levelorder traversal on this binary tree, using a * tempList. * * @return an iterator over the binary tree */ public Iterator iteratorLevelOrder() { ArrayUnorderedList tempList = new ArrayUnorderedList(); int ct = 0; // current number of elements added to list int i = 0; // current position in array while (ct < count) { if (tree[i] != null) { tempList.addToRear(tree[i]); ct++; } i++; } return tempList.iterator(); } }