二叉树的建立
我采用的是递归方式。。不料二叉树没有连接起来,想问原因,这样子C语言可以写成功。。
int index = 0;
void CreateBiTree(BiTree T, String str) {
char ch;
if (index < str.length()) {
ch = str.charAt(index);
index++;
} else {
return;
}
if (ch == '#') {
T = null;
} else {
T = new BiTree();
T.data = ch;
CreateBiTree(T.lchild, str);
CreateBiTree(T.rchild, str);
}
下面是类的成员变量和初始化函数
char data;
BiTree lchild, rchild;
/**
* 初始化
*/
void Init(BiTree T) {
T = null; // 初始化
}
主函数
public class Test {
public static void main(String[] args) {
BiTree T = new BiTree();
T.Init(T);
T.CreateBiTree(T, "ABDH#K###E##CFI###G#J##");
System.out.println("");
}
}
若有回复,感激不尽。