package hw8; import java.util.Iterator; //begin#fragment LinkedBinaryTree /** * An implementation of the BinaryTree interface by means of a linked structure. //end#fragment LinkedBinaryTree * This class serves as a superclass for the BinarySearchTree * implementation. This design decision was made to emphasize the * conceptual relationship that a BinarySearchTree is a specialized * LinkedBinaryTree. An unwanted side-effect of this is that the * {@link #size() size} method returns the number of total nodes * whereas the {@link BinarySearchTree#size() size} method in the * {@link BinarySearchTree BinarySearchTree} class returns the number * of internal nodes only. For this reason, the the {@link #size * size} variable instead of the {@link #size() size} method is used * within this class. * * @author Luca Vismara, Roberto Tamassia, Michael Goodrich, Eric * Zamore * @see BinaryTree //begin#fragment LinkedBinaryTree */ public class LinkedBinaryTree implements BinaryTree { protected BTPosition root; // reference to the root protected int size; // number of nodes /** Creates an empty binary tree. */ public LinkedBinaryTree() { root = null; // start with an empty tree size = 0; } /** Returns the number of nodes in the tree. */ public int size() { return size; } //end#fragment LinkedBinaryTree /** Returns whether the tree is empty. */ public boolean isEmpty() { return (size == 0); } //begin#fragment LinkedBinaryTree /** Returns whether a node is internal. */ public boolean isInternal(Position v) throws InvalidPositionException { checkPosition(v); // auxiliary method return (hasLeft(v) || hasRight(v)); } //end#fragment LinkedBinaryTree /** Returns whether a node is external. */ public boolean isExternal(Position v) throws InvalidPositionException { return !isInternal(v); } //begin#fragment LinkedBinaryTree /** Returns whether a node is the root. */ public boolean isRoot(Position v) throws InvalidPositionException { checkPosition(v); return (v == root()); } /** Returns whether a node has a left child. */ public boolean hasLeft(Position v) throws InvalidPositionException { BTPosition vv = checkPosition(v); return (vv.getLeft() != null); } //end#fragment LinkedBinaryTree /** Returns whether a node has a right child. */ public boolean hasRight(Position v) throws InvalidPositionException { BTPosition vv = checkPosition(v); return (vv.getRight() != null); } //begin#fragment LinkedBinaryTree /** Returns the root of the tree. */ public Position root() throws EmptyTreeException { if (root == null) throw new EmptyTreeException("The tree is empty"); return root; } /** Returns the left child of a node. */ public Position left(Position v) throws InvalidPositionException, BoundaryViolationException { BTPosition vv = checkPosition(v); Position leftPos = vv.getLeft(); if (leftPos == null) throw new BoundaryViolationException("No left child"); return leftPos; } //end#fragment LinkedBinaryTree /** Returns the right child of a node. */ public Position right(Position v) throws InvalidPositionException, BoundaryViolationException { BTPosition vv = checkPosition(v); Position rightPos = vv.getRight(); if (rightPos == null) throw new BoundaryViolationException("No right child"); return rightPos; } //begin#fragment LinkedBinaryTree2 /** Returns the parent of a node. */ public Position parent(Position v) throws InvalidPositionException, BoundaryViolationException { BTPosition vv = checkPosition(v); Position parentPos = vv.getParent(); if (parentPos == null) throw new BoundaryViolationException("No parent"); return parentPos; } /** Returns an iterable collection of the children of a node. */ public Iterable> children(Position v) throws InvalidPositionException { PositionList> children = new NodePositionList>(); if (hasLeft(v)) children.addLast(left(v)); if (hasRight(v)) children.addLast(right(v)); return children; } /** Returns an iterable collection of the tree nodes. */ public Iterable> positions() { PositionList> positions = new NodePositionList>(); if(size != 0) preorderPositions(root(), positions); // assign positions in preorder return positions; } /** Returns an iterator of the elements stored at the nodes */ public Iterator iterator() { Iterable> positions = positions(); PositionList elements = new NodePositionList(); for (Position pos: positions) elements.addLast(pos.element()); return elements.iterator(); // An iterator of elements } /** Replaces the element at a node. */ public E replace(Position v, E o) throws InvalidPositionException { BTPosition vv = checkPosition(v); E temp = v.element(); vv.setElement(o); return temp; } //end#fragment LinkedBinaryTree2 //begin#fragment LinkedBinaryTree3 // Additional accessor method /** Return the sibling of a node */ public Position sibling(Position v) throws InvalidPositionException, BoundaryViolationException { BTPosition vv = checkPosition(v); BTPosition parentPos = vv.getParent(); if (parentPos != null) { BTPosition sibPos; BTPosition leftPos = parentPos.getLeft(); if (leftPos == vv) sibPos = parentPos.getRight(); else sibPos = parentPos.getLeft(); if (sibPos != null) return sibPos; } throw new BoundaryViolationException("No sibling"); } // Additional update methods /** Adds a root node to an empty tree */ public Position addRoot(E e) throws NonEmptyTreeException { if(!isEmpty()) throw new NonEmptyTreeException("Tree already has a root"); size = 1; root = createNode(e,null,null,null); return root; } /** Inserts a left child at a given node. */ public Position insertLeft(Position v, E e) throws InvalidPositionException { BTPosition vv = checkPosition(v); Position leftPos = vv.getLeft(); if (leftPos != null) throw new InvalidPositionException("Node already has a left child"); BTPosition ww = createNode(e, vv, null, null); vv.setLeft(ww); size++; return ww; } //end#fragment LinkedBinaryTree3 /** Inserts a right child at a given node. */ public Position insertRight(Position v, E e) throws InvalidPositionException { BTPosition vv = checkPosition(v); Position rightPos = vv.getRight(); if (rightPos != null) throw new InvalidPositionException("Node already has a right child"); BTPosition w = createNode(e, vv, null, null); vv.setRight(w); size++; return w; } //begin#fragment LinkedBinaryTree4 /** Removes a node with zero or one child. */ public E remove(Position v) throws InvalidPositionException { BTPosition vv = checkPosition(v); BTPosition leftPos = vv.getLeft(); BTPosition rightPos = vv.getRight(); if (leftPos != null && rightPos != null) throw new InvalidPositionException("Cannot remove node with two children"); BTPosition ww; // the only child of v, if any if (leftPos != null) ww = leftPos; else if (rightPos != null) ww = rightPos; else // v is a leaf ww = null; if (vv == root) { // v is the root if (ww != null) ww.setParent(null); root = ww; } else { // v is not the root BTPosition uu = vv.getParent(); if (vv == uu.getLeft()) uu.setLeft(ww); else uu.setRight(ww); if(ww != null) ww.setParent(uu); } size--; return v.element(); } //end#fragment LinkedBinaryTree4 //begin#fragment LinkedBinaryTree5 /** Attaches two trees to be subtrees of an external node. */ public void attach(Position v, BinaryTree T1, BinaryTree T2) throws InvalidPositionException { BTPosition vv = checkPosition(v); if (isInternal(v)) throw new InvalidPositionException("Cannot attach from internal node"); int newSize = size + T1.size() + T2.size(); if (!T1.isEmpty()) { BTPosition r1 = checkPosition(T1.root()); vv.setLeft(r1); r1.setParent(vv); // T1 should be invalidated } if (!T2.isEmpty()) { BTPosition r2 = checkPosition(T2.root()); vv.setRight(r2); r2.setParent(vv); // T2 should be invalidated } size = newSize; } //end#fragment LinkedBinaryTree5 /** Swap the elements at two nodes */ public void swapElements(Position v, Position w) throws InvalidPositionException { BTPosition vv = checkPosition(v); BTPosition ww = checkPosition(w); E temp = w.element(); ww.setElement(v.element()); vv.setElement(temp); } /** Expand an external node into an internal node with two external node children */ public void expandExternal(Position v, E l, E r) throws InvalidPositionException { if (!isExternal(v)) throw new InvalidPositionException("Node is not external"); insertLeft(v, l); insertRight(v, r); } /** Remove an external node v and replace its parent with v's sibling */ public void removeAboveExternal(Position v) throws InvalidPositionException { if (!isExternal(v)) throw new InvalidPositionException("Node is not external"); if (isRoot(v)) remove(v); else { Position u = parent(v); remove(v); remove(u); } } // Auxiliary methods //begin#fragment LinkedBinaryTree5 /** If v is a good binary tree node, cast to BTPosition, else throw exception */ protected BTPosition checkPosition(Position v) throws InvalidPositionException { if (v == null || !(v instanceof BTPosition)) throw new InvalidPositionException("The position is invalid"); return (BTPosition) v; } /** Creates a new binary tree node */ protected BTPosition createNode(E element, BTPosition parent, BTPosition left, BTPosition right) { return new BTNode(element,parent,left,right); } /** Creates a list storing the the nodes in the subtree of a node, * ordered according to the preorder traversal of the subtree. */ protected void preorderPositions(Position v, PositionList> pos) throws InvalidPositionException { pos.addLast(v); if (hasLeft(v)) preorderPositions(left(v), pos); // recurse on left child if (hasRight(v)) preorderPositions(right(v), pos); // recurse on right child } //end#fragment LinkedBinaryTree5 /** Creates a list storing the the nodes in the subtree of a node, * ordered according to the inorder traversal of the subtree. */ protected void inorderPositions(Position v, PositionList> pos) throws InvalidPositionException { if (hasLeft(v)) inorderPositions(left(v), pos); // recurse on left child pos.addLast(v); if (hasRight(v)) inorderPositions(right(v), pos); // recurse on right child } }