我是采用递归方式建立的二叉树
贴上代码:
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("");
}
}
若有回复,感激不尽。