From 4bf8776b54074785cc363c0af4953e323d592d1d Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Thu, 27 Mar 2025 21:31:48 -0400 Subject: [PATCH] adding post order code and notes --- lectures/21_trees_IV/README.md | 69 +++++++++++++++++++- lectures/21_trees_IV/postorder_main.cpp | 83 +++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 lectures/21_trees_IV/postorder_main.cpp diff --git a/lectures/21_trees_IV/README.md b/lectures/21_trees_IV/README.md index 03428dd..d9ee5b7 100644 --- a/lectures/21_trees_IV/README.md +++ b/lectures/21_trees_IV/README.md @@ -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 ``` diff --git a/lectures/21_trees_IV/postorder_main.cpp b/lectures/21_trees_IV/postorder_main.cpp new file mode 100644 index 0000000..72a2d48 --- /dev/null +++ b/lectures/21_trees_IV/postorder_main.cpp @@ -0,0 +1,83 @@ +#include + +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; +}