用扩展的先行路径构造二叉树,根节点的地址用函数值返回。
例如
输入AB#DF##G##C##,如下图所示创建二叉树。
二叉树. png
输出该二叉树的先行遍历序列ABDFGC。
# include stdio.h # include stdlib.htypedefcharelementtype; typedefstructbitnode { elementtypedata; struct BiTNode* lchild; struct BiTNode* rchild; }BiTNode,* BiTree; BiTree CreatBinTree (; voidpreorder(bitreet; int main () { BiTree T=CreatBinTree ); preorder(t; 返回0; }voidpreorder(bitreet ) if ) t ) printf )、T-data ); preorder(t-lchild ); preorder(t-rchild ); }}BiTree CreatBinTree () ) { char ch; BiTree T; scanf('%c ',ch ); if(ch=='# ' ) /如果当前字符为#,则指示当前节点为空,返回空值; t=(bitree* ) malloc (sizeof ) bitree ); T-data=ch; T-lchild=CreatBinTree (; 调用//函数以创建左子T-rchild=CreatBinTree (; 调用//函数创建右边的子级return T; () ) ) ) )。