二叉树入门简析

给出一些二叉树入门级(确信)使用方法:

复制代码
 1 //用递归的方式建立一个完全二叉树
 2 void Bulid(int t){
 3     UpdateData(t);  //添加数据
 4     Build(t + t);   //如果子节点存在
 5     Build(t + t + 1);
 6     //用这种方法建立非完全二叉树 会存在空间浪费的问题
 7 }
 8 
 9 //可以用数组下标模拟节点编号,用多个数组来记录节点信息
10 struct TreeNode1{    //可以存储一般二叉树
11     int value;      //但是也会产生浪费的空间,具体用哪个就看情况了
12     int l,r,fa;
13 } a[100001];
14  
15 //也可以与链表类似,用指针存储二叉树的父节点&子节点 同时要记录根节点指针
16 struct TreeNode2{
17     int value;
18     TreeNode2 *l,*r,*fa;
19 };
20 TreeNode2 *root;
21 
22 //以下就以指针操作来简介二叉树的基本操作
23 
24 //新建节点
25 struct TreeNode{
26     int value;
27     TreeNode *l, *r, *fa; //初始为NULL
28     TreeNode(int x){ value = x;}
29 }
30 TreeNode *p = new TreeNode(x);
31 
32 //根节点初始化
33 TreeNode *root;
34 root = new TreeNode(v);
35 
36 //删除节点(暂空)
37 
38 //插入子节点
39 void Insert(TreeNode *fa, TreeNode *p,int flag){
40     //flag = 0 插入到左边
41     //flag = 1 插入到右边
42     if (!flag){
43         fa->l = p;
44     }
45     else{
46         fa->r = p;
47     }
48     p->fa = fa;
49 }
50 
51 TreeNode *p = new TreeNode(v);
52 Insert(fa,p,flag);
53 
54 
55 //二叉树的遍历顺序(先序遍历、中序遍历、后序遍历)<-以根节点的访问时间作为区分
56 
57 //先序遍历(DLR)
58 void PreOrder(TreeNode *p){
59     cout << p->value << endl;
60     if (p->l) PreOrder(p->l);
61     if (p->r) PreOrder(p->r);
62 }
63 Pre Order(root);
64 
65 //中序遍历(LDR)
66 void InOrder(TreeNode *p){
67     if (p->l) InOrder(p->l);
68     cout << p->value << endl;
69     if (p->r) InOrder(p->r);
70 }
71 InOrder(root);
72 
73 //后序遍历(LRD)
74 void PostOrder(TreeNode *p){
75     if (p->l) PostOrder(p->l);
76     if (p->r) PostOrder(p->r);
77     cout << p->value << endl;
78 }
79 PostOrder(root);
80 
81 //层级遍历(BFS序列)(基于队列)
82 TreeNode *q[N];
83 void BFS(TreeNode *root){
84     int front = 1, rear = 1;
85     q[1] = root;
86     while (front <= rear){
87         TreeNode *p = q[front];
88         front++;
89         cout << p->value << endl;
90         if (p->l) q[++rear] = p->l;
91         if (p->r) q[++rear] = p->r;
92     }
93 }
94 BFS(root);
95 
96 //要实现计算每个节点的深度也不难
97 //root->d = 1; 每次++时p->l/r->d=p->d+1;
复制代码

 LCA板子题AC代码:

复制代码
 1 #include<bits/stdc++.h>
 2 #define ios ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
 3 typedef long long ll;
 4 //const ll mod = 1e9+7;
 5 //const ll mod = 1000003;
 6 #define endl "\n"
 7 using namespace std;
 8 
 9 //LCA
