当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

12.进程同步与信号量

The nanny level tutorial of flutter environment configuration makes the doctor green to the end

Mongodb quickly get started with some simple operations of mongodb command line
![二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)](/img/c2/bb85b681af0f78b380b1d179c7ea49.png)
二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)

shell编程01_Shell基础

JSP webshell免杀——JSP的基础

Easyexcel, a concise, fast and memory saving excel processing tool

UVM learning - object attribute of UVM phase

QT学习日记8——资源文件添加

2022-06-17
随机推荐
Introduction to MySQL 8 DBA foundation tutorial
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
HDU1236 排名(结构体排序)
二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)
从MediaRecord录像中读取H264参数
【AppLinking实战案例】通过AppLinking分享应用内图片
【ARK UI】HarmonyOS ETS的启动页的实现
Read H264 parameters from mediarecord recording
Mongodb quickly get started with some simple operations of mongodb command line
PCL之滤波
SPSS做Shapiro-Wilk正态分析
AppGallery Connect场景化开发实战—图片存储分享
华为应用市场应用统计数据问题大揭秘
1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
PCL之K-d树与八叉树
记录 AttributeError: ‘NoneType‘ object has no attribute ‘nextcall‘
二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)
UWA报告使用小技巧,你get了吗?(第四弹)
Operator-1 first acquaintance with operator
Hdu1228 a + B (map mapping)