// Textbook fragment 08.10 public class ArrayListCompleteBinaryTree implements CompleteBinaryTree { protected ArrayList> T; // indexed list of tree positions /** Nested class for a index list-based complete binary tree node. */ protected static class BTPos implements Position { E element; // element stored at this position int index; // index of this position in the array list public BTPos(E elt, int i) { element = elt; index = i; } public E element() { return element; } public int index() { return index; } public E setElement(E elt) { E temp = element; element = elt; return temp; } } /** default constructor */ public ArrayListCompleteBinaryTree() { T = new ArrayList>(); T.add(0, null); // the location at rank 0 is deliberately empty } /** Returns the number of (internal and external) nodes. */ public int size() { return T.size() - 1; } /** Returns whether the tree is empty. */ public boolean isEmpty() { return (size() == 0); }