CMSC 132A Lecture Notes - Lecture 34: Visitor Pattern
CMSC132A Lccture 34: Midterm 2 Review
The visitor pattern has as many type parameters as your source.
Class BTExamples{
Void testBTInOrder( tester.Tester t){
BT<Integer> i=new Leaf<>();
BT<Integer> t1=
New Node<>(4, new Node<>(2,
New Node<>(1,1,i),
New Node<>(3,i,i)),
New Leaf<>());
Listof<Integer> li= new Cons<>(i, new Cons<>(2, new Cons<>(3,
New Cons<>(4, new Empty<>())))));
t.checkExpect(i.accept(new BSTInOrder<>()), new Empty<>());
t.checkExpect(t1.accept(new BTInOrder<>()), t1);
}
}
Class BTInOrder<T> implements BTVisitor<T, Listof<T>> {
Public Listof<T> visitLeaf(Leaf<T> l) {
…
}
}
What if we are given an element in a list and we want the position in the list?