运行程序时出现异常。它编译好并且它之前工作过,所以我不确定出了什么问题。错误: http: //gyazo.com/091235db6cc37154e8f7304e4c1482a8
它应该打印出横向树中给出的序列。
import java.io.*;
import java.util.Scanner;
public class PrintSideWays{
public static void main (String[] args){
PrintSideWays p = new PrintSideWays();
p.start(args[0]);
}
public void start (String args){
buildTree(args);
}
public void buildTree(String preorder){
BinarySearchTree<StringItem, String> bst = new BinarySearchTree<StringItem, String>();
StringItem stringItem;
System.out.println("Sideways printing tree by wcor690:");
for ( int i=0; i < preorder.length() ; i++){
String letter = preorder.substring(i,i+1);
stringItem = new StringItem(letter);
bst.insert(stringItem);
}
TreeNode root = bst.getRoot();
print(root, " ");
System.out.print("Inorder traversal:");
inorderTraversal(root);
System.out.println();
System.out.println("Please enter the remove sequence: ");
Scanner in = new Scanner(System.in);
String userRemove = in.next();
for(int i = 0; i < userRemove.length(); i++){
String letter = userRemove.substring(i,i+1);
StringItem removeString = new StringItem(letter);
try{ bst.delete(removeString);
}
catch(TreeException e){
System.out.println(letter + " not found in tree.");
}
}
print(root, " ");
System.out.print("Inorder traversal:");
inorderTraversal(root);
System.out.println();
}
public void inorderTraversal(TreeNode node){
if(node == null){
return;
}
inorderTraversal(node.getLeft());
System.out.print(node.getItem());
inorderTraversal(node.getRight());
}
public void print(TreeNode treeNode, String indent ) {
if (treeNode == null){
return;
}
print( treeNode.getRight(), indent + " " );
System.out.println(indent + treeNode.getItem());
print(treeNode.getLeft(), indent + " ");
}
}