当前位置:网站首页>Binary tree god level traversal: Morris traversal
Binary tree god level traversal: Morris traversal
2022-07-01 03:35:00 【Yi EBA】
Binary tree Morris Traverse
1. Create a binary tree
typedef struct BiTNode{
int data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
// Create a binary tree in the previous order
void createBiTree(BiTree *T){
int value;
cin>>value;
if (value == 0)
*T = NULL;
else{
*T = (BiTree)malloc(sizeof(BiTNode));
if(!*T)
exit(OVERFLOW);
(*T)->data = value;
createBiTree(&(*T)->lchild);
createBiTree(&(*T)->rchild);
}
}
1 2 4 0 0 5 0 0 3 6 0 0 7 0 0 Building a binary tree

2. Regular traversal
// The former sequence traversal
void PreOrderTraverse(BiTree T){
if(T==NULL)
return;
cout<<T->data<<" ";
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
// In the sequence traversal
void InOrderTraverse(BiTree T){
if(T==NULL)
return;
InOrderTraverse(T->lchild);
cout<<T->data<<" ";
InOrderTraverse(T->rchild);
}
// After the sequence traversal
void PostOrderTraverse(BiTree T){
if(T==NULL)
return;
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
cout<<T->data<<" ";
}
// test
int main(){
BiTree T;
createBiTree(&T);
PreOrderTraverse(T); // 1 2 4 5 3 6 7
cout<<endl;
InOrderTraverse(T); // 4 2 5 1 6 3 7
cout<<endl;
PostOrderTraverse(T); // 4 5 2 6 7 3 1
return 0;
}
3. Morris Traverse
- get Morris The process of executing a sequence :
- Remember that the current node is current( initial current = head), If current There is no left subtree (current.left = null), be current To the right (current = current.right),
- When current There's a Zuozi tree (current.left != null), find current The rightmost node of the left subtree ( Write it down as mostRight),
- If mostRight.right = null, Will mostRight Of right Pointer to current, namely mostRight.right = current, here current Move to the left ,
- If mostRight.right = current, Will mostRight Of right Pointer to a null, namely mostRight.right = null, here current To the right ,
- current = null when , Run terminated .
- Record every time current The node of finally gets Morris order .
Example demonstration :











// obtain Morris Sequence
void morris(BiTree T){
if(T==NULL)
return;
BiTree cur = T; // initialization cur=T Head node
BiTree mostRight = NULL; // initialization mostRight=NULL
while(cur != NULL){
mostRight = cur->lchild; //mostRight=cur The left child
cout<<cur->data<<" "; // Print Morris Sequence
if(mostRight!=NULL){
while(mostRight->rchild!=NULL && mostRight->rchild!=cur){
mostRight = mostRight->rchild; // take mostRight Move to cur The rightmost node of the left child
}
if(mostRight->rchild == NULL){
// mostRight The right node of NULL
mostRight->rchild = cur; // Point to cur
cur = cur->lchild; // cur Move left turn to continue Go straight to the next cycle
continue;
} else {
mostRight->rchild = NULL; // otherwise mostRight Set up NULL
}
}
cur = cur->rchild; // There is no left subtree or execution mostRight->rchild=NULL After execution
}
}
Morris The characteristics of order :
1. Node has no left subtree , It's in Morris There is only one occurrence in the sequence ,
2. The node has a left subtree , It's in Morris There are two times in the sequence .below , According to what you get Morris First order 、 Middle preface 、 After the sequence traversal :
1. The first sequence traversal : stay Morris Print the first occurrence in the sequence , Don't print the second time ,
2. In the sequence traversal : stay Morris Print once in the sequence , Appear twice in the second print ,
3. After the sequence traversal : stay Morris Nodes that appear twice in the sequence and appear the second time are processed : Print the right boundary of the left subtree of the node in reverse order , Finally, print the right boundary of the binary tree in reverse order .
//Morris The first sequence traversal :
void morrisPre(BiTree T){
if(T==NULL)
return;
BiTree cur = T;
BiTree mostRight = NULL;
while(cur != NULL){
mostRight = cur->lchild;
if(mostRight!=NULL){
// The left subtree is not NULL, necessarily Morris There are two nodes in the sequence
while(mostRight->rchild!=NULL && mostRight->rchild!=cur){
mostRight = mostRight->rchild;
}
if(mostRight->rchild == NULL){
// Morris The first occurrence of two nodes in the sequence data Print
cout<<cur->data<<" ";
mostRight->rchild = cur;
cur = cur->lchild;
continue;
} else {
mostRight->rchild = NULL;
}
} else{
// Morris There can only be one case in the sequence data Print
cout<<cur->data<<" ";
}
cur = cur->rchild;
}
}
//Morris In the sequence traversal :
void morrisIn(BiTree T){
if(T==NULL)
return;
BiTree cur = T;
BiTree mostRight = NULL;
while(cur != NULL){
mostRight = cur->lchild;
if(mostRight!=NULL){
// The left subtree is not NULL, necessarily Morris There are two nodes in the sequence
while(mostRight->rchild!=NULL && mostRight->rchild!=cur){
mostRight = mostRight->rchild;
}
if(mostRight->rchild == NULL){
// Morris Two nodes in the sequence appear for the first time
mostRight->rchild = cur;
cur = cur->lchild;
continue;
} else {
// The second time
mostRight->rchild = NULL;
}
}
cout<<cur->data<<" "; // Print the middle order sequence node
cur = cur->rchild;
}
}
//Morris After the sequence traversal :
- Morris order :1 2 4 2 5 1 3 6 3 7
- In the second occurrence 2 When printing in reverse order 2 The right boundary of the left subtree of is 4,
- In the second occurrence 1 When printing in reverse order 1 The right boundary of the left subtree of is 5,2,
- In the second occurrence 2 When printing in reverse order 3 The right boundary of the left subtree of is 6,
- Finally, the right boundary of the binary tree is printed in reverse order 7,3,1.
- The result of post order traversal is :4,5,2,6,7,3,1.
To ensure that the space complexity is O(1), When printing the right boundary in reverse order , The right pointer of the boundary node must be adjusted .
// from now Start , Consider only the right pointer , Use it as a single linked list next, Reverse the single linked list , Returns the last node
BiTree reverseEdge(BiTree T){
BiTree pre = NULL;
BiTree next = NULL;
while(T!=NULL){
next = T->rchild;
T->rchild = pre;
pre = T;
T = next;
}
return pre;
}
// With T Node headed tree , Print the right boundary of the tree in reverse order
void printEdge(BiTree T){
BiTree tail = reverseEdge(T);
BiTree cur = tail;
while(cur!=NULL){
cout<<cur->data<<" ";
cur = cur->rchild;
}
reverseEdge(tail);
}
// Subsequent sequence
void morrisPost(BiTree T){
if(T==NULL)
return;
BiTree cur = T;
BiTree mostRight = NULL;
while(cur != NULL){
mostRight = cur->lchild;
if(mostRight!=NULL){
// The left subtree is not NULL, necessarily Morris There are two nodes in the sequence
while(mostRight->rchild!=NULL && mostRight->rchild!=cur){
mostRight = mostRight->rchild;
}
if(mostRight->rchild == NULL){
// Morris Two nodes in the sequence appear for the first time
mostRight->rchild = cur;
cur = cur->lchild;
continue;
} else {
// Two times and the second time
mostRight->rchild = NULL;
printEdge(cur->lchild); // To deal with
}
}
cur = cur->rchild;
}
// After the end , Print the right boundary of the binary tree in reverse order
printEdge(T);
}
4. Complexity of time and space
Time complexity :O(n)
Spatial complexity :O(1)
——————END-2022-06-26——————
边栏推荐
- split(),splice(),slice()傻傻分不清楚?
- Hal library operation STM32 serial port
- Pytest -- plug-in writing
- 数据库中COMMENT关键字的使用
- Take you through a circuit board, from design to production (dry goods)
- Filter
- How do I use Google Chrome 11's Upload Folder feature in my own code?
- 文件上传下载
- Md5sum operation
- Pyramid scene parsing network [pspnet] thesis reading
猜你喜欢

Learning notes for introduction to C language multithreaded programming

Stop saying that you can't solve the "cross domain" problem
![[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st](/img/9f/187ca83be1b88630a6c6fbfb0620ed.png)
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st

Listener listener

Nacos

IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?

The combination of applet container technology and IOT

Explain spark operation mode in detail (local+standalone+yarn)

数据交换 JSON

How do spark tasks of 10W workers run? (Distributed Computing)
随机推荐
Leetcode:829. Sum of continuous integers
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
终极套娃 2.0 | 云原生交付的封装
Depth first traversal of C implementation Diagram -- non recursive code
Valid brackets (force deduction 20)
Develop industrial Internet with the technical advantages of small programs
Bilinear upsampling and f.upsample in pytorch_ bilinear
Thread data sharing and security -threadlocal
Ctfshow blasting WP
文件上传下载
C语言的sem_t变量类型
Explain spark operation mode in detail (local+standalone+yarn)
Pathmeasure implements loading animation
Test function in pychram
JS daily development tips (continuous update)
LeetCode 128最长连续序列(哈希set)
File upload and download
shell脚本使用两个横杠接收外部参数
Edge Drawing: A combined real-time edge and segment detector 翻译
完全背包问题