2018-10-11 14:24:29 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
/**************************
|
|
|
|
@author shrutisheoran
|
|
|
|
**************************/
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
struct Btree
|
|
|
|
{
|
2018-10-11 14:24:29 +08:00
|
|
|
int data;
|
2019-08-21 10:10:08 +08:00
|
|
|
struct Btree *left; //Pointer to left subtree
|
|
|
|
struct Btree *right; //Pointer to right subtree
|
2018-10-11 14:24:29 +08:00
|
|
|
};
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
void insert(Btree **root, int d)
|
|
|
|
{
|
|
|
|
Btree *nn = new Btree(); //Creating new node
|
2018-10-11 14:24:29 +08:00
|
|
|
nn->data = d;
|
|
|
|
nn->left = NULL;
|
|
|
|
nn->right = NULL;
|
2019-08-21 10:10:08 +08:00
|
|
|
if (*root == NULL)
|
|
|
|
{
|
2018-10-11 14:24:29 +08:00
|
|
|
*root = nn;
|
|
|
|
return;
|
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
queue<Btree *> q;
|
2018-10-11 14:24:29 +08:00
|
|
|
// Adding root node to queue
|
|
|
|
q.push(*root);
|
2019-08-21 10:10:08 +08:00
|
|
|
while (!q.empty())
|
|
|
|
{
|
2018-10-11 14:24:29 +08:00
|
|
|
Btree *node = q.front();
|
|
|
|
// Removing parent node from queue
|
|
|
|
q.pop();
|
2019-08-21 10:10:08 +08:00
|
|
|
if (node->left)
|
2018-10-11 14:24:29 +08:00
|
|
|
// Adding left child of removed node to queue
|
|
|
|
q.push(node->left);
|
2019-08-21 10:10:08 +08:00
|
|
|
else
|
|
|
|
{
|
2018-10-11 14:24:29 +08:00
|
|
|
// Adding new node if no left child is present
|
|
|
|
node->left = nn;
|
|
|
|
return;
|
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
if (node->right)
|
2018-10-11 14:24:29 +08:00
|
|
|
// Adding right child of removed node to queue
|
|
|
|
q.push(node->right);
|
2019-08-21 10:10:08 +08:00
|
|
|
else
|
|
|
|
{
|
2018-10-11 14:24:29 +08:00
|
|
|
// Adding new node if no right child is present
|
|
|
|
node->right = nn;
|
|
|
|
return;
|
2019-08-21 10:10:08 +08:00
|
|
|
}
|
2018-10-11 14:24:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
void morrisInorder(Btree *root)
|
|
|
|
{
|
2018-10-11 14:24:29 +08:00
|
|
|
Btree *curr = root;
|
|
|
|
Btree *temp;
|
2019-08-21 10:10:08 +08:00
|
|
|
while (curr)
|
|
|
|
{
|
|
|
|
if (curr->left == NULL)
|
|
|
|
{
|
|
|
|
cout << curr->data << " ";
|
2018-10-11 14:24:29 +08:00
|
|
|
// If left of current node is NULL then curr is shifted to right
|
|
|
|
curr = curr->right;
|
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
else
|
|
|
|
{
|
2018-10-11 14:24:29 +08:00
|
|
|
// Left of current node is stored in temp
|
|
|
|
temp = curr->left;
|
|
|
|
// Moving to extreme right of temp
|
2019-08-21 10:10:08 +08:00
|
|
|
while (temp->right && temp->right != curr)
|
2018-10-11 14:24:29 +08:00
|
|
|
temp = temp->right;
|
|
|
|
// If extreme right is null it is made to point to currrent node (will be used for backtracking)
|
2019-08-21 10:10:08 +08:00
|
|
|
if (temp->right == NULL)
|
|
|
|
{
|
2018-10-11 14:24:29 +08:00
|
|
|
temp->right = curr;
|
|
|
|
// current node is made to point its left subtree
|
|
|
|
curr = curr->left;
|
|
|
|
}
|
|
|
|
// If extreme right already points to currrent node it it set to null
|
2019-08-21 10:10:08 +08:00
|
|
|
else if (temp->right == curr)
|
|
|
|
{
|
|
|
|
cout << curr->data << " ";
|
2018-10-11 14:24:29 +08:00
|
|
|
temp->right = NULL;
|
|
|
|
// current node is made to point its right subtree
|
|
|
|
curr = curr->right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
int main()
|
|
|
|
{
|
2018-10-11 14:24:29 +08:00
|
|
|
// Testing morrisInorder funtion
|
|
|
|
Btree *root = NULL;
|
|
|
|
int i;
|
2019-08-21 10:10:08 +08:00
|
|
|
for (i = 1; i <= 7; i++)
|
2018-10-11 14:24:29 +08:00
|
|
|
insert(&root, i);
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << "Morris Inorder: ";
|
2018-10-11 14:24:29 +08:00
|
|
|
morrisInorder(root);
|
|
|
|
return 0;
|
|
|
|
}
|