adding preorder notes and code

This commit is contained in:
Jidong Xiao
2025-03-27 21:19:38 -04:00
committed by JamesFlare
parent 4536070bcd
commit 2e1604f3bd
7 changed files with 76 additions and 15 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

View File

@@ -40,8 +40,7 @@ Instead of using extra memory (like recursion stack or an explicit stack), Morri
- Repeat until you traverse the entire tree. - Repeat until you traverse the entire tree.
```cpp ```cpp
vector<int> inorderTraversal(TreeNode* root) { void inorderTraversal(TreeNode* root) {
vector<int> result;
TreeNode *current=root; TreeNode *current=root;
TreeNode *rightmost; TreeNode *rightmost;
while(current!=NULL){ while(current!=NULL){
@@ -54,16 +53,16 @@ vector<int> inorderTraversal(TreeNode* root) {
rightmost->right=current; rightmost->right=current;
current=current->left; current=current->left;
}else{ /* second time */ }else{ /* second time */
result.push_back(current->val); std::cout << current->val << " ";
rightmost->right=NULL; rightmost->right=NULL;
current=current->right; current=current->right;
} }
}else{ /* nodes which do not have left child */ }else{ /* nodes which do not have left child */
result.push_back(current->val); std::cout << current->val << " ";
current=current->right; current=current->right;
} }
} }
return result; return;
} }
``` ```
@@ -71,7 +70,7 @@ You can test the above function using this program: [inorder_main.cpp](inorder_m
For this test case, For this test case,
![alt text](binaryTree.png "Binary Tree example") ![alt text](binaryTree.png "Binary Tree Test Case")
The testing program prints: The testing program prints:
@@ -85,9 +84,7 @@ Inorder Traversal using Morris Traversal:
## 21.3 Morris Traversal - Pre Order ## 21.3 Morris Traversal - Pre Order
```cpp ```cpp
vector<int> preorderTraversal(TreeNode* root) { void preorderTraversal(TreeNode* root) {
vector<int> result;
int index=0;
TreeNode *current=root; TreeNode *current=root;
TreeNode *rightmost; TreeNode *rightmost;
while(current != nullptr){ while(current != nullptr){
@@ -97,7 +94,7 @@ vector<int> preorderTraversal(TreeNode* root) {
rightmost=rightmost->right; rightmost=rightmost->right;
} }
if(rightmost->right==nullptr){ /* first time */ if(rightmost->right==nullptr){ /* first time */
result.push_back(current->val); std::cout << current->val << " ";
rightmost->right=current; rightmost->right=current;
current=current->left; current=current->left;
}else{ /* second time */ }else{ /* second time */
@@ -105,12 +102,23 @@ vector<int> preorderTraversal(TreeNode* root) {
current=current->right; current=current->right;
} }
}else{ /* nodes which do not have left child */ }else{ /* nodes which do not have left child */
result.push_back(current->val); std::cout << current->val << " ";
current=current->right; current=current->right;
} }
} }
return result; return;
} }
```
You can test the above function using this program: [inorder_main.cpp](inorder_main.cpp).
For above test case, the testing program prints:
```console
$ g++ preorder_main.cpp
$ ./a.out
Preorder Traversal using Morris Traversal:
1 2 4 5 6 7 3 8 9
``` ```
## 21.4 Morris Traversal - Post Order ## 21.4 Morris Traversal - Post Order

Binary file not shown.

Before

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

View File

@@ -22,12 +22,12 @@ void inorderTraversal(TreeNode* root) {
rightmost->right=current; rightmost->right=current;
current=current->left; current=current->left;
}else{ /* second time */ }else{ /* second time */
std::cout << current->val << " "; std::cout << current->val << " ";
rightmost->right=NULL; rightmost->right=NULL;
current=current->right; current=current->right;
} }
}else{ /* nodes which do not have left child */ }else{ /* nodes which do not have left child */
std::cout << current->val << " "; std::cout << current->val << " ";
current=current->right; current=current->right;
} }
} }

View File

@@ -0,0 +1,53 @@
#include <iostream>
class TreeNode {
public:
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int value) : val(value), left(NULL), right(NULL) {}
};
void preorderTraversal(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){ /* first time */
std::cout << current->val << " ";
rightmost->right=current;
current=current->left;
}else{ /* second time */
rightmost->right=nullptr;
current=current->right;
}
}else{ /* nodes which do not have left child */
std::cout << current->val << " ";
current=current->right;
}
}
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 << "Preorder Traversal using Morris Traversal:\n";
preorderTraversal(root);
std::cout << std::endl;
return 0;
}