C语言 用二叉链表构建四层满二叉树

用二叉链表构建如图二叉树

进行如下输入

1
2
4
8
0
0
9
0
0
5
10
0
0
11
0
0
3
6
12
0
0
13
0
0
7
14
0
0
15
0
0
View Code

代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 struct binaryTree{
 4     int data;
 5     struct binaryTree *lchild,*rchild;
 6     };
 7 int main()
 8 {
 9     struct binaryTree *head,*create();
10     
11     head=create();
12     
13     printf("首结点地址为:%p\n",head);
14     
15     printf("%d",head->lchild->rchild->lchild->data);/*输出10*/
16     
17     system("pause");
18     return 0;
19     
20 }
21 struct binaryTree *create()
22 {
23     int data;
24     struct binaryTree *p;
25     
26     printf("输入数据:");
27     scanf("%d",&data);
28     
29     p=(struct binaryTree *)malloc(sizeof(struct binaryTree));
30     
31     if(data==0)
32         p=NULL;
33     else{
34         p->data=data;
35         p->lchild=create();
36         p->rchild=create();
37     }        
38     
39     return p;
40 }

 

posted @ 2017-02-07 17:29  kslee  阅读(638)  评论(0)    收藏  举报