技术学习博2

补个档,第⑨周学的树

难的还是递归,绕来绕去( ´_ゝ` )

 1 //树的定义(结点定义)
 2 typedef char DataType;
 3 typedef struct Node {
 4      DataType  data; 
 5      struct  Node  *lchild; 
 6      struct  Node  *rchild;                   
 7 } BiNode,*BiTree;
 8 
 9 //创建树的二叉链表(递归)
10 void CreateBiTree(BiTree *bt)
11 {
12     char ch;
13     ch = getchar();
14     if(ch=='.') *bt=NULL;
15     else 
16     {
17         *bt= (BiNode *)malloc(sizeof(BiNode)); 
18         (*bt)->data=ch;
19          CreateBiTree(&((*bt)->lchild)); //生成左子树
20          CreateBiTree(&((*bt)->rchild)); //生成右子树
21     }
22 }
23 
24 //输出二叉树的元素(先序)
25 void Print(BiTree bt)
26 {
27     if(bt==NULL)    
28         return;
29     else
30     {
31         printf("%c ", bt->data);
32         Print(bt->lchild);
33         Print(bt->rchild);
34     }
35 
36 }
37 
38 //主函数
39 void main()
40 {
41     BiTree T;
42     CreateBiTree(&T);
43     Print(T);
44     
45 }

 

posted @ 2020-05-10 15:37  琥珀色的忧郁  阅读(177)  评论(0编辑  收藏  举报