当前位置:网站首页>Morris traversal
Morris traversal
2022-06-24 23:32:00 【Today I will also write bugs】
The traditional traversal method is either recursive or stack , The space complexity of its traversal is O(N), and Morris Traversal can be done with O(1) Spatial complexity of , And its time complexity is the same as that of the traditional method O(N).
List of articles
Morris Traversal introduction
Morris Traversal uses a large number of free pointers in the original tree , To save space .
Morris The traversal rule is :
Suppose the current node cur, At the beginning of the cur Go to the position of the head node
- If cur There is no left child ,cur To the right (cur=cur->right)
- If cur There are left children , Find the rightmost node on the left subtree mostRight
1). If mostRight The right pointer of points to null , Make it point cur, then cur Move to the left (cur=cur->left)
2). If mostRight The right pointer of points to cur, Make it point nullptr, then cur To the right (cur=cur->right) - cur It's empty time , Stop traversal .
class Node
{
public:
Node(int _value)
{
value = _value;
left = nullptr;
right = nullptr;
}
int value;
Node* left;
Node* right;
};
void morris(Node* head)
{
if (head == nullptr)
{
return;
}
Node* cur = head;
//mostRight by cur The rightmost node of the left tree
Node* mostRight = nullptr;
while (cur != nullptr)
{
mostRight = cur->left;
// If cur No left tree , that cur Will move directly to the right
if (mostRight != nullptr)
{
// look for cur The rightmost node of the left tree , When empty or equal to cur Stop when
while (mostRight->right != nullptr && mostRight->right != cur)
{
mostRight = mostRight->right;
}
// If it is empty , Explain the first time to the rightmost node , Now point the pointer to cur
// then cur Move to left subtree
if (mostRight->right == nullptr)
{
mostRight->right = cur;
cur = cur->left;
continue;
}
// here mostRight->right == cur
// Indicates the second arrival at the node , Recover the pointer of this node
else
{
mostRight->right = nullptr;
}
}
cur = cur->right;
}
}

