// Textbook fragment 07.17 /** 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; }