-1

运行程序时出现异常。它编译好并且它之前工作过,所以我不确定出了什么问题。错误: 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 + "   ");
    }
}  
4

2 回答 2

0

p.start(args[0]);是您基于错误的问题。你没有向它传递任何论据。

于 2013-10-25T02:55:53.617 回答
0

它说“在 PrintSideWays.main(PrintSideWays.java:8)”,这意味着错误出在程序的 main 方法中。并且错误位于第 8 行。这意味着它们是这行代码中的错误:p.start(args[0]) 因此,如果 args[0] 是数组越界异常,则它一定不存在。

于 2013-10-25T02:57:16.307 回答