Morris The first sequence traversal
The order of preorder traversal is about the root , because Morris Traversal may reach a node twice , So we need to print the first time we reach the node .
void morrisPre(Node* head)
{
if (head == nullptr)
{
return;
}
Node* cur = head;
//mostRight by cur The rightmost node of the left tree
Node* mostRight = nullptr;
while (cur != nullptr)
{
mostRight = cur->left;
// If cur No left tree , that cur Will move directly to the right
if (mostRight != nullptr)
{
// look for cur The rightmost node of the left tree , When empty or equal to cur Stop when
while (mostRight->right != nullptr && mostRight->right != cur)
{
mostRight = mostRight->right;
}
// If it is empty , Explain the first time to the rightmost node , Now point the pointer to cur
// then cur Move to left subtree
if (mostRight->right == nullptr)
{
cout << cur->value << endl;
mostRight->right = cur;
cur = cur->left;
continue;
}
// here mostRight->right == cur
// explain mostRight Arrive at this node for the second time , Recover the pointer of this node
else
{
mostRight->right = nullptr;
}
}
//cur There is no left tree , Print directly cur, And then move cur To right subtree
else
{
cout << cur->value << endl;
}
cur = cur->right;
}
}
Morris In the sequence traversal
Middle order traversal follows the principle of left root right , For a node , Print on second arrival .
void morrisIn(Node* head)
{
if (head == nullptr)
{
return;
}
Node* cur = head;
//mostRight by cur The rightmost node of the left tree
Node* mostRight = nullptr;
while (cur != nullptr)
{
mostRight = cur->left;
// If cur No left tree , that cur Will move directly to the right
if (mostRight != nullptr)
{
// look for cur The rightmost node of the left tree , When empty or equal to cur Stop when
while (mostRight->right != nullptr && mostRight->right != cur)
{
mostRight = mostRight->right;
}
// If it is empty , Explain the first time to the rightmost node , Now point the pointer to cur
// then cur Move to left subtree
if (mostRight->right == nullptr)
{
mostRight->right = cur;
cur = cur->left;
continue;
}
// here mostRight->right == cur
// explain mostRight Arrive at this node for the second time , Recover the pointer of this node
else
{
mostRight->right = nullptr;
}
}
// Because the first time mostRight->right = cur;
// So the left subtree cur You can reach this node twice , Print the second time
cout << cur->value << endl;
cur = cur->right;
}
}
Morris After the sequence traversal
Postorder traversal is the order of left and right roots , Because we didn't set the parent pointer , Therefore, it is impossible to print from the right subtree to the root node . Unless the stack structure is used in reverse order , But this violates the space complexity of O(1) The purpose of .
therefore , We need to flip the linked list , Point the right pointer to its parent node , Change back after printing .
// Flip list
Node* reverseEdge(Node* from)
{
Node* pre = nullptr;
Node* next = nullptr;
while (from != nullptr)
{
next = from->right;
from->right = pre;
pre = from;
from = next;
}
return pre;
}
void printEdge(Node* head)
{
// Regard the rightmost part of the right subtree as a linked list
// Flip the linked list
Node* tail = reverseEdge(head);
Node* cur = tail;
while (cur != nullptr)
{
cout << cur->value << endl;
cur = cur->right;
}
// Turn the linked list back
reverseEdge(tail);
}
void morrisPos(Node* head)
{
if (head == nullptr)
{
return;
}
Node* cur = head;
//mostRight by cur The rightmost node of the left tree
Node* mostRight = nullptr;
while (cur != nullptr)
{
mostRight = cur->left;
// If cur No left tree , that cur Will move directly to the right
if (mostRight != nullptr)
{
// look for cur The rightmost node of the left tree , When empty or equal to cur Stop when
while (mostRight->right != nullptr && mostRight->right != cur)
{
mostRight = mostRight->right;
}
// If it is empty , Explain the first time to the rightmost node , Now point the pointer to cur
// then cur Move to left subtree
if (mostRight->right == nullptr)
{
mostRight->right = cur;
cur = cur->left;
continue;
}
// here mostRight->right == cur
// Indicates the second arrival at the node , Recover the pointer of this node
else
{
mostRight->right = nullptr;
// Print the right boundary of the left subtree of a tree in reverse order
printEdge(cur->left);
}
}
cur = cur->right;
}
// Print the right boundary of the whole tree in reverse order
printEdge(head);
}
边栏推荐
- Volcano成Spark默认batch调度器
- OpenSSL SSL_ read: Connection was reset, errno 10054
- Bubble sort
- InnoDB, the storage engine of MySQL Architecture Principle_ Redo log and binlog
- 257. 关押罪犯
- What you must know about time series database!
- Hydropower project construction scheme based on 3D GIS Development
- Collation of Digital IC design experience (II)
- Actipro WPF Controls 2022.1.2
- 7-8 梯云纵
猜你喜欢

Super detailed cookie addition, deletion, modification and query

斐波那契
What you must know about time series database!

Ganglia 的安装与部署

File contains vulnerability issues

当初吃土建起来的“中台”,现在为啥不香了?

7-6 铺设油井管道

明天就是PMP考试了(6月25日),这些大家都了解了吗?

Adding, deleting, querying and modifying MySQL tables

年薪百万,7年测试经验:守在一个还算不错的赛道,慢慢积累,等风来
随机推荐
Selective sort method
379. 捉迷藏
Ningde times will increase RMB 45billion: Hillhouse subscribes RMB 3billion and Zeng Yuqun still controls 23% of the equity
golang convert json string to map
7-8 循环日程安排问题
宁德时代定增450亿:高瓴认购30亿 曾毓群仍控制23%股权
idea创建模块提示已存在
Installing IBM CPLEX academic edition | CONDA installing CPLEX
Latest development of jetpack compose
(Smooth)ScrollToPosition doesn't work properly with RecyclerView
7-5 maximal submatrix sum problem
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses exp function and coef function to obtain the corresponding odds rat
7-3 最大子段和
OpenSSL SSL_ read: Connection was reset, errno 10054
[JS] - [tree] - learning notes
Quickly build KVM virtual machine on # yyds dry goods inventory # physical machine
Detailed explanation of online group chat and dating platform project (servlet implementation)
【js】-【栈、队-应用】-学习笔记
点的螺旋距离
sql -CONVERT函数