二叉树链式结构建立的传值问题 本帖最后由 weiyingguo 于 2013-03-25 10:24:20 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class Node { public char data; public Node lchild, rchild; public int ltag, rtag; public Node(char data) { this(data, null, 0, null, 0); } public Node(char data, Node lchild, int ltag, Node rchild, int rtag) { this.data = data; this.lchild = lchild; this.ltag = ltag; this.rchild = rchild; this.rtag = rtag; }} 因为java是值传递,这样在方法内试图给引用变量(形参)赋值,对实参无效create(T.lchild); 不知道你是有心还是无心,你的代码已经变成了递归调用了....System.in.read();当你输入8加回车,产生了3个字符,8+13+10就是回车变成了13+10所以:char tr = (char)System.in.read(); System.in.read(); System.in.read();---->这里要多加一个System.in.read();过滤了回车健.总之,混乱啊....不知道你想要什么样的效果public class ThreadedTree { public Node root; public ThreadedTree() throws IOException{ this.root = create(); System.out.print(root.data + root.lchild.toString()); } private Node create() throws IOException { Node T = new Node(' '); char tr = (char)System.in.read(); System.in.read(); System.in.read(); if(tr == '#') T = null; else { T.data = tr; System.out.print("Please input " + T.data + "'s left child: "); T.lchild=create(); System.out.print("Please input " + T.data + "'s right child: "); T.rchild=create(); //System.out.print( T.lchild.data); } } } return T; } public static void main(String[] args) { try { new ThreadedTree(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }} 我是想通过递归调用的方式给二叉树复制,我是想用System.in.read();进行char字符的读取,至于多的两个System.in.read()是用来吸收产生的空格,防止把空格赋给节点。我还是菜鸟,代码确实很乱,希望大家多多指教 那怎么才能使root作为根节点开辟一棵树呢? 像根节点那样通过调用返回值对实参赋值就行T.lchild = create(T.lchild);T.rchild = create(T.rchild); 看的一本书上代码,问题见注释 帮忙看一下,为什么这样的代码,会有0输出?? 请教一下 我刚刚开始练习作JAVA程序,用什么可视化开发工具好呢? 谁有空!!帮忙把这几个题做一下吧!!!!谢谢!!!!!!!! 有偿转让Java即时通信工具源代码及设计文档 JAVA基础问题,求各位大哥帮忙指点一下!!! 关于equals的简单问题 关于wait,notify用法 c/s与b/s之间如何通信 哪位大侠帮忙解读下这个程序 Runtime脱钩的问题
public char data;
public Node lchild, rchild;
public int ltag, rtag;
public Node(char data) { this(data, null, 0, null, 0); }
public Node(char data, Node lchild, int ltag, Node rchild, int rtag) {
this.data = data;
this.lchild = lchild;
this.ltag = ltag;
this.rchild = rchild;
this.rtag = rtag;
}
}
因为java是值传递,这样在方法内试图给引用变量(形参)赋值,对实参无效
create(T.lchild);
System.in.read();当你输入8加回车,产生了3个字符,
8+13+10就是回车变成了13+10
所以:
char tr = (char)System.in.read();
System.in.read();
System.in.read();---->这里要多加一个System.in.read();过滤了回车健.总之,混乱啊....不知道你想要什么样的效果
public class ThreadedTree {
public Node root;
public ThreadedTree()
throws IOException{
this.root = create();
System.out.print(root.data + root.lchild.toString());
}
private Node create() throws IOException {
Node T = new Node(' ');
char tr = (char)System.in.read();
System.in.read();
System.in.read();
if(tr == '#')
T = null;
else {
T.data = tr;
System.out.print("Please input " + T.data + "'s left child: ");
T.lchild=create();
System.out.print("Please input " + T.data + "'s right child: ");
T.rchild=create();
//System.out.print( T.lchild.data); } }
}
return T;
}
public static void main(String[] args) {
try {
new ThreadedTree();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
T.rchild = create(T.rchild);