diff --git a/exceptions/NonEmptyTreeException.java b/exceptions/NonEmptyTreeException.java new file mode 100644 index 0000000..03e6089 --- /dev/null +++ b/exceptions/NonEmptyTreeException.java @@ -0,0 +1,18 @@ +package exceptions; + +/** + * Signals that the boundaries of a data structure have been illegally + * traversed (e.g. past the end of a list). + * @author Roberto Tamassia + */ +//Copyright (c) 2003 Brown University, Providence, RI +//Additional modifications and methods by xgiovio + +public class NonEmptyTreeException extends RuntimeException { + public NonEmptyTreeException(String message) { + super (message); + } + public NonEmptyTreeException() { + super ("Tree already has a root"); + } +} diff --git a/exceptions/UndeletableNodeException.java b/exceptions/UndeletableNodeException.java new file mode 100644 index 0000000..feeb912 --- /dev/null +++ b/exceptions/UndeletableNodeException.java @@ -0,0 +1,17 @@ +package exceptions; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 05/03/14 + * Time: 0.09 + */ +public class UndeletableNodeException extends RuntimeException { + + public UndeletableNodeException(){ + super("Undeletable Node"); + } + public UndeletableNodeException(String msg){ + super(msg); + } +} diff --git a/tree/LinkedTree.java b/tree/LinkedTree.java index 0028969..e679d20 100644 --- a/tree/LinkedTree.java +++ b/tree/LinkedTree.java @@ -1,9 +1,9 @@ package tree; -import exceptions.BoundaryViolationException; -import exceptions.EmptyTreeException; -import exceptions.InvalidPositionException; +import exceptions.*; +import position.NodePositionList; import position.Position; +import position.PositionList; import java.util.Iterator; @@ -15,62 +15,178 @@ import java.util.Iterator; */ public class LinkedTree implements Tree { + protected TreePosition root = null; + protected int size = 0; + - @Override public int size() { - return 0; + return size; } - @Override public boolean isEmpty() { - return false; + return (size == 0); } - @Override + public Iterable> positions() { - return null; + PositionList> positions = new NodePositionList>(); + if(size != 0) + preorderPositions(root(), positions); + return positions; } - @Override - public E replace(Position v, E e) throws InvalidPositionException { - return null; + public E replace(Position v, E o) throws InvalidPositionException { + TreePosition vv = checkPosition(v); + E temp = v.element(); + vv.setElement(o); + return temp; } - @Override public Position root() throws EmptyTreeException { - return null; + if (root == null) + throw new EmptyTreeException("The tree is empty"); + return root; } - @Override + public Position parent(Position v) throws InvalidPositionException, BoundaryViolationException { - return null; + TreePosition vv = checkPosition(v); + Position parentPos = vv.getParent(); + if (parentPos == null) + throw new BoundaryViolationException("No parent"); + return parentPos; } - @Override public Iterable> children(Position v) throws InvalidPositionException { - return null; + TreePosition vv = checkPosition(v); + if (isExternal(v)) + throw new InvalidPositionException("External nodes have no children"); + return vv.getChildren(); } - @Override public boolean isInternal(Position v) throws InvalidPositionException { - return false; + return !isExternal(v); } - @Override public boolean isExternal(Position v) throws InvalidPositionException { - return false; + TreePosition vv = checkPosition(v); + return vv.getChildren().isEmpty(); } - @Override + + + public boolean isRoot(Position v) throws InvalidPositionException { - return false; + checkPosition(v); + return (v == root()); + } + + public Iterator iterator() { + Iterable> positions = positions(); + PositionList elements = new NodePositionList(); + for (Position pos: positions) + elements.addLast(pos.element()); + return elements.iterator(); + } + + ////////////////////////// help methods + + protected void preorderPositions(Position v, PositionList> pos) throws InvalidPositionException { + + TreePosition a = checkPosition(v); + pos.addLast(a); + + for (Position w : a.getChildren()) + preorderPositions(w, pos); + } + + protected TreePosition checkPosition(Position v) + throws InvalidPositionException { + if (v == null || !(v instanceof TreePosition) || isEmpty() ) + throw new InvalidPositionException("The position is invalid"); + return (TreePosition) v; + } + + + + public Position addRoot(E e) throws NonEmptyTreeException { + if(!isEmpty()) + throw new NonEmptyTreeException("Tree already has a root"); + size = 1; + root = createNode(e,null,null); + return root; + } + + public void swapElements(Position v, Position w) + throws InvalidPositionException { + TreePosition vv = checkPosition(v); + TreePosition ww = checkPosition(w); + E temp = w.element(); + ww.setElement(v.element()); + vv.setElement(temp); + } + + protected TreePosition createNode(E element, TreePosition parent, PositionList> children) { + return new TreeNode(element,parent,children); + } + + + public E removeRoot() throws EmptyTreeException, UndeletableNodeException { + if (isEmpty()) + throw new EmptyTreeException(); + if (size() > 1) + throw new UndeletableNodeException(); + E to_return = root().element(); + root = null; + size--; + return to_return; + + } + + public E removeExternalChild(Position v) throws UndeletableNodeException,InvalidPositionException{ + TreePosition a = checkPosition(v); + if (isExternal(a)) + throw new InvalidPositionException(); + PositionList> c = a.getChildren(); + if (isInternal(c.first().element())) + throw new UndeletableNodeException(); + + E to_return = (c.remove(c.first())).element(); + size--; + return to_return; + + } + + public Position addChild (E e , Position v) throws InvalidPositionException { + TreePosition a = checkPosition(v); + a.getChildren().addLast(createNode(e,a,null)); + size++; + return a.getChildren().last().element(); + + } + + public int depth ( Position v) { + TreePosition a = checkPosition(v); + if (a.getParent() == null) + return 0; + return (depth(a.getParent()) + 1); + } + + public int height ( Position v) { + TreePosition a = checkPosition(v); + if (isExternal(a)) + return 0; + int max = 0; + for ( Position w : a.getChildren()){ + if (max == 0 || height(w) > max){ + max = height(w); + } + } + return 1 + max; } @Override - public Iterator iterator() { - return null; + public String toString() { + return super.toString(); } - - - } diff --git a/tree/TreeNode.java b/tree/TreeNode.java index 9c2c1d5..87477a1 100644 --- a/tree/TreeNode.java +++ b/tree/TreeNode.java @@ -1,5 +1,6 @@ package tree; +import position.NodePositionList; import position.Position; import position.PositionList; @@ -13,13 +14,15 @@ public class TreeNode implements TreePosition { private E element = null; private TreePosition parent = null; - private PositionList> children= null; + private PositionList> children= new NodePositionList>(); public TreeNode(E in_element, TreePosition in_parent, PositionList> in_children ){ element = in_element; parent = in_parent; - children = in_children; + if (in_children != null){ + children = in_children; + } } @@ -35,7 +38,9 @@ public class TreeNode implements TreePosition { @Override public void setChildren(PositionList> c) { - children = c; + if (c != null){ + children = c; + } } @Override