completato binary tree e implementato euler tour astratto + una sua implementazione per computazione aritmetica
This commit is contained in:
41
euler_tour/EulerTour.java
Normal file
41
euler_tour/EulerTour.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package euler_tour;
|
||||
|
||||
import tree.binarytree.*;
|
||||
import position.Position;
|
||||
|
||||
|
||||
|
||||
public abstract class EulerTour<E, R> {
|
||||
protected BinaryTree<E> tree;
|
||||
|
||||
public abstract R execute(BinaryTree<E> T);
|
||||
|
||||
protected void init(BinaryTree<E> T) { tree = T; }
|
||||
|
||||
protected R eulerTour(Position<E> v) {
|
||||
TourResult<R> r = new TourResult<R>();
|
||||
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<E> v, TourResult<R> r) {}
|
||||
|
||||
protected void visitBelow(Position<E> v, TourResult<R> r) {}
|
||||
|
||||
protected void visitRight(Position<E> v, TourResult<R> r) {}
|
||||
|
||||
|
||||
public class TourResult<R> {
|
||||
public R left;
|
||||
public R right;
|
||||
public R out;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
16
euler_tour/expressions/AdditionOperator.java
Normal file
16
euler_tour/expressions/AdditionOperator.java
Normal file
@@ -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("+"); }
|
||||
|
||||
}
|
||||
30
euler_tour/expressions/EvaluateExpressionTour.java
Normal file
30
euler_tour/expressions/EvaluateExpressionTour.java
Normal file
@@ -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<ExpressionTerm,Integer> {
|
||||
|
||||
public Integer execute(BinaryTree<ExpressionTerm> T) {
|
||||
init(T);
|
||||
return eulerTour(tree.root());
|
||||
}
|
||||
|
||||
protected void visitRight(Position<ExpressionTerm> v, TourResult<Integer> r) {
|
||||
ExpressionTerm term = v.element();
|
||||
if (tree.isInternal(v)) {
|
||||
ExpressionOperator op = (ExpressionOperator) term;
|
||||
op.setOperands(r.left, r.right);
|
||||
}
|
||||
r.out = term.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
17
euler_tour/expressions/ExpressionOperator.java
Normal file
17
euler_tour/expressions/ExpressionOperator.java
Normal file
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
15
euler_tour/expressions/ExpressionTerm.java
Normal file
15
euler_tour/expressions/ExpressionTerm.java
Normal file
@@ -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("");
|
||||
}
|
||||
}
|
||||
18
euler_tour/expressions/ExpressionVariable.java
Normal file
18
euler_tour/expressions/ExpressionVariable.java
Normal file
@@ -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(); }
|
||||
|
||||
|
||||
}
|
||||
45
euler_tour/expressions/StringExpressionTour.java
Normal file
45
euler_tour/expressions/StringExpressionTour.java
Normal file
@@ -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<ExpressionTerm,String> {
|
||||
|
||||
public String execute(BinaryTree<ExpressionTerm> T) {
|
||||
init(T);
|
||||
return eulerTour(T.root());
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected void visitLeft(Position<ExpressionTerm> v, TourResult<String> r) {
|
||||
if (tree.isInternal(v)) {
|
||||
r.out = new String("(");
|
||||
}else {
|
||||
r.out = new String("");
|
||||
}
|
||||
}
|
||||
|
||||
protected void visitBelow(Position<ExpressionTerm> v, TourResult<String> r) {
|
||||
if (tree.isInternal(v)) {
|
||||
r.out += r.left;
|
||||
}
|
||||
r.out += v.element();
|
||||
}
|
||||
|
||||
protected void visitRight(Position<ExpressionTerm> v,TourResult<String> r) {
|
||||
if (tree.isInternal(v)) {
|
||||
r.out += r.right;
|
||||
r.out += ")";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package binarytree;
|
||||
package tree.binarytree;
|
||||
|
||||
public class BTNode<E> implements BTPosition<E> {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package binarytree;
|
||||
package tree.binarytree;
|
||||
|
||||
|
||||
import position.Position;
|
||||
@@ -1,4 +1,4 @@
|
||||
package binarytree;
|
||||
package tree.binarytree;
|
||||
|
||||
|
||||
import exceptions.BoundaryViolationException;
|
||||
@@ -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<E> implements BinaryTree<E> {
|
||||
inorderPositions(right(v), pos);
|
||||
}
|
||||
|
||||
protected void postorderPositions(Position<E> v, PositionList<Position<E>> 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<Position<E>> t = new NodePositionList<Position<E>>();
|
||||
postorderPositions(root(),t);
|
||||
|
||||
for (Position<E> w :t){
|
||||
to_return+=w.element() + ",";
|
||||
}
|
||||
to_return = to_return.substring(0, to_return.length()-1);
|
||||
|
||||
return to_return+= "]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user