diff --git a/com/xgiovio/LinkedTreeTest.java b/com/xgiovio/LinkedTreeTest.java new file mode 100644 index 0000000..63ec56b --- /dev/null +++ b/com/xgiovio/LinkedTreeTest.java @@ -0,0 +1,46 @@ +package com.xgiovio; + +import tree.LinkedTree; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 23/04/2014 + * Time: 21:46 + */ +public class LinkedTreeTest { + + public static void main(String[] args) { + + LinkedTree a = new LinkedTree(); + System.out.println(a.size()); + System.out.println(a.isEmpty()); + System.out.println(a); + + + a.addRoot(10); + System.out.println(a.size()); + System.out.println(a.isEmpty()); + System.out.println(a); + System.out.println(a.height()); + + a.addChild(15,a.root()); + a.addChild(30,a.root()); + System.out.println(a.size()); + System.out.println(a.isEmpty()); + System.out.println(a); + + a.addChild(100,a.children(a.root()).iterator().next()); + System.out.println(a.size()); + System.out.println(a.isEmpty()); + System.out.println(a); + + System.out.println(a.height()); + + + + + + } + +} diff --git a/tree/LinkedTree.java b/tree/LinkedTree.java index e679d20..6a14524 100644 --- a/tree/LinkedTree.java +++ b/tree/LinkedTree.java @@ -73,9 +73,6 @@ public class LinkedTree implements Tree { return vv.getChildren().isEmpty(); } - - - public boolean isRoot(Position v) throws InvalidPositionException { checkPosition(v); return (v == root()); @@ -100,6 +97,17 @@ public class LinkedTree implements Tree { preorderPositions(w, pos); } + protected void postorderPositions(Position v, PositionList> pos) throws InvalidPositionException { + + TreePosition a = checkPosition(v); + + + for (Position w : a.getChildren()) + postorderPositions(w, pos); + + pos.addLast(a); + } + protected TreePosition checkPosition(Position v) throws InvalidPositionException { if (v == null || !(v instanceof TreePosition) || isEmpty() ) @@ -138,7 +146,7 @@ public class LinkedTree implements Tree { throw new UndeletableNodeException(); E to_return = root().element(); root = null; - size--; + size = 0; return to_return; } @@ -165,21 +173,25 @@ public class LinkedTree implements Tree { } - public int depth ( Position v) { + public int depth ( Position v) throws InvalidPositionException{ TreePosition a = checkPosition(v); if (a.getParent() == null) return 0; return (depth(a.getParent()) + 1); } - public int height ( Position v) { + public int height () throws EmptyTreeException{ + return height_f(root()); + } + + public int height_f ( 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); + if (max == 0 || height_f(w) > max){ + max = height_f(w); } } return 1 + max; @@ -187,6 +199,23 @@ public class LinkedTree implements Tree { @Override public String toString() { - return super.toString(); + String to_return = ""; + to_return = to_return + "["; + if (size() == 0 ) + return to_return+= "]"; + + NodePositionList> t = new NodePositionList>(); + postorderPositions(root(),t); + + for (Position w :t){ + to_return+=w.element() + ","; + } + to_return = to_return.substring(0, to_return.length()-1); + + return to_return+= "]"; } + + + + }