diff --git a/euler_tour/EulerTour.java b/euler_tour/EulerTour.java new file mode 100644 index 0000000..ff03cbb --- /dev/null +++ b/euler_tour/EulerTour.java @@ -0,0 +1,41 @@ +package euler_tour; + +import tree.binarytree.*; +import position.Position; + + + +public abstract class EulerTour { + protected BinaryTree tree; + + public abstract R execute(BinaryTree T); + + protected void init(BinaryTree T) { tree = T; } + + protected R eulerTour(Position v) { + TourResult r = new TourResult(); + visitLeft(v, r); + if (tree.hasLeft(v)) + r.left = eulerTour(tree.left(v)); + visitBelow(v, r); + if (tree.hasRight(v)) + r.right = eulerTour(tree.right(v)); + visitRight(v, r); + return r.out; + } + + protected void visitLeft(Position v, TourResult r) {} + + protected void visitBelow(Position v, TourResult r) {} + + protected void visitRight(Position v, TourResult r) {} + + + public class TourResult { + public R left; + public R right; + public R out; + } + +} + diff --git a/euler_tour/expressions/AdditionOperator.java b/euler_tour/expressions/AdditionOperator.java new file mode 100644 index 0000000..ed5192b --- /dev/null +++ b/euler_tour/expressions/AdditionOperator.java @@ -0,0 +1,16 @@ +package euler_tour.expressions; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 27/04/2014 + * Time: 13:08 + */ +public class AdditionOperator extends ExpressionOperator { + public Integer getValue() { + return (firstOperand + secondOperand); + } + public String toString() { + return new String("+"); } + +} diff --git a/euler_tour/expressions/EvaluateExpressionTour.java b/euler_tour/expressions/EvaluateExpressionTour.java new file mode 100644 index 0000000..3803cb7 --- /dev/null +++ b/euler_tour/expressions/EvaluateExpressionTour.java @@ -0,0 +1,30 @@ +package euler_tour.expressions; + +import euler_tour.EulerTour; +import position.Position; +import tree.binarytree.BinaryTree; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 27/04/2014 + * Time: 13:09 + */ +public class EvaluateExpressionTour extends EulerTour { + + public Integer execute(BinaryTree T) { + init(T); + return eulerTour(tree.root()); + } + + protected void visitRight(Position v, TourResult r) { + ExpressionTerm term = v.element(); + if (tree.isInternal(v)) { + ExpressionOperator op = (ExpressionOperator) term; + op.setOperands(r.left, r.right); + } + r.out = term.getValue(); + } +} + + diff --git a/euler_tour/expressions/ExpressionOperator.java b/euler_tour/expressions/ExpressionOperator.java new file mode 100644 index 0000000..9159582 --- /dev/null +++ b/euler_tour/expressions/ExpressionOperator.java @@ -0,0 +1,17 @@ +package euler_tour.expressions; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 27/04/2014 + * Time: 13:07 + */ +public class ExpressionOperator extends ExpressionTerm { + + protected Integer firstOperand, secondOperand; + public void setOperands(Integer x, Integer y) { + firstOperand = x; + secondOperand = y; + } + +} diff --git a/euler_tour/expressions/ExpressionTerm.java b/euler_tour/expressions/ExpressionTerm.java new file mode 100644 index 0000000..ceb992a --- /dev/null +++ b/euler_tour/expressions/ExpressionTerm.java @@ -0,0 +1,15 @@ +package euler_tour.expressions; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 27/04/2014 + * Time: 13:06 + */ +public class ExpressionTerm { + + public Integer getValue() { return 0; } + public String toString() { + return new String(""); + } +} diff --git a/euler_tour/expressions/ExpressionVariable.java b/euler_tour/expressions/ExpressionVariable.java new file mode 100644 index 0000000..1dfdee2 --- /dev/null +++ b/euler_tour/expressions/ExpressionVariable.java @@ -0,0 +1,18 @@ +package euler_tour.expressions; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 27/04/2014 + * Time: 13:06 + */ +public class ExpressionVariable extends ExpressionTerm { + + protected Integer var; + public ExpressionVariable(Integer x) { var = x; } + public void setVariable(Integer x) { var = x; } + public Integer getValue() { return var; } + public String toString() { return var.toString(); } + + +} diff --git a/euler_tour/expressions/StringExpressionTour.java b/euler_tour/expressions/StringExpressionTour.java new file mode 100644 index 0000000..701c3ab --- /dev/null +++ b/euler_tour/expressions/StringExpressionTour.java @@ -0,0 +1,45 @@ +package euler_tour.expressions; + +import euler_tour.EulerTour; +import position.Position; +import tree.binarytree.BinaryTree; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 27/04/2014 + * Time: 13:11 + */ +public class StringExpressionTour extends EulerTour { + + public String execute(BinaryTree T) { + init(T); + return eulerTour(T.root()); + } + + + + protected void visitLeft(Position v, TourResult r) { + if (tree.isInternal(v)) { + r.out = new String("("); + }else { + r.out = new String(""); + } + } + + protected void visitBelow(Position v, TourResult r) { + if (tree.isInternal(v)) { + r.out += r.left; + } + r.out += v.element(); + } + + protected void visitRight(Position v,TourResult r) { + if (tree.isInternal(v)) { + r.out += r.right; + r.out += ")"; + } + } + + +} diff --git a/binarytree/BTNode.java b/tree/binarytree/BTNode.java similarity index 97% rename from binarytree/BTNode.java rename to tree/binarytree/BTNode.java index 4fa15be..2e0532b 100644 --- a/binarytree/BTNode.java +++ b/tree/binarytree/BTNode.java @@ -1,4 +1,4 @@ -package binarytree; +package tree.binarytree; public class BTNode implements BTPosition { diff --git a/binarytree/BTPosition.java b/tree/binarytree/BTPosition.java similarity index 93% rename from binarytree/BTPosition.java rename to tree/binarytree/BTPosition.java index a951bfe..e14711c 100644 --- a/binarytree/BTPosition.java +++ b/tree/binarytree/BTPosition.java @@ -1,4 +1,4 @@ -package binarytree; +package tree.binarytree; import position.Position; diff --git a/binarytree/BinaryTree.java b/tree/binarytree/BinaryTree.java similarity index 95% rename from binarytree/BinaryTree.java rename to tree/binarytree/BinaryTree.java index 50f7e57..6bf81d1 100644 --- a/binarytree/BinaryTree.java +++ b/tree/binarytree/BinaryTree.java @@ -1,4 +1,4 @@ -package binarytree; +package tree.binarytree; import exceptions.BoundaryViolationException; diff --git a/binarytree/LinkedBinaryTree.java b/tree/binarytree/LinkedBinaryTree.java similarity index 92% rename from binarytree/LinkedBinaryTree.java rename to tree/binarytree/LinkedBinaryTree.java index 13e0381..014ec91 100644 --- a/binarytree/LinkedBinaryTree.java +++ b/tree/binarytree/LinkedBinaryTree.java @@ -1,4 +1,4 @@ -package binarytree; +package tree.binarytree; import exceptions.BoundaryViolationException; import exceptions.EmptyTreeException; import exceptions.InvalidPositionException; @@ -307,9 +307,29 @@ public class LinkedBinaryTree implements BinaryTree { inorderPositions(right(v), pos); } + protected void postorderPositions(Position v, PositionList> pos) throws InvalidPositionException { + if (hasLeft(v)) + inorderPositions(left(v), pos); + if (hasRight(v)) + inorderPositions(right(v), pos); + pos.addLast(v); + } + - @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+= "]"; } }