Java Reference
In-Depth Information
Program P8.4
import java.io.*;
import java.util.*;
public class LevelOrderTest {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new FileReader("btree.in"));
BinaryTree bt = new BinaryTree(in);
System.out.printf("\n\nThe level-order traversal is: ");
bt.levelOrderTraversal();
System.out.printf("\n");
in.close();
} // end main
} //end class LevelOrderTest
class NodeData {
String word;
public NodeData(String w) {
word = w;
}
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 BinaryTree(Scanner in) {
root = buildTree(in);
}
Search WWH ::




Custom Search