#include <stdio.h>
#include <stdlib.h>
struct
node
{
int
data;
struct
node *left, *right;
};
struct
node* newNode(
int
data)
{
struct
node* temp = (
struct
node*)
malloc
(
sizeof
(
struct
node));
temp->data = data;
temp->left = temp->right = NULL;
return
temp;
}
void
morrisTraversalPreorder(
struct
node* root)
{
while
(root)
{
if
(root->left == NULL)
{
printf
(
"%d "
, root->data );
root = root->right;
}
else
{
struct
node* current = root->left;
while
(current->right && current->right != root)
current = current->right;
if
(current->right == root)
{
current->right = NULL;
root = root->right;
}
else
{
printf
(
"%d "
, root->data);
current->right = root;
root = root->left;
}
}
}
}
void
preorder(
struct
node* root)
{
if
(root)
{
printf
(
"%d "
, root->data);
preorder(root->left);
preorder(root->right);
}
}
int
main()
{
struct
node* root = NULL;
root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->left->left->left = newNode(8);
root->left->left->right = newNode(9);
root->left->right->left = newNode(10);
root->left->right->right = newNode(11);
morrisTraversalPreorder(root);
printf
(
"\n"
);
preorder(root);
return
0;
}