我是靠谱客的博主 过时河马,这篇文章主要介绍二叉树(C语言),现在分享给大家,希望可以做个参考。

其中用到了队列 可以用一个循环队列替换 可以翻看之前的博文 ,
这次主要介绍了二叉树的四种遍历以及构建二叉树

复制代码
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
#include<stdio.h> #include<queue> #include<windows.h> using namespace std; typedef struct Tree { int data; struct Tree *lchild; struct Tree *rchild; }Tree; Tree* CreateTree() { Tree* T; T=(Tree*)malloc(sizeof(Tree)); int data1; scanf("%d",&data1); if(data1==-1) { return NULL; } T->data=data1; printf("Please input %d 's Left tree:n",data1); T->lchild=CreateTree(); printf("Please input %d 's Right tree:n",data1); T->rchild=CreateTree(); return T; } void FirstPrintTree(Tree *T) { if(T==NULL) { return ; } printf("%d ",T->data); FirstPrintTree(T->lchild); FirstPrintTree(T->rchild); } void MidnumPrintTree(Tree *T) { if(T==NULL) { return ; } MidnumPrintTree(T->lchild); printf("%d ",T->data); MidnumPrintTree(T->rchild); } void LastPrintTree(Tree *T) { if(T==NULL) { return ; } LastPrintTree(T->lchild); LastPrintTree(T->rchild); printf("%d ",T->data); } void LayerOrder(Tree *T) { queue<Tree*>q; q.push(T); while(!q.empty()) { Tree *flag; flag=q.front(); q.pop(); printf("%d ",flag->data); if(flag->lchild!=NULL) q.push(flag->lchild); if(flag->rchild!=NULL) q.push(flag->rchild); } } int main() { Tree *T; T=CreateTree(); FirstPrintTree(T); printf("n"); MidnumPrintTree(T); printf("n"); LastPrintTree(T); printf("n"); LayerOrder(T); printf("n"); system("pause"); return 0; }

最后

以上就是过时河马最近收集整理的关于二叉树(C语言)的全部内容,更多相关二叉树(C语言)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部