Java Reference
In-Depth Information
class NodeData {
String word;
public NodeData(String w) {
word = w;
}
public int compareTo(NodeData d) {
return this.word.compareTo(d.word);
}
public void visit() {
System.out.printf("%s ", word);
}
} //end class NodeData
class TreeNode {
NodeData data;
TreeNode left, right, parent;
public TreeNode(NodeData d) {
data = d;
left = right = parent = null;
}
} //end class TreeNode
//The BinaryTree class - only the methods relevant to this problem are shown
class BinaryTree {
TreeNode root;
public BinaryTree() {
root = null;
}
public void inOrderTraversal() {
if (root == null) return;
//find first node in in-order
TreeNode node = root;
while (node.left != null) node = node.left;
while (node != null) {
node.data.visit(); //from the NodeData class
node = inOrderSuccessor(node);
}
} //end inOrderTraversal
private static TreeNode inOrderSuccessor(TreeNode node) {
if (node.right != null) {
node = node.right;
while (node.left != null) node = node.left;
return node;
}
Search WWH ::




Custom Search