大神帮帮我啊!!!

顽皮猫喵 2017-09-02 03:22:34
我写的一个生成二叉树的简单代码:
package com.mzy;

import java.util.Scanner;

public class Binary_Tree {
private Binary_Tree LeftChild;
private Binary_Tree RightChild;
private String value;

/**
* 利用前序遍历的方式创建
*/
static void CreateBitTree(Binary_Tree tree){
//输入value
Scanner scanner = new Scanner(System.in);
String c;
c=scanner.next();

// 根据输入的值来判断插入的节点 输入#为null节点
if(c.equals("#")){
tree = null;
}else{
tree = new Binary_Tree();
tree.setValue(c);
CreateBitTree(tree.getLeftChild());
CreateBitTree(tree.getRightChild());
}

}







public Binary_Tree getLeftChild() {
return LeftChild;
}

public void setLeftChild(Binary_Tree leftChild) {
LeftChild = leftChild;
}

public Binary_Tree getRightChild() {
return RightChild;
}

public void setRightChild(Binary_Tree rightChild) {
RightChild = rightChild;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}



main中:
package com.mzy;

public class Test {
public static void main(String[] args) {
Binary_Tree binary_tree = new Binary_Tree();
Binary_Tree.CreateBitTree(binary_tree);

System.out.println(11111);
}
}



可是为啥返回的二叉树总是null啊??!!??
...全文
249 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
自由自在_Yu 2017-09-05
  • 打赏
  • 举报
回复
引用 5 楼 m15527097561 的回复:
[quote=引用 2 楼 yuxiangaaaaa 的回复:] 代码逻辑有问题呀,方法没有返回值,怎么能传递到Test类里面呢 要么把main方法写到Binary_Tree,定义一个全局变量Binary_Tree tree, 要么就在方法添加返回值 static Binary_TreeCreateBitTree(Binary_Tree tree){ return tree; }
可是,我在方法里面改变了引用的值了啊。就像是void swap()方法一样的。 应该是可以改变值的 啊?![/quote] 这样是可以改值的,只是你的创建树方法有问题,死循环了,一直就不能执行结束。。。 下面是创建一个节点的

    /**
     * 利用前序遍历的方式创建
     */
    static void CreateBitTree(Binary_Tree tree){
        //输入value
        Scanner scanner = new Scanner(System.in);
        String c;
        c = scanner.next();
        // 根据输入的值来判断插入的节点  输入#为null节点
        if(c.equals("#")){
            tree = null;
        }else{
            tree.setValue(c);
            
            Binary_Tree lefTree = new Binary_Tree();
            c = scanner.next();
            lefTree.setValue(c);
            tree.setLeftChild(lefTree);
            //CreateBitTree(tree.getLeftChild());
            
            Binary_Tree rightTree = new Binary_Tree();
            c = scanner.next();
            rightTree.setValue(c);
            tree.setRightChild(rightTree);
            //CreateBitTree(tree.getRightChild());
        }

    }
public static void main(String[] args) {
	Binary_Tree binary_tree = new Binary_Tree();
        Binary_Tree.CreateBitTree(binary_tree);
        System.out.println(binary_tree.getValue());
        System.out.println(binary_tree.getLeftChild().getValue());
        System.out.println(binary_tree.getRightChild().getValue());        
        System.out.println(11111);
	}
自由自在_Yu 2017-09-04
  • 打赏
  • 举报
回复
代码逻辑有问题呀,方法没有返回值,怎么能传递到Test类里面呢 要么把main方法写到Binary_Tree,定义一个全局变量Binary_Tree tree, 要么就在方法添加返回值 static Binary_TreeCreateBitTree(Binary_Tree tree){ return tree; }
qq_38386401 2017-09-04
  • 打赏
  • 举报
回复
好好看下你写的代码逻辑吧,很明显啊,看不出来就调试下
顽皮猫喵 2017-09-04
  • 打赏
  • 举报
回复
引用 2 楼 yuxiangaaaaa 的回复:
代码逻辑有问题呀,方法没有返回值,怎么能传递到Test类里面呢 要么把main方法写到Binary_Tree,定义一个全局变量Binary_Tree tree, 要么就在方法添加返回值 static Binary_TreeCreateBitTree(Binary_Tree tree){ return tree; }
可是,我在方法里面改变了引用的值了啊。就像是void swap()方法一样的。 应该是可以改变值的 啊?!
顽皮猫喵 2017-09-04
  • 打赏
  • 举报
回复
引用 1 楼 qq_38386401 的回复:
好好看下你写的代码逻辑吧,很明显啊,看不出来就调试下
可是,我在方法里面改变了引用的值了啊。就像是void swap()方法一样的。 应该是可以改变值的 啊?!
顽皮猫喵 2017-09-04
  • 打赏
  • 举报
回复
可是,我在方法里面改变了引用的值了啊。就像是void swap()方法一样的。 应该是可以改变值的 啊?!

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