创建一个二叉树
从键盘先序扩展一个二叉树
typedef struct Node {
char data;
struct Node *Lchild;
struct Node *Rchild;
}BiTNode, * BiTree;
int lengthOfTree = 0;
BiTree init() {
char s = '#';
scanf("%c", &s);
lengthOfTree++;
BiTree tree;
// ABC##DE#G##F###
if (s == '#') {
tree = NULL;
} else if (s == ' ') {
tree = NULL;
} else{
tree = (BiTree)malloc(sizeof(BiTNode));
tree->data = s;
tree->Lchild = init();
tree->Rchild = init();
}
return tree;
}
int main(void) {
BiTree root = (BiTree)malloc(sizeof(BiTNode));
root = init();
return 0;
}
先序遍历
递归
先访问跟节点
然后左子树
然后右子树
void PreOrder(BiTree root) {
if (root) {
printf("%c", root->data);