What is a Visitor interface?

S

Steve

I downloaded the source for a Binary Search tree. It contains three files: A
BinaryTreeNode interface, a LinkedBinaryTreeNode implementation of this
interface and the BinarySearchTreeClass.

It includes code that is designed to traverse the tree. Each of the traverse
methods have a reference to something called a "Visitor" (e.g
traverseInorder(Visitor visitor)). Visitor is an interface in the
BinaryTreeNode interface.

What is this "Visitor" interface and how do I use it to traverse the tree?

Here is the relevant part from BinaryTreeNode.java:

/**
* Simple visitor interface.
*/
public interface Visitor {
void visit(BinaryTreeNode node);
}


Here is an example of a traversal method, this is in
LinkedBinaryTreeNode.java:

/**
* Visits the nodes in this tree in inorder.
*/
public void traverseInorder(Visitor visitor) {
if (left != null) left.traverseInorder(visitor);
visitor.visit(this);
if (right != null) right.traverseInorder(visitor);
}
 
A

Alun Harford

Steve said:
I downloaded the source for a Binary Search tree. It contains three files: A
BinaryTreeNode interface, a LinkedBinaryTreeNode implementation of this
interface and the BinarySearchTreeClass.

It includes code that is designed to traverse the tree. Each of the traverse
methods have a reference to something called a "Visitor" (e.g
traverseInorder(Visitor visitor)). Visitor is an interface in the
BinaryTreeNode interface.

What is this "Visitor" interface and how do I use it to traverse the tree?

Put:

visitor design pattern

into your search engine.

Alun Harford
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top