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);
}
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);
}