10 int n,x,y,c[1001],d[1001],u,v;
11 
12 struct node{
13     int l,r,fa;
14 } a[2001];
15 
16 
17 int main(){
18     ios;
19     cin >> n;
20     for (int i = 1;i <= n;i++){
21         cin >> x >> y;
22         if (x){
23             a[i].l = x;
24             a[x].fa = i;
25         }
26         if (y){
27             a[i].r = y;
28             a[y].fa = i;
29         }
30     }
31     cin >> u >> v;
32     int l1 = 0;
33     while (u != 1){
34         c[++l1] = u, u = a[u].fa;
35     }
36     c[++l1] = 1;
37     int l2 = 0;
38     while (v != 1){
39         d[++l2] = v, v = a[v].fa;
40     }
41     d[++l2] = 1;
42     x = 0;
43     for (int i = l1, j = l2;i && j;i--,j--){
44         if (c[i] == d[j]){
45             x = c[i];
46         }
47         else{
48             break;
49         }
50     }
51     cout << x << endl;
52     return 0;
53 }
复制代码

 二叉树基础操作大全 :

复制代码
  1 #include<bits/stdc++.h>
  2 #define ios ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
  3 using namespace std;
  4 //const ll mod = 1e9+7;
  5 //const ll mod = 1000003;
  6 #define endl "\n"
  7 #define rep(i,a,n) for (int i=a;i<=n-1;i++)
  8 #define per(i,a,n) for (int i=n-1;i>=a;i--)
  9 #define pb push_back
 10 #define mp make_pair
 11 #define all(x) (x).begin(),(x).end()
 12 #define fir first
 13 #define sec second
 14 #define SZ(x) ((int)(x).size())
 15 typedef vector<int> VI;
 16 typedef long long ll;
 17 typedef pair<int,int> PII;
 18 typedef double db;
 19 mt19937 mrand(random_device{}()); 
 20 const ll mod = 998244353;
 21 int rnd(int x) { return mrand() % x;}
 22 ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
 23 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
 24 const int N = 1000001;
 25 /*---------------------------------------*/
 26 
 27 int n, x, y, c[N], d[N], u, v, sum[N], tar;
 28 
 29 struct TreeNode{
 30     int l, r, fa, val, dep;
 31 } a[N];
 32 
 33 inline void PreOrder(int t){
 34     cout << t << " ";
 35     if (a[t].l){
 36         PreOrder(a[t].l);
 37     }
 38     if (a[t].r){
 39         PreOrder(a[t].r);
 40     }
 41     return;
 42 }
 43 
 44 inline void InOrder(int t){
 45     if (a[t].l){
 46         InOrder(a[t].l);
 47     }
 48     cout << t << " ";
 49     if (a[t].r){
 50         InOrder(a[t].r);
 51     }
 52     return;
 53 }
 54 
 55 inline void PostOrder(int t){
 56     if (a[t].l){
 57         PostOrder(a[t].l);
 58     }
 59     if (a[t].r){
 60         PostOrder(a[t].r);
 61     }
 62     cout << t << " ";
 63     return;
 64 }
 65 
 66 inline int LCA(int u, int v){
 67     cout << "---This is LCA about " << u << " and " << v << "---" << endl;
 68     int l1 = 0;
 69     while (u != 1){
 70         c[++l1] = u, u = a[u].fa;
 71     }
 72     c[++l1] = 1;
 73     int l2 = 0;
 74     while (v != 1){
 75         d[++l2] = v, v = a[v].fa;
 76     }
 77     d[++l2] = 1;
 78     x = 0;
 79     for (int i = l1, j = l2;i && j;i--,j--){
 80         if (c[i] == d[j]){
 81             x = c[i];
 82         }
 83         else{
 84             break;
 85         }
 86     }
 87     cout << x << endl;
 88     return x;
 89 }
 90 
 91 inline void BFS(){
 92     cout << "---This is BFS---" << endl;
 93     int i = 1;
 94     while(a[i].val){
 95         cout << a[i].val << " ";
 96         i++;
 97     }
 98     return;
 99 }
