当前位置:网站首页>Middle order clue binary tree
Middle order clue binary tree
2022-06-26 00:45:00 【Be nice 123】
Clue binary tree
The basic concept of clue binary tree
The clue of binary tree is to change the null pointer in the binary linked list to point to the precursor or subsequent clue . The precursor or successor information can only be obtained when traversing , Therefore, the implementation of threaded binary tree is to traverse the binary tree once
Taking the establishment of middle order clue binary tree as an example :
- Attached pointer pre Point to the node just visited , The pointer p Point to the node being accessed , namely pre Point to p The precursor of .
- In the process of middle order traversal , Check p Whether the left pointer of is null , If empty, point it to pre; Check pre Whether the right pointer of is null , If empty, point it to p
Construction of ordered clue binary tree
InThread
Recursive algorithm for binary tree cueing through middle order traversal ( It is to make the null pointer of the node point to the predecessor or successor node of its order traversal )
// Recursive algorithm for binary tree cueing through middle order traversal ( Is to make the null pointer of a node point to its predecessor or successor node )
// Construction of ordered clue binary tree , Left root right
void InThread(ThreadTree &p,ThreadTree &pre){
if(p != NULL){
InThread(p->lchild,pre);// recursive , Clue left subtree
if(p->lchild == NULL){
// The left subtree is empty , Establish precursor clues
p->lchild=pre;
p->ltag=1;
}
if(pre!=NULL && pre->rchild==NULL){
pre->rchild=p;// Establish the following clues of the precursor node
pre->rtag=1;
}
pre=p;// Mark the current node as the node just visited
InThread(p->rchild, pre);// recursive , Clue right subtree
}//if(p != NULL)
}
CreateInThread
The main process algorithm of establishing the middle order clue binary tree through the middle order traversal is as follows :
This function is used to build a medium order clue binary tree , call InThread function , And handle the last node traversed .
CreateInThread and InThread The specific relationship between : The former can be regarded as the foreman who commands the overall situation , The latter is a small worker who moves bricks
// The main process algorithm of establishing the middle order clue binary tree through the middle order traversal is as follows :
void CreateInThread(ThreadTree &T){
ThreadTree pre=NULL;
if(T){
// Nonempty binary tree , Clue
InThread(T,pre);// Clue binary tree
pre->rchild==NULL;// Process the last node traversed
pre->rtag=1;//@@@
}
}
Clue binary tree with leading node
If you don't want the first node traversed by the middle order lchild And the last node rchild by NULL, You must add a header node , Cannot point it to the tree root , Because as shown in the figure below , The predecessor and successor nodes of the root point to the root , If we traverse the first node of the middle order lchild And the last node rchild Point to the root node , The correctness of the result cannot be guaranteed during traversal .
In a word, i.e :
If we let the middle order traverse the first node lchild And the last node rchild Not for NULL, You must add an additional Head Head node , Cannot simply point to the root node
Traversal of ordered clue binary tree
The nodes of the middle order clue binary tree contain the precursor and successor information of the clue binary tree . When traversing it , Just find the first node in the sequence , Then find the successor of the node in turn , Until its successor is empty .
Firstnode
Find the middle order clue in the binary tree , The first node under the middle order sequence
// Find the middle order clue in the binary tree , The first node under the middle order sequence
ThreadNode *Firstnode(ThreadNode *p){
//printf("Firstnode:");
while(p->ltag==0){
p=p->lchild;// Bottom left node ( It doesn't have to be a leaf node )
}
return p;
}
Nextnode
Find the middle order clue in the binary tree , node p Successor under middle order sequence
// Find the middle order clue in the binary tree , node p Successor under middle order sequence
ThreadNode *Nextnode(ThreadNode *p){
if(p->rtag==0){
return Firstnode(p->rchild);
}
else{
return p->rchild;//rtag==1 Return directly to the follow-up clues
}
}
Inorder
We can write a middle order traversal algorithm of middle order clue binary tree without head node
// Using the above two algorithms ,
// We can write a middle order traversal algorithm of middle order clue binary tree without head node
void Inorder(ThreadNode *T){
for(ThreadNode *p=Firstnode(T);p!=NULL;p=Nextnode(p)){
visit(p);
}
}
Complete test code
Complete test code c++
Be careful :
- You still need to create a binary tree first , Then the binary tree is traversed in the middle order
- When you create a binary tree, you should correct ltag rtag Initialize to 0, Express lchild,rchild It points to the left / The right child ( Not the precursor / The subsequent ), And initialization cannot be initialized in the definition of structure , Because when defining a structure , It is not allocated memory , So the initial value cannot be stored . After the structure variable should be declared , Manual assignment . In the following code is malloc Then initialize
- Because after the threaded binary tree, each node is threaded into a ring , Therefore, in the functions of middle order recursive traversal and post order recursive destruction, we need to ltag rtag Judge .
// Clue binary tree
#include<stdio.h>
#include<stdlib.h>
#define ElemType char
//tag by 0 Indicates pointing to the left / The right child , by 1 Indicates the precursor to the node / The subsequent
typedef struct ThreadNode{
ElemType data;// data elements
struct ThreadNode *lchild,*rchild;// Left and right child pointer
int ltag;// Because when defining a structure , It is not allocated memory , So the initial value cannot be stored . After the structure variable should be declared , Manual assignment
int rtag;// Left and right cue marks
}ThreadNode,*ThreadTree;
void visit(ThreadTree T){
printf("%c ",T->data);
}
// Construction of ordered clue binary tree , Left root right
void InThread(ThreadTree &p,ThreadTree &pre){
if(p != NULL){
InThread(p->lchild,pre);// recursive , Clue left subtree
if(p->lchild == NULL){
// The left subtree is empty , Establish precursor clues
p->lchild=pre;
p->ltag=1;
}
if(pre!=NULL && pre->rchild==NULL){
pre->rchild=p;// Establish the following clues of the precursor node
pre->rtag=1;
}
pre=p;// Mark the current node as the node just visited
InThread(p->rchild, pre);// recursive , Clue right subtree
}//if(p != NULL)
}
// The main process algorithm of establishing the middle order clue binary tree through the middle order traversal is as follows :
void CreateInThread(ThreadTree &T){
//ThreadNode *Head = (ThreadNode*)malloc(sizeof(ThreadNode));
// Head->lchild=T;
//Head->ltag=1;
//ThreadTree pre=T;
ThreadTree pre=NULL;
if(T){
// Nonempty binary tree , Clue
InThread(T,pre);// Clue binary tree
//Head->rchild=pre;
//Head->rtag=1;
pre->rchild==NULL;
//pre->rchild=T;// Process the last node traversed
pre->rtag=1;
}
}
// Find the middle order clue in the binary tree , The first node under the middle order sequence
ThreadNode *Firstnode(ThreadNode *p){
//printf("Firstnode:");
while(p->ltag==0){
p=p->lchild;// Bottom left node ( It doesn't have to be a leaf node )
}
return p;
}
// Find the middle order clue in the binary tree , node p Successor under middle order sequence
ThreadNode *Nextnode(ThreadNode *p){
if(p->rtag==0){
return Firstnode(p->rchild);
}
else{
return p->rchild;//rtag==1 Return directly to the follow-up clues
}
}
// Using the above two algorithms ,
// We can write a middle order traversal algorithm of middle order clue binary tree without head node
void Inorder(ThreadNode *T){
for(ThreadNode *p=Firstnode(T);p!=NULL;p=Nextnode(p)){
visit(p);
}
}
// Create a threaded binary tree , Enter... In the previous order , # Represents an empty node
bool CreateThreadTree(ThreadTree &T){
ElemType ch;
scanf("%c", &ch);
if(ch == '#'){
//printf(" Do you want to create an empty tree ?\n");
T=NULL;
return false;
}
else{
T=(ThreadTree)malloc(sizeof(ThreadNode));
T->ltag=T->rtag=0;
if(!T){
printf("malloc failure\n");
return false;
}
T->data = ch;
CreateThreadTree(T->lchild);
CreateThreadTree(T->rchild);
return true;
}
}
bool DestroyThreadTree(ThreadTree T){
if(T==NULL){
printf(" Blank nodes \n");
return false;
}
if(T->ltag!=1)
DestroyThreadTree(T->lchild);
if(T->rtag!=1)
DestroyThreadTree(T->rchild);
printf(" The destruction %c\n",T->data);
free(T);//@@@'
T=NULL;
return true;
}
// Traversing the clue binary tree in the middle order
void InOrder(ThreadTree T){
if(T){
if(T->ltag!=1)
InOrder(T->lchild);
visit(T);
if(T->rtag != 1)
InOrder(T->rchild);
}
}
int main(){
ThreadTree T=NULL;
printf(" Enter the values of the nodes in the binary tree in the previous order ( Input # Represents an empty node )\n");
CreateThreadTree(T);// Input preamble , Build a binary tree
CreateInThread(T);// Through the middle order traversal, the middle order clue binary tree is established
ThreadNode *p=Firstnode(T);// Find the first node under the middle order traversal
printf("\n The first node traversed in middle order p: %c\n",p->data);
ThreadNode* r=Nextnode(p);// Find the middle order to traverse p In the subsequent
printf("p In the subsequent r: %c\n",r->data);
printf(" Traversing the clue binary tree in the middle order ( recursive InOrder ≈ normal BinaryTree Traverse ): \n");
InOrder(T);
printf("\n");
printf("\n Traversing the clue binary tree in the middle order ( Non recursive Inorder ≈ Firstnode+Nextnode): \n");
Inorder(T);
printf("\n Remember to destroy it after use !\n");
DestroyThreadTree(T);
return 0;
}
sample input


