Skip to content

Commit 3aa1b38

Browse files
committed
add for binary tree preoreder inorder postoreder levleorder destroy
tree ; you can mtrace a.out 1,txt check memory
1 parent 96d3080 commit 3aa1b38

File tree

3 files changed

+328
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)