2013-11-10 26 views
-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(); 
} 

} 

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

回答

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

该行为树创建一个10个元素的数组,但不初始化它们中的任何一个。你可能想通过数组通过迭代和实例的每一个元素,像这样:

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

我也诚恳希望所有的代码是不是在同一个文件。尝试让每个课程在不同的文件中。