100 
101 
102 /*
103 //基于指针队列的BFS
104 TreeNode *q[N];
105 void BFS(TreeNode *root){
106     int front = 1, rear = 1;
107     q[1] = root;
108     while (front <= rear){
109         TreeNode *p = q[front];
110         front++;
111         cout << p->value << endl;
112         if (p->l) q[++rear] = p->l;
113         if (p->r) q[++rear] = p->r;
114     }
115 }
116 BFS(root);
117 */
118 
119 inline int SubTreeSum(int t){
120     int x = a[t].val;
121     if (a[t].l){
122         x += SubTreeSum(a[t].l);
123     }
124     if (a[t].r){
125         x += SubTreeSum(a[t].r);
126     }
127     sum[t] = x;
128     return x;
129 }
130 
131 inline void Del(){
132     memset(a, 0, sizeof(a));
133     memset(c, 0, sizeof(c));
134     memset(d, 0, sizeof(d));
135     memset(sum, 0, sizeof(sum));
136     return;
137 }
138 
139 inline void Deep(){
140     cout << "---This is Deep---" << endl;
141     int i = 1;
142     while(a[i].val){
143         cout << a[i].dep << " ";
144         i++;
145     }
146     cout << endl;
147     return;
148 }
149 
150 inline void Path(){
151     cout << "---This is Path---" << endl;
152     int i = 1;
153     while (a[i].val){
154         int temp = i;
155         int deep = a[i].dep;
156         cout << "number : " << i << endl;
157         cout << "val    : " << a[i].val << endl;
158         cout << "deep   : " << a[i].dep << endl;
159         while(deep != 1){
160             cout << a[temp].fa << " ";
161             temp--;
162             deep--;
163         }
164         cout << endl;
165         i++;
166     }
167 }
168 
169 inline void ReversedPath(){
170     cout << "---This is ReversedPath---" << endl;
171     VI vv;
172     int i = 1;
173     while (a[i].val){
174         vv.clear();
175         int temp = i;
176         int deep = a[i].dep;
177         cout << "number : " << i << endl;
178         cout << "val    : " << a[i].val << endl;
179         cout << "deep   : " << a[i].dep << endl;
180         while(deep != 1){
181             vv.pb(a[temp].fa);
182             temp--;
183             deep--;
184         }
185         reverse(all(vv));
186         int m = vv.size();
187         for (int i = 0;i <= m-1;i++){
188             cout << vv[i] << " ";
189         }
190         cout << endl;
191         i++;
192     }
193 }
194 
195 inline void Query(int x){
196     cout << "---This is Query about " << x << " ---" << endl;
197     int i = 1;
198     while(a[i].val){
199         if (a[i].val == x){
200             cout << "number : " << i << endl;
201             cout << "val    : " << a[i].val << endl;
202             cout << "deep   : " << a[i].dep << endl;
203             if (a[i].fa) cout << "father : " << a[i].fa << endl;
204             if (a[i].l) cout <<  "l      : " << a[i].l << endl;
205             if (a[i].r) cout <<  "r      : " << a[i].r << endl;
206             return;
207         }
208         i++;
209     }
210     return;
211 }
212 
213 int main(){
214     ios;
215     cin >> n;
216     a[0].dep = 1;
217     a[1].dep = 1;
218     for (int i = 1;i <= n;i++){
219         cin >> x >> y;
220         if (x){
221             a[i].l = x;
222             a[x].fa = i;
223             a[x].dep = a[i].dep + 1;
224         }
225         if (y){
226             a[i].r = y;
227             a[y].fa = i;
228             a[y].dep = a[i].dep + 1;
229         }
230     }
231     for (int i = 1;i <= n;i++){
232         cin >> a[i].val;
233     }
234     cout << "---This is PreOrder---" << endl;
235     PreOrder(1); cout << endl;
236     cout << "---This is InOrder---" << endl;
237     InOrder(1); cout << endl;
238     cout << "---This is PostOrder---" << endl;
239     PostOrder(1); cout << endl;
240     cin >> u >> v;
241     LCA(u, v);
242     BFS();
243     cout << endl << "---This is SubTreeSum---" << endl;
244     SubTreeSum(1);
245     cout << endl;
246     for(int i = 1;i <= n;i++){
247         cout << sum[i] << " ";
248     }
249     cout << endl;
250     Deep();
251     Path();
252     cin >> tar;
253     Query(tar);
254     ReversedPath();
255     Del();
256     return 0;
257 }
258 
259 
260 /*
261 测试样例:
262 4
263 2 3
264 0 0
265 4 0
266 0 0
267 1 3 2 4
268 3 4
269 4
270 */
复制代码

 

posted @   Conqueror712  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示