这是个创建二叉树的递归程序
public Node2 setup(int h,int t,int w)
{
Node2 tree;
if (index >= treeLine.length) return tree = null;
if (treeLine[index] == '#') return tree = null;
if (treeLine[index] == ' ') return tree = null;
tree = new Node2(treeLine[index],t,h*50); //treeLine是字符串
index = index * 2 + 1;
tree.left = setup(h+1,t-w,w/2); index ++;
tree.right = setup(h+1,t+w,w/2); index = index / 2 - 1;
return tree;
}
这个是Node2类
class Node2
{
static char ch;
static Node2 left;
static Node2 right;
static int x;
static int y; public Node2(char c,int x1,int y1)
{
this.ch = c;
this.left = null;
this.right = null;
this.x = x1;
this.y = y1;
}
}我在其他类里调用setup方法得到的节点总是最后一个创建的节点。。为什么啊~?不应该是第一个根节点嘛
public Node2 setup(int h,int t,int w)
{
Node2 tree;
if (index >= treeLine.length) return tree = null;
if (treeLine[index] == '#') return tree = null;
if (treeLine[index] == ' ') return tree = null;
tree = new Node2(treeLine[index],t,h*50); //treeLine是字符串
index = index * 2 + 1;
tree.left = setup(h+1,t-w,w/2); index ++;
tree.right = setup(h+1,t+w,w/2); index = index / 2 - 1;
return tree;
}
这个是Node2类
class Node2
{
static char ch;
static Node2 left;
static Node2 right;
static int x;
static int y; public Node2(char c,int x1,int y1)
{
this.ch = c;
this.left = null;
this.right = null;
this.x = x1;
this.y = y1;
}
}我在其他类里调用setup方法得到的节点总是最后一个创建的节点。。为什么啊~?不应该是第一个根节点嘛
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货