adding preorder notes and code
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 25 KiB |
@@ -40,8 +40,7 @@ Instead of using extra memory (like recursion stack or an explicit stack), Morri
|
||||
- Repeat until you traverse the entire tree.
|
||||
|
||||
```cpp
|
||||
vector<int> inorderTraversal(TreeNode* root) {
|
||||
vector<int> result;
|
||||
void inorderTraversal(TreeNode* root) {
|
||||
TreeNode *current=root;
|
||||
TreeNode *rightmost;
|
||||
while(current!=NULL){
|
||||
@@ -54,16 +53,16 @@ vector<int> inorderTraversal(TreeNode* root) {
|
||||
rightmost->right=current;
|
||||
current=current->left;
|
||||
}else{ /* second time */
|
||||
result.push_back(current->val);
|
||||
std::cout << current->val << " ";
|
||||
rightmost->right=NULL;
|
||||
current=current->right;
|
||||
}
|
||||
}else{ /* nodes which do not have left child */
|
||||
result.push_back(current->val);
|
||||
std::cout << current->val << " ";
|
||||
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,
|
||||
|
||||

|
||||

|
||||
|
||||
The testing program prints:
|
||||
|
||||
@@ -85,9 +84,7 @@ Inorder Traversal using Morris Traversal:
|
||||
## 21.3 Morris Traversal - Pre Order
|
||||
|
||||
```cpp
|
||||
vector<int> preorderTraversal(TreeNode* root) {
|
||||
vector<int> result;
|
||||
int index=0;
|
||||
void preorderTraversal(TreeNode* root) {
|
||||
TreeNode *current=root;
|
||||
TreeNode *rightmost;
|
||||
while(current != nullptr){
|
||||
@@ -97,7 +94,7 @@ vector<int> preorderTraversal(TreeNode* root) {
|
||||
rightmost=rightmost->right;
|
||||
}
|
||||
if(rightmost->right==nullptr){ /* first time */
|
||||
result.push_back(current->val);
|
||||
std::cout << current->val << " ";
|
||||
rightmost->right=current;
|
||||
current=current->left;
|
||||
}else{ /* second time */
|
||||
@@ -105,12 +102,23 @@ vector<int> preorderTraversal(TreeNode* root) {
|
||||
current=current->right;
|
||||
}
|
||||
}else{ /* nodes which do not have left child */
|
||||
result.push_back(current->val);
|
||||
std::cout << current->val << " ";
|
||||
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
|
||||
|
||||
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 |
@@ -22,12 +22,12 @@ void inorderTraversal(TreeNode* root) {
|
||||
rightmost->right=current;
|
||||
current=current->left;
|
||||
}else{ /* second time */
|
||||
std::cout << current->val << " ";
|
||||
std::cout << current->val << " ";
|
||||
rightmost->right=NULL;
|
||||
current=current->right;
|
||||
}
|
||||
}else{ /* nodes which do not have left child */
|
||||
std::cout << current->val << " ";
|
||||
std::cout << current->val << " ";
|
||||
current=current->right;
|
||||
}
|
||||
}
|
||||
|
||||
53
lectures/21_trees_IV/preorder_main.cpp
Normal file
53
lectures/21_trees_IV/preorder_main.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user