当前位置:网站首页>Special topic of binary tree -- acwing 3540 Binary search tree building (use the board to build a binary search tree and output the pre -, middle -, and post sequence traversal)
Special topic of binary tree -- acwing 3540 Binary search tree building (use the board to build a binary search tree and output the pre -, middle -, and post sequence traversal)
2022-07-02 10:59:00 【Morgannr】


The question :
Enter a series of integers , Use the given data to build a binary search tree , And output its preamble 、 Middle order and post order traversal sequences .
Ideas :
This code can be used to make a practical board .
I used to Array of structs bst Save tree :
const int N = 110;
struct node
{
int key; // The key value of the current node
int l = -1, r = -1; // The left and right sons of the current node , First initialize to -1, It means you haven't left or right your son
int cnt; // identical key How many nodes of value
} bst[N];
according to Definition of binary search tree , Recursively build a tree , Here is insert function :
void insert(int u, int key)
{
if (bst[0].key == 0) bst[idx++].key = key; // Root node key value special processing
if (key < bst[u].key)
{
if (bst[u].l == -1) // Recurse to the left until you can't recurse anymore , Generate a new leaf node and assign the left pointer
{
bst[idx].key = key;
bst[u].l = idx++;
}
else insert(bst[u].l, key); // Continue to recurse to the left before the end
}
else if (key > bst[u].key)
{
if (bst[u].r == -1) // Recurse to the right until you can no longer recurse , Generate a new leaf node and assign the right pointer
{
bst[idx].key = key;
bst[u].r = idx++;
}
else insert(bst[u].r, key); Continue to recurse to the right before the end
}
else if(key == bst[u].key) bst[u].cnt++;
}
Next is the front 、 in 、 The routine code of post sequence traversal :
Before the order dfs1 function :( root Left Right )
void dfs1(int u) // Before the order
{
if (u == -1) return; // Back to the end
ans1.push_back(bst[u].key); // root
dfs1(bst[u].l); // Left
dfs1(bst[u].r); // Right
}
Middle preface dfs2 function :( Left root Right )
void dfs2(int u) // Continued
{
if (u == -1) return;
dfs2(bst[u].l); // Left
ans2.push_back(bst[u].key); // root
dfs2(bst[u].r); // Right
}
In the following order dfs3 function :( Left Right root )
void dfs3(int u) // follow-up
{
if (u == -1) return;
dfs3(bst[u].l); // Left
dfs3(bst[u].r); // Right
ans3.push_back(bst[u].key); // root
}
Code :
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
//#define int long long
//#define map unordered_map
const int N = 110;
int idx;
struct node
{
int key;
int l = -1, r = -1;
int cnt;
} bst[N];
int w[N];
int n;
vector<int> ans1, ans2, ans3;
void insert(int u, int key)
{
if (bst[0].key == 0) bst[idx++].key = key;
if (key < bst[u].key)
{
if (bst[u].l == -1)
{
bst[idx].key = key;
bst[u].l = idx++;
}
else insert(bst[u].l, key);
}
else if (key > bst[u].key)
{
if (bst[u].r == -1)
{
bst[idx].key = key;
bst[u].r = idx++;
}
else insert(bst[u].r, key);
}
else if(key == bst[u].key) bst[u].cnt++;
}
void dfs1(int u)// Before the order
{
if (u == -1) return;
ans1.push_back(bst[u].key);
dfs1(bst[u].l);
dfs1(bst[u].r);
}
void dfs2(int u)// Continued
{
if (u == -1) return;
dfs2(bst[u].l);
ans2.push_back(bst[u].key);
dfs2(bst[u].r);
}
void dfs3(int u)// follow-up
{
if (u == -1) return;
dfs3(bst[u].l);
dfs3(bst[u].r);
ans3.push_back(bst[u].key);
}
signed main()
{
int T = 1; //cin >> T;
while (T--)
{
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> w[i];
insert(0, w[i]);
}
dfs1(0), dfs2(0), dfs3(0);
for (auto v : ans1) cout << v << ' ';
puts("");
for (auto v : ans2) cout << v << ' ';
puts("");
for (auto v : ans3) cout << v << ' ';
puts("");
}
return 0;
}
边栏推荐
- AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
- 快应用中实现自定义抽屉组件
- P1055 [noip2008 popularization group] ISBN number
- 点云投影图片
- OpenMLDB Meetup No.4 会议纪要
- C#中索引器
- PCL之滤波
- Convert yv12 to rgb565 image conversion, with YUV to RGB test
- 二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
- Retrofit's callback hell is really vulnerable in kotlin synergy mode!
猜你喜欢

Disassembling Meitu SaaS: driving the plane to change the engine

Introduction to MySQL 8 DBA foundation tutorial

UVM learning - build a simple UVM verification platform

Nodejs+express+mysql simple blog building

The URL in the RTSP setup header of the axis device cannot take a parameter

How to get the password of cpolar?

HDU1236 排名(结构体排序)

OpenMLDB Meetup No.4 会议纪要

【AGC】构建服务3-认证服务示例

一招快速实现自定义快应用titlebar
随机推荐
Mongodb quickly get started with some simple operations of mongodb command line
Jsp webshell Free from killing - The Foundation of JSP
js promise. all
PCL projection point cloud
The nanny level tutorial of flutter environment configuration makes the doctor green to the end
C#中索引器
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
Nodejs+express+mysql simple blog building
13. Semaphore critical zone protection
axis设备的rtsp setup头中的url不能带参
PCL之K-d树与八叉树
【AppLinking实战案例】通过AppLinking分享应用内图片
js数组常用方法
使用华为性能管理服务,按需配置采样率
Set the playback speed during the playback of UOB equipment
How to get the password of cpolar?
正则及常用公式
Retrofit's callback hell is really vulnerable in kotlin synergy mode!
二叉树专题--洛谷 P1229 遍历问题(乘法原理 已知前、后序遍历求中序遍历个数)
QT学习日记7——QMainWindow