当前位置:网站首页>Tree reconstruction (pre order + middle order or post order + middle order)
Tree reconstruction (pre order + middle order or post order + middle order)
2022-06-12 13:47:00 【Disobey the law】
List of articles
I feel it is the simplest to write recursively
Before the order + Middle preface
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int zhong[40], qian[40];
int root = 0;
typedef struct node* Node;
struct node {
int data;
Node left;
Node right;
};
void Cen_Travel(Node T){
};
// Functions of sequence traversal
void BuildTree(Node& T, int beg, int end) {
// The pointer points to just a piece of space , You can only modify the contents of the space , You want to change the point of the pointer , You need to pass in the address of the pointer
int pos = -1;// Used to locate the position of the root node in the sequence
for (int i = beg; i <= end; i++)
{
if (zhong[i] == qian[root]) {
pos = i;
break;
}
}
if (pos == -1) {
T = NULL;
return;
}
T = new struct node;
T->data = qian[root];
root++;
// You must first left the subtree , Then the right subtree , You can't change the order , Later said
BuildTree(T->left, beg, pos-1);
BuildTree(T->right, pos+1, end);
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> zhong[i];
for (int i = 0; i < n; i++)
cin >> qian[i];
Node T;
BuildTree(T, 0, n-1);
Cen_Travel(T);
return 0;
}
In the following order + Middle preface
int root = n-1;
void BuildTree(Node &node,int beg, int end)
{
int pos = -1;
for (int i = beg; i <= end; i++) {
if (zhong[i] == hou[root]) {
pos = i;
break;
}
}
if (pos == -1) {
node = NULL;
return;
}
node = new struct node;
node->data = hou[root];
root--;
// The order from right to left cannot be changed
BuildTree(node->right, pos + 1, end);
BuildTree(node->left, beg, pos-1);
}
explain
( Preorder is to access the root node first , The first step is to access the root node in the middle , The last step is to access the root node )
It is recommended to use it in conjunction with recursive traversal code
1. Why do you order first + Middle order is first left subtree and then right subtree , Posterior order + The middle order is reversed :
Preface :
Let's first look at how the preorder traverses .
First visit the root node And then I go through the left subtree , Finally, traverse the right subtree .
So the first node of our output sequence must be the root node of the tree , Then we visit the left subtree first , The second node is the current subtree ( The left subtree ) The root node , Then continue to visit the left subtree , So the third node is the root node of the current subtree …
So we root++ You can find the root node of each subtree step by step . This subtree in the middle order sequence corresponds to [beg,end], The left subtree of this subtree is [beg,pos-1], The right subtree is [pos+1,end].
So first reconstruct the left subtree .
In the following order :
After the sequence in the same way , The following sequence is the last access to the root node , So the last node is the root node of the whole tree . Because first access the left subtree and then access the right subtree , So the post sequence is close to root Is a right subtree . our rot from n-1 Start , So close to root The node of is root The right subtree of the corresponding tree , So first reconstruct the right subtree , The left subtree is only after the reconstruction of the right subtree .
summary
The antecedent root is on the left , near root Is a left subtree , First reconstruct the left subtree .
Postorder root on the right , near root Is a right subtree , Rebuild the right subtree first .
边栏推荐
- Innovation training (XII) project summary
- Install RPM package offline using yum
- 一种快速创建测试窗口的方法
- 【mysql进阶】mysql索引数据结构的演变(四)
- jupyternotebook有汉字数据库吗。在深度学习中可以识别手写中文吗
- Record some settings for visual studio 2019
- Xcode debugging OpenGLES
- Real time software source code of COVID-19
- Code debugging - print log output to file
- 简历 NFT 平台 TrustRecruit 加入章鱼网络成为候选应用链
猜你喜欢

事件的传递和响应以及使用实例

Encryptor and client authenticate with each other

【mysql进阶】索引分类及索引优化方案(五)

Implementing pytorch style deep learning framework similartorch with numpy

高通平台开发系列讲解(协议篇)QMI简单介绍及使用方法

Pytorch framework

A method of quickly creating test window

上海解封背后,这群开发者“云聚会”造了个AI抗疫机器人
![[wustctf2020] selfie score query -1](/img/90/e4c2882357e0a1c6a80f778887e3f5.png)
[wustctf2020] selfie score query -1

imagemagick:a gentle introduction to magick++
随机推荐
JVM runtime parameters
import torch_ Geometric loads some common datasets
AVFoundation
Recursion of subviews of view
What if the MySQL installation on the apple computer is completed and cannot be found
上海解封背后,这群开发者“云聚会”造了个AI抗疫机器人
Informatics Olympiad all in one 2059: [example 3.11] buy a pen
2062: [example 1.3] movie tickets
[brush title] probability of winning a draw
Codeforces 1637 D. yet another minimization problem - Mathematics, DP
Overview of embedded system 2- composition and application of embedded system
Codeforces 1629 A. download more RAM - simple greed
事件的传递和响应以及使用实例
Seeking magic square of order n with C language
Convert the string to hexadecimal string and display it
Implementing tensorflow deep learning framework similarflow with numpy
Language skills used in development
Simple implementation of gpuimage chain texture
D1 哪吒开发板 了解基本的启动加载流程
【mysql进阶】mysql索引数据结构的演变(四)