二叉树基础

一.基本概念

https://www.jianshu.com/p/bf73c8d50dc2 的3.8之前

二.基础代码实施

1.建树

 1 Tree *CreatTree(Tree *root){
 2     int c;
 3     c = getchar();
 4     if(c == '#') root = NULL;//是特殊字符则没有叶子节点
 5     else {//从左至右建树 6         root = new Tree;
 7         root->date = c;
 8         root->left = CreatTree(root->left);
 9         root->right = CreatTree(root->right);
10     }
11     return root;
12 }

 

如:构造下列二叉树


那么我们的输入应该是: ABDG##H###CE#I##F##

 

解析:

 

从A->G:未遇见特殊字符'#',依次建立左子树.

 

G->H:G后读入特殊字符'#',则G左孩子为空;

 

继续读入下一个字符;

 

依然是'#',则G右孩子为空.

 

此时G左右孩子已输入,返回上一层D.

 

读入字符'H'写入D右孩子.

 

其他的以此类推即可

2.遍历

(1)前序遍历

双亲->左->右

 

1 void PreOrder(Tree *root) {
2     if(root == NULL) return;
3     putchar(root->date);//输出等操作
4     PreOrder(root->left);
5     PreOrder(root->right);
6 }

 

依照上图前序遍历结果:

A B D G H C E I F

(2)中序遍历

左->双亲->右

 

1 void InOrder(Tree *root) {
2     if(root == NULL) return;
3     InOrder(root->left);
4     putchar(root->date);
5     InOrder(root->right);
6 }

 

中序遍历结果:
G D H B A E I C F

(3)后序遍历

左->右->双亲

 

1 void PostOrder(Tree *root) {
2     if(root == NULL) return;
3     PostOrder(root->left);
4     PostOrder(root->right);
5     putchar(root->date);
6 }

 

后序遍历结果:
G H D B I E F C A

3.销毁

 

1 void DesTree(Tree *root)
2 {
3     if(root == NULL) return ;
4     DesTree(root->left);
5     DesTree(root->right);
6     free(root);         //从最右依次free至根节点
7 }

 

完整代码

 

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 #define ll long long
 5 #define pi acos(-1)
 6 #define mod 1000000007
 7 
 8 typedef struct TREE{
 9     char date;
10     struct TREE *left, *right;
11 }Tree;
12 
13 Tree *CreatTree(Tree*);
14 void PreOrder(Tree*);
15 void InOrder(Tree*);
16 void PostOrder(Tree*);
17 void DesTree(Tree *root);
18 
19 int main() {
20     Tree *root = NULL;
21     root = CreatTree(root);
22     PreOrder(root);
23     printf("\n");
24     InOrder(root);
25     printf("\n");
26     PostOrder(root);
27     DesTree(root);
28     return 0;
29 }
30 //建树
31 Tree *CreatTree(Tree *root){
32     int c;
33     c = getchar();
34     if(c == '#') root = NULL;
35     else {
36         root = new Tree;
37         root->date = c;
38         root->left = CreatTree(root->left);
39         root->right = CreatTree(root->right);
40     }
41     return root;
42 }
43 //前序
44 void PreOrder(Tree *root) {
45     if(root == NULL) return;
46     putchar(root->date);
47     PreOrder(root->left);
48     PreOrder(root->right);
49 }
50 //中序
51 void InOrder(Tree *root) {
52     if(root == NULL) return;
53     InOrder(root->left);
54     putchar(root->date);
55     InOrder(root->right);
56 }
57 //后序
58 void PostOrder(Tree *root) {
59     if(root == NULL) return;
60     PostOrder(root->left);
61     PostOrder(root->right);
62     putchar(root->date);
63 }
63 //销毁
64 void DesTree(Tree *root) 65 { 66 if(root == NULL) return ; 67 DesTree(root->left); 68 DesTree(root->right); 69 free(root); 70 } 71 72 //for(int i = 0; i < n; i++)

 

参考博文 https://blog.csdn.net/bestsort/article/details/78784419

https://www.jianshu.com/p/bf73c8d50dc2

 

posted @ 2020-04-05 22:03  湖上的程序员  阅读(110)  评论(0编辑  收藏  举报