test result

边栏推荐
- 10.2.2、Kylin_ Kylin installation, uploading and decompressing, verifying environment variables, starting and accessing
- Redux workflow explanation + small examples
- Methods to realize asynchrony
- 基于OpenVINOTM开发套件“无缝”部署PaddleNLP模型
- Solution to component stele in SMT chip processing
- no_ Expand and use_ concat
- Analyze the five root causes of product development failure
- Login interceptor
- 1-9Vmware中网络配置
- 鼠标拖拽围绕某个物体旋转展示
猜你喜欢

Permission design = function permission + Data permission

How to deliver a shelter hospital within 48 hours?

Redisson 3.17.4 release

删库跑路、“投毒”、改协议,开源有哪几大红线千万不能踩?

Shenzhen Taipower: the way of "communication" of the United Nations

渗透工具-Burpsuite

Regular expression introduction and some syntax
![Mysql5.7 is in the configuration file my Ini[mysqld] cannot be started after adding skip grant tables](/img/b2/2b87b3cea1422e2a860f5e0e7dcc40.png)
Mysql5.7 is in the configuration file my Ini[mysqld] cannot be started after adding skip grant tables

Installation and configuration of gradle environment

Idea set the template of mapper mapping file
随机推荐
mongodb
Oracle RAC cluster failed to start
Mysql5.7.31 user defined installation details
Comprehensive introduction to Simulink solver
11.1.1 overview of Flink_ Flink overview
flink报错:No ExecutorFactory found to execute the application
STL tutorial 5-basic concepts of STL and the use of string and vector
What do SMT operators do? operating duty?
原型和原型链的理解
Some basic uses of mongodb
Research and development practice of Kwai real-time data warehouse support system
DPVS fullnat mode kept
Idea view unit test coverage
Core ideas of SQL optimization
Apache foundation officially announced Apache inlong as a top-level project
2021-04-28
机器视觉:照亮“智”造新“视”界
Resolve thread concurrency security issues
QT custom QSlider with cursor
学习识别对话式问答中的后续问题
