Question 1:
===========
Consider the following two class dictionaries:
The first one, called cd-with-noise,
defines trees with some noise added:
cd-with-noise:
BSTInt : EmptyInt | NodeInt.
NodeInt = "(" int N1 ** N2 ")".
N1 = BSTInt .
N2 = BSTInt .
EmptyInt = "empty".
The second defines "pure" trees:
cd-pure:
BSTInt : NodeInt | EmptyInt.
NodeInt = "(" Integer BSTInt BSTInt ")".
EmptyInt = "()".
We are charged to develop a library of tree processing
routines that work with these two data structures as well as
with several other "tree-like" data structures.
The functions we want to implement are: height, incr, sum.
For each of those functions you get the solution for cd-pure
and you are asked to generalize it so that it works for both
cd-pure and cd-with-noise.
To achieve this impressive flexibility we use DemeterF.
PART 1a
=======
Here is a solution for height which works for class dictionary
cd-pure:
class Height2 extends IDb{
int combine(NodeInt t, int d, int l, int r){ int n= Math.max(l,r) + 1;
System.out.println(" height increased " + n); return n;}
int combine(BSTInt l){ return 0; }
static int height (Object o)
{
return new Traversal(new Height2()). traverse(o);
}
}
PART 1b
=======
Add one or more combine methods to make your program
work for both class dictionary cd-pure and
class dictionary cd-with-noise
as well as for many other class dictionaries.
PART 1c
=======
Give an English description of the kind of class dictionaries for which
your program will work.
PART 2:
=======
Do the same for: incr.
Here is a partial solution for incr which actually works for class dictionary
cd-pure:
class Incr extends IDf{
int apply(int i){ return i+1; }
static BSTInt incr (Object o)
{
return new Traversal(new Incr()). traverse(o);
}
}
Answer parts 1a, 1b and 1c.
Part 3:
=======
Do the same for: sum
class Sum extends IDb{
int combine(NodeInt t, int d, int l, int r){ return d+r+l; }
int combine(BSTInt l){ return 0; }
static int sum (Object o)
{
return new Traversal(new Sum()). traverse(o);
}
}
Answer parts 1a, 1b and 1c.
**