forked from yzhu798/CodingInterviewsNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytree.c
More file actions
213 lines (180 loc) · 3.81 KB
/
binarytree.c
File metadata and controls
213 lines (180 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*************************************************************************
> File Name: binarytree.c
> Author: jinshaohui
> Mail: [email protected]
> Time: 18-11-12
> Desc:
************************************************************************/
#include<assert.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include"list_queue.h"
typedef struct _treenode
{
int data;
struct _treenode *lchild;
struct _treenode *rchild;
}Tnode,Tree;
void binarytree_create(Tree **Root)
{
int a = 0;
printf("\r\n输入节点数值((当输入为100时,当前节点创建完成))):");
scanf("%d",&a);
if (a == 100)
{
*Root = NULL;
}
else
{
*Root = (Tnode *)malloc(sizeof(Tnode));
if (*Root == NULL)
{
return;
}
(*Root)->data = a;
printf("\r\n create %d 的左孩子:",a);
binarytree_create(&((*Root)->lchild));
printf("\r\n create %d 的右孩子:",a);
binarytree_create(&((*Root)->rchild));
}
return ;
}
void binarytree_destory(Tree *root)
{
if (root == NULL)
{
return;
}
binarytree_destory(root->lchild);
binarytree_destory(root->rchild);
free(root);
}
/*先序遍历:根结点--》左子树---》右子树*/
void binarytree_preorder(Tree *root)
{
if (root == NULL)
{
return;
}
printf(" %d ",root->data);
binarytree_preorder(root->lchild);
binarytree_preorder(root->rchild);
return;
}
/*中序遍历:左子树--》跟节点---》右子树*/
void binarytree_inorder(Tree *root)
{
if (root == NULL)
{
return;
}
binarytree_inorder(root->lchild);
printf(" %d ",root->data);
binarytree_inorder(root->rchild);
return;
}
/*后序遍历:左子树---》右子树-》根节点*/
void binarytree_postorder(Tree *root)
{
if (root == NULL)
{
return;
}
binarytree_postorder(root->lchild);
binarytree_postorder(root->rchild);
printf(" %d ",root->data);
return;
}
void binarytree_levelorder(Tree * root)
{
list_queue *queue = NULL;
Tnode * node = NULL;
if(root == NULL)
{
return;
}
queue = list_queue_create();
/*根节点先入队*/
list_queue_enqueue(queue,(void *)root);
while(!list_queue_is_empty(queue))
{
list_queue_dequeue(queue,(void *)&node);
printf(" %d ",node->data);
if(node->lchild != NULL)
{
list_queue_enqueue(queue,(void *)node->lchild);
}
if(node->rchild != NULL)
{
list_queue_enqueue(queue,(void *)node->rchild);
}
}
free(queue);
}
/*打印叶子节点*/
void binarytree_printfleaf(Tree *root)
{
if (root == NULL)
{
return;
}
if ((root->lchild == NULL) && (root->rchild == NULL))
{
printf(" %d ",root->data);
}
else
{
binarytree_printfleaf(root->lchild);
binarytree_printfleaf(root->rchild);
}
}
/*打印叶子的个数*/
int binarytree_getleafnum(Tree*root)
{
if (root == NULL)
{
return 0;
}
if ((root->lchild == NULL) && (root->rchild == NULL))
{
return 1;
}
return binarytree_getleafnum(root->lchild) + binarytree_getleafnum(root->rchild);
}
/*打印数的高度*/
int binarytree_gethigh(Tree *root)
{
int lhigh = 0;
int rhigh = 0;
if (root == NULL)
{
return 0;
}
lhigh = binarytree_gethigh(root->lchild);
rhigh = binarytree_gethigh(root->rchild);
return ((lhigh > rhigh)?(lhigh + 1):(rhigh + 1));
}
int main()
{
Tree *root = NULL;
setenv("MALLOC_TRACE","1.txt",1);
mtrace();
printf("\r\n创建二叉树:");
binarytree_create(&root);
printf("\r\n先序遍历二叉树:");
binarytree_preorder(root);
printf("\r\n中序遍历二叉树:");
binarytree_inorder(root);
printf("\r\n后序遍历二叉树:");
binarytree_postorder(root);
printf("\r\n层次遍历二叉树:");
binarytree_levelorder(root);
printf("\r\n打印二叉树叶子节点:");
binarytree_printfleaf(root);
printf("\r\n打印二叉树叶子节点个数:%d",binarytree_getleafnum(root));
printf("\r\n打印二叉树高度:%d",binarytree_gethigh(root));
binarytree_destory(root);
muntrace();
return 0;
}