adding post order code and notes
This commit is contained in:
@@ -119,7 +119,7 @@ void preorderTraversal(TreeNode* root) {
|
||||
}
|
||||
```
|
||||
|
||||
You can test the above function using this program: [inorder_main.cpp](inorder_main.cpp).
|
||||
You can test the above function using this program: [preorder_main.cpp](preorder_main.cpp).
|
||||
|
||||
For above test case, the testing program prints:
|
||||
|
||||
@@ -138,5 +138,72 @@ Preorder Traversal using Morris Traversal:
|
||||
|
||||
## 21.4 Morris Traversal - Post Order
|
||||
|
||||
Post order is different, and we need to write some helper functions here.
|
||||
|
||||
```cpp
|
||||
// function to reverse the right-edge path of a subtree
|
||||
TreeNode* reverse(TreeNode* head) {
|
||||
TreeNode* prev = nullptr;
|
||||
TreeNode* next = nullptr;
|
||||
|
||||
while (head != nullptr) {
|
||||
next = head->right;
|
||||
head->right = prev;
|
||||
prev = head;
|
||||
head = next;
|
||||
}
|
||||
return prev;
|
||||
}
|
||||
|
||||
// function to traverse and collect nodes along a reversed right edge
|
||||
void reverseTraverseRightEdge(TreeNode* head) {
|
||||
TreeNode* tail = reverse(head);
|
||||
TreeNode* current = tail;
|
||||
|
||||
while (current != nullptr) {
|
||||
std::cout << current->val << " ";
|
||||
current = current->right;
|
||||
}
|
||||
reverse(tail); // restore the original tree structure
|
||||
}
|
||||
|
||||
// Morris Postorder Traversal
|
||||
void postorderTraversal(TreeNode* root) {
|
||||
TreeNode* current = root;
|
||||
TreeNode* rightmost;
|
||||
|
||||
while (current != nullptr) {
|
||||
if (current->left != nullptr) {
|
||||
rightmost = current->left;
|
||||
while (rightmost->right != nullptr && rightmost->right != current) {
|
||||
rightmost = rightmost->right;
|
||||
}
|
||||
|
||||
if (rightmost->right == nullptr) {
|
||||
rightmost->right = current;
|
||||
current = current->left;
|
||||
} else {
|
||||
rightmost->right = nullptr;
|
||||
reverseTraverseRightEdge(current->left);
|
||||
current = current->right;
|
||||
}
|
||||
} else {
|
||||
current = current->right;
|
||||
}
|
||||
}
|
||||
|
||||
reverseTraverseRightEdge(root); // traverse the final right edge
|
||||
return;
|
||||
}
|
||||
```
|
||||
|
||||
You can test the above function using this program: [postorder_main.cpp](postorder_main.cpp).
|
||||
|
||||
For above test case, the testing program prints:
|
||||
|
||||
```console
|
||||
$ g++ postorder_main.cpp
|
||||
$ ./a.out
|
||||
Postorder Traversal using Morris Traversal:
|
||||
4 6 7 5 2 9 8 3 1
|
||||
```
|
||||
|
||||
83
lectures/21_trees_IV/postorder_main.cpp
Normal file
83
lectures/21_trees_IV/postorder_main.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <iostream>
|
||||
|
||||
class TreeNode {
|
||||
public:
|
||||
int val;
|
||||
TreeNode* left;
|
||||
TreeNode* right;
|
||||
|
||||
TreeNode(int value) : val(value), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
// function to reverse the right-edge path of a subtree
|
||||
TreeNode* reverse(TreeNode* head) {
|
||||
TreeNode* prev = nullptr;
|
||||
TreeNode* next = nullptr;
|
||||
|
||||
while (head != nullptr) {
|
||||
next = head->right;
|
||||
head->right = prev;
|
||||
prev = head;
|
||||
head = next;
|
||||
}
|
||||
return prev;
|
||||
}
|
||||
|
||||
// function to traverse and collect nodes along a reversed right edge
|
||||
void reverseTraverseRightEdge(TreeNode* head) {
|
||||
TreeNode* tail = reverse(head);
|
||||
TreeNode* current = tail;
|
||||
|
||||
while (current != nullptr) {
|
||||
std::cout << current->val << " ";
|
||||
current = current->right;
|
||||
}
|
||||
reverse(tail); // restore the original tree structure
|
||||
}
|
||||
|
||||
// Morris Postorder Traversal
|
||||
void postorderTraversal(TreeNode* root) {
|
||||
TreeNode* current = root;
|
||||
TreeNode* rightmost;
|
||||
|
||||
while (current != nullptr) {
|
||||
if (current->left != nullptr) {
|
||||
rightmost = current->left;
|
||||
while (rightmost->right != nullptr && rightmost->right != current) {
|
||||
rightmost = rightmost->right;
|
||||
}
|
||||
|
||||
if (rightmost->right == nullptr) {
|
||||
rightmost->right = current;
|
||||
current = current->left;
|
||||
} else {
|
||||
rightmost->right = nullptr;
|
||||
reverseTraverseRightEdge(current->left);
|
||||
current = current->right;
|
||||
}
|
||||
} else {
|
||||
current = current->right;
|
||||
}
|
||||
}
|
||||
|
||||
reverseTraverseRightEdge(root); // traverse the final right edge
|
||||
return;
|
||||
}
|
||||
|
||||
int main() {
|
||||
TreeNode* root = new TreeNode(1);
|
||||
root->left = new TreeNode(2);
|
||||
root->right = new TreeNode(3);
|
||||
root->left->left = new TreeNode(4);
|
||||
root->left->right = new TreeNode(5);
|
||||
root->left->right->left = new TreeNode(6);
|
||||
root->left->right->right = new TreeNode(7);
|
||||
root->right->right = new TreeNode(8);
|
||||
root->right->right->left = new TreeNode(9);
|
||||
|
||||
std::cout << "Postorder Traversal using Morris Traversal:\n";
|
||||
postorderTraversal(root);
|
||||
std::cout << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user