-1

我有一个项目,它是“从 tree.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();
}

}

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

4

1 回答 1

2
Tree[] forest = new Tree[10];

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

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

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

于 2013-11-10T03:32:13.393 回答