riordinato alcuni package e creato una prima implementazione di set e mergetemplate
This commit is contained in:
41
utility/euler_tour/EulerTour.java
Normal file
41
utility/euler_tour/EulerTour.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package utility.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
utility/euler_tour/expressions/AdditionOperator.java
Normal file
16
utility/euler_tour/expressions/AdditionOperator.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package utility.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
utility/euler_tour/expressions/EvaluateExpressionTour.java
Normal file
30
utility/euler_tour/expressions/EvaluateExpressionTour.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package utility.euler_tour.expressions;
|
||||
|
||||
import utility.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
utility/euler_tour/expressions/ExpressionOperator.java
Normal file
17
utility/euler_tour/expressions/ExpressionOperator.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package utility.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
utility/euler_tour/expressions/ExpressionTerm.java
Normal file
15
utility/euler_tour/expressions/ExpressionTerm.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package utility.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
utility/euler_tour/expressions/ExpressionVariable.java
Normal file
18
utility/euler_tour/expressions/ExpressionVariable.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package utility.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
utility/euler_tour/expressions/StringExpressionTour.java
Normal file
45
utility/euler_tour/expressions/StringExpressionTour.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package utility.euler_tour.expressions;
|
||||
|
||||
import utility.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 += ")";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
80
utility/merge/MergeTemplate.java
Normal file
80
utility/merge/MergeTemplate.java
Normal file
@@ -0,0 +1,80 @@
|
||||
package utility.merge;
|
||||
|
||||
import exceptions.BoundaryViolationException;
|
||||
import exceptions.EmptyListException;
|
||||
import exceptions.InvalidPositionException;
|
||||
import position.NodePositionList;
|
||||
import position.Position;
|
||||
import position.PositionList;
|
||||
import utility.DefaultComparator;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Created with xgiovio.macbookair.
|
||||
* User: xgiovio
|
||||
* Date: 14/05/14
|
||||
* Time: 14:48
|
||||
*/
|
||||
public abstract class MergeTemplate<E> {
|
||||
|
||||
PositionList<E> s = new NodePositionList<E>();
|
||||
Comparator<E> c = null;
|
||||
|
||||
public MergeTemplate (PositionList<E> A, PositionList <E> B){
|
||||
this(A,B,new DefaultComparator<E>());
|
||||
}
|
||||
|
||||
public MergeTemplate (PositionList<E> A, PositionList <E> B, Comparator<E> in_c){
|
||||
|
||||
c = in_c;
|
||||
E a,b;
|
||||
Iterator<E> Ait, Bit;
|
||||
Ait = A.iterator();
|
||||
Bit = B.iterator();
|
||||
|
||||
while (!(Ait.hasNext()) && !(Bit.hasNext())) {
|
||||
|
||||
a = Ait.next();
|
||||
b= Bit.next();
|
||||
|
||||
if ( c.compare(a,b) < 0 )
|
||||
aIsLess(a);
|
||||
else if (c.compare(a,b) > 0)
|
||||
bIsLess(b);
|
||||
else // se b = a
|
||||
bothAreEqual(a, b);
|
||||
|
||||
}
|
||||
while (!(Ait.hasNext())) {
|
||||
a = Ait.next();
|
||||
aIsLess(a);
|
||||
}
|
||||
while (!(Bit.hasNext())) {
|
||||
b = Bit.next();
|
||||
bIsLess(b);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
abstract protected void aIsLess (E a);
|
||||
|
||||
abstract protected void bIsLess (E b);
|
||||
|
||||
abstract protected void bothAreEqual (E a, E b);
|
||||
|
||||
|
||||
public PositionList<E> getResult () {
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user