如何在Java中创建树结构?

时间:2014-09-22 17:40:06

标签: java data-structures tree

我正在创建树结构,但问题是在进入显示时它不显示任何内容。也许因为root在这里仍然是null,我无法理解为什么。

public class Tree extends LLQueue {

int data12;
tree left;
tree right;


@Override
public int getdata(){
    return data12;
}

@Override
public void setdata(int data){
    this.data12 =data;
}
public tree getleft(){
    return left;
}
public void setleft(tree left){
    this.left=left;
}
public tree getright(){
    return right;
}
public void setright(tree right){
    this.right =right;
}

public static void main(String args[]) throws IOException, EmptyQueueException{
    BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
    System.out.println("enter option ");
    int n =Integer.parseInt(br.readLine());
     tree root =null;
    do{
        System.out.println("\nQueue Operations");
        System.out.println("1. insert");
        System.out.println("2. display");

        int choice = Integer.parseInt(br.readLine());
        switch(choice){
            case 1:

                System.out.println("insert here");
                int input =Integer.parseInt(br.readLine());
                insertintree(root,input);
                break;

            case 2:
                System.out.println("result is");
                preorder(root);

        }
    }while(n!=0);
}
private static void insertintree(tree root, int input) throws EmptyQueueException {
    LLQueue Q =new LLQueue();
    tree temp = null;
    int temp1;
    tree newnode =new tree();
    newnode.setdata(input);
    newnode.setleft(null);
    newnode.setright(null);

 if(newnode ==null){
     System.out.println("memory error");
     return;
 }
 if(root ==null){
     root =newnode;
     return;
 }
 Q.enQueue(root.getdata());
 while(!Q.isEmpty()){
         temp1=Q.deQueue();
         temp.setdata(temp1);
         if(temp.getleft()!=null){
          tree temp2 =temp.getleft();   

         Q.enQueue(temp2.getdata());
         }
         else 
         {
             temp.setleft(newnode);
             Q.deQueue();
             return;
         }
           if(temp.getright()!=null){
          tree temp2 =temp.getright();   

         Q.enQueue(temp2.getdata());
         }
         else 
         {
             temp.setright(newnode);
             Q.deQueue();
             return;
         }
 }
 Q.deQueue();

}
private static void preorder(tree root) {

    if(root!=null){

        System.out.println(root.getdata());
        preorder(root.getleft());
        preorder(root.getright());
    }
}
}

1 个答案:

答案 0 :(得分:0)

您的问题是您需要返回创建的新节点以将其设置为root。或者您也可以为根节点

公开一个setter

将此代码用于您的方法

private static Tree insertintree(tree root, int input) throws EmptyQueueException {
    LLQueue Q =new LLQueue();
    tree temp = null;
    int temp1;
    tree newnode =new tree();
    newnode.setdata(input);
    newnode.setleft(null);
    newnode.setright(null);
 if(newnode ==null){
     System.out.println("memory error");
     return null;
 } 
     return newnode;
 }

您在main中的代码将更改为

switch(choice){
        case 1:

            System.out.println("insert here");
            int input =Integer.parseInt(br.readLine());
            Tree treeNode = insertintree(root,input);
            if(treeNode!=null && root==null) {root = treeNode;}
            break;