使用二叉树的空指针异常



我有一个项目,它是"从树开始.java程序(清单 8.1),并修改它以创建一个二进制文件用户输入的字母字符串(如 A、B 等)的树。每字母将显示在其自己的节点中。构造树,以便所有节点包含字母的是叶子。父节点可以包含一些非字母符号,如 +。确保每个父节点正好有两个子节点。如果树不平衡,请不要担心。请注意,这不是搜索树;没有快速的方法可以找到给定的节点。

import java.io.*;
import java.util.*;
class Node
{
    public String iData; // data item (key)
    public Node leftChild; // this node’s left child
    public Node rightChild; // this node’s right child
    public void displayNode() // display ourself
    {
        System.out.print('{');
        System.out.print(iData);
        System.out.print("} ");
    }
} // end class Node
class Tree
{
    private Node root; // first node of tree
    public void setNode(Node newNode)
    {root = newNode;}
    public Node getNode()
    {return root;}
// -------------------------------------------------------------
    public Tree() // constructor
    { root = null; } // no nodes in tree yet
// -------------------------------------------------------------
public void traverse(int traverseType)
{
    switch(traverseType)
    {
        case 1: System.out.print("nPreorder traversal: ");
        preOrder(root);
        break;
        case 2: System.out.print("nInorder traversal: ");
        inOrder(root);
        break;
        case 3: System.out.print("nPostorder traversal: ");
        postOrder(root);
        break;
    }
    System.out.println();
}
private void preOrder(Node localRoot)
{
    if(localRoot != null)
    {
        System.out.print(localRoot.iData + " ");
        preOrder(localRoot.leftChild);
        preOrder(localRoot.rightChild);
    }
}
//A function I made to try and get the letters into leaves.
void preOrderLeaves(Node localRoot, Tree[] forest, int i)
{
    if(localRoot != null)
    {
        localRoot.iData = "+";
        localRoot.leftChild.iData = "+";
        localRoot.rightChild = forest[i].getNode();
        preOrderLeaves(localRoot.leftChild, forest, i + 1);
        preOrderLeaves(localRoot.rightChild, forest, i + 1);
    }
}
// -------------------------------------------------------------
private void inOrder(Node localRoot)
{
    if(localRoot != null)
    {
        inOrder(localRoot.leftChild);
        System.out.print(localRoot.iData + " ");
        inOrder(localRoot.rightChild);
    }
}
// -------------------------------------------------------------
private void postOrder(Node localRoot)
{
    if(localRoot != null)
    {
        postOrder(localRoot.leftChild);
        postOrder(localRoot.rightChild);
        System.out.print(localRoot.iData + " ");
    }
}
// -------------------------------------------------------------
public void displayTree()
{
    Stack globalStack = new Stack();
    globalStack.push(root);
    int nBlanks = 32;
    boolean isRowEmpty = false;
    System.out.println(
    "......................................................");
    while(isRowEmpty==false)
    {
        Stack localStack = new Stack();
        isRowEmpty = true;
        for(int j=0; j<nBlanks; j++)
        System.out.print(' ');
        while(globalStack.isEmpty()==false)
        {
            Node temp = (Node)globalStack.pop();
            if(temp != null)
        {
                System.out.print(temp.iData);
                localStack.push(temp.leftChild);
                localStack.push(temp.rightChild);
                if(temp.leftChild != null ||
                        temp.rightChild != null)
                    isRowEmpty = false;
        }
        else
        {
            System.out.print("--");
            localStack.push(null);
            localStack.push(null);
        }
        for(int j=0; j<nBlanks*2-2; j++)
            System.out.print(' ');
        } // end while globalStack not empty
        System.out.println();
        nBlanks /= 2;
        while(localStack.isEmpty()==false)
            globalStack.push( localStack.pop() );
        } // end while isRowEmpty is false
        System.out.println(
        "......................................................");
    } // end displayTree()
        // -------------------------------------------------------------
}

public class Leaves 
{
   //I Tried to create an array of individual trees and then add them to a 
   //larger tree
public static void main(String[] args) 
{
    Tree[] forest = new Tree[10];
    Scanner sc = new Scanner(System.in);
    for(int i = 0; i < 10; i++)
    {
        String letter;
        System.out.println("Enter a letter: ");
        letter = sc.nextLine();
        Node newNode = new Node();
        newNode.iData = letter;
        forest[i].setNode(newNode); //This line causes the null pointer exception
    }
    Tree letterTree = new Tree();
    letterTree.preOrderLeaves(letterTree.getNode(), forest, 0);
    letterTree.displayTree();
}
}

当我尝试将林设置为新节点时,我收到空点异常。请帮忙。

Tree[] forest = new Tree[10];

此行为 Trees 创建一个包含 10 个元素的数组,但不初始化其中任何一个。您可能希望遍历数组并实例化每个元素,如下所示:

for(int i = 0; i < forest.length; ++i)
  fores[i] = new Tree();

我也真诚地希望所有这些代码都不在同一个文件中。尝试将每个类放在不同的文件中。

最新更新