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