public class ArrayBinaryTree> implements BinaryTreeInterface { /** The default size of the underlying array. This will allow a tree of depth 9 */ private static final int DEFAULT_CAPACITY = 1032; /** The array in which the tree is stored. */ private E[] backArray; /** THe number of items in the tree */ private int size; /** Default constuctor. Make a tree able to hold a max of the default number of items */ public ArrayBinaryTree() { this(DEFAULT_CAPACITY); } /** Make a tree able to hold at most the given number f items * @param capacity the max number of items in the tree */ @SuppressWarnings("unchecked") public ArrayBinaryTree(int capacity) { size = 0; backArray = (E[]) new Comparable[capacity]; } /** * @return The number of items actually in the tree */ public int size() { return size; } /** * @return true iff the tree has any items */ public boolean isEmpty() { return size==0; } /** * Does the tree contain the given element * @param element the element to be checked for * @return true iff the tree contain the element */ public boolean contains(E element) { if (size==0) return false; return iContains(0, element)>=0; } /** * Recursive helper function for contains * @param loc the location in the backing array * @param toBeFound the item to be found * @return the location at which the item was found */ private int iContains(int loc, E toBeFound) { if (loc>=backArray.length) return -1; if (backArray[loc]==null) return -1; int cmp = backArray[loc].compareTo(toBeFound); if (cmp==0) { return loc; } else if (cmp>0) { return iContains(2*loc+1, toBeFound); } else { return iContains(2*loc+2, toBeFound); } } /** * Non-recursive version of contains * @param toBeFound the item to be found * @return true iff the item is in the tree */ public boolean containsAlt(E toBeFound) { return iContainsAlt(toBeFound)>=0; } /** * No recursive version of iContains * @param toBeFound the item to be found * @return the location of the item (or -1 if not in the tree) */ public int iContainsAlt(E toBeFound) { int loc=0; while (loc0) { loc = 2*loc+1; } else { loc = 2*loc+2; } } return -1; } /** * Insert element into tree */ public void insert(E element) { iInsert(0, element); } /** * Helper function for insert * @param loc the location of the current node * @param toBeAdded the item to be added to the tree */ private void iInsert(int loc, E toBeAdded) { if (loc>=backArray.length) return; if (backArray[loc]==null) { backArray[loc]=toBeAdded; size++; return; } int cmp = backArray[loc].compareTo(toBeAdded); if (cmp==0) { return; // the item is in the tree } if (cmp>0) { iInsert(loc*2+1, toBeAdded); } else { // cmp>0 iInsert(loc*2+2, toBeAdded); } } /** * Get the inorder predecessor * @param loc * @return */ private int getPredecessor(int loc) { while (true) { int nxt = (loc*2+2); if (nxt>backArray.length) return loc; if (backArray[nxt] == null) return loc; loc = nxt; } } /** * Get the inorder successor * @param loc * @return */ private int getSuccessor(int loc) { while (true) { int nxt = (loc*2+1); if (nxt>backArray.length) return loc; if (backArray[nxt] == null) return loc; loc = nxt; } } /** * Remove an item from the tree * @param toBeRemoved the item to be removed * @return true iff the item was in the tree and therefore was removed. */ public boolean remove(E toBeRemoved) { int loc = iContains(0, toBeRemoved); if (loc<0) return false; E cleft=null; E cright = null; int child = 2*loc+1; if (childbackArray.length) return; if (backArray[loc] == null) return; iPrint(loc*2+1, sb); sb.append(backArray[loc]); sb.append(" "); iPrint(loc*2+2, sb); } /** * @return the height of the tree */ public int height() { return iHeight(0); } /** * Recursive helper function for height * @param loc the location root of the current subtree * @return the height of this subtree */ private int iHeight(int loc) { if (loc>backArray.length) return 0; if (backArray[loc] == null) return 0; int hl = iHeight(loc*2+1); int hr = iHeight(loc*2+2); return (hr>hl?hr:hl)+1; } /** * A tricky, no-recursive version of height * @return the height of the tree */ public int altHeight() { for (int i=backArray.length-1; i>=0; i--) { if (backArray[i]!=null) { int j=0; while(i>0) { j++; i/=2; } return j; } } return 0; } public static void main(String[] args) { ArrayBinaryTree lbt = new ArrayBinaryTree<>(); int[] inn = { 100, 200, 50, 25, 75, 60, 12,150,175,187}; for (int ins : inn) { lbt.insert(ins); } System.out.println(lbt); //System.out.println(lbt.height()); //System.out.println(lbt.altHeight()); //System.out.println(lbt.contains(75)); //System.out.println(lbt.containsAlt(150)); //System.out.println(lbt.containsAlt(250)); lbt.remove(150); System.out.println(lbt); } }