#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct Node
{
int data;
struct Node *left, *right;
} Node;
typedef struct Stack
{
int top;
int capacity;
Node* *array;
} Stack;
Node* newNode( int data )
{
Node* temp = (Node *)malloc( sizeof( Node ) );
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
Stack* createStack( int capacity )
{
Stack* stack = (Stack *)malloc( sizeof( Stack ) );
stack->top = -1;
stack->capacity = capacity;
stack->array = (Node **)malloc( stack->capacity * sizeof( Node* ) );
return stack;
}
int isFull( Stack* stack )
{
return stack->top == stack->capacity - 1;
}
int isEmpty( Stack* stack )
{
return stack->top == -1;
}
void push( Stack* stack, Node* item )
{
if( isFull( stack ) )
return;
stack->array[ ++stack->top ] = item;
}
Node* pop( Stack* stack )
{
if( isEmpty( stack ) )
return NULL;
return stack->array[ stack->top-- ];
}
Node* peek( Stack* stack )
{
return stack->array[ stack->top ];
}
Node* constructTree ( int pre[], int size )
{
Stack* stack = createStack( size );
Node* root = newNode( pre[0] );
push( stack, root );
int i;
Node* temp;
for ( i = 1; i < size; ++i )
{
temp = NULL;
while ( !isEmpty( stack ) && pre[i] > peek( stack )->data )
temp = pop( stack );
if ( temp != NULL)
{
temp->right = newNode( pre[i] );
push( stack, temp->right );
}
else
{
peek( stack )->left = newNode( pre[i] );
push( stack, peek( stack )->left );
}
}
return root;
}
void printInorder (Node* node)
{
if (node == NULL)
return;
printInorder(node->left);
printf("%d ", node->data);
printInorder(node->right);
}
int main ()
{
int pre[] = {10, 5, 1, 7, 40, 50};
int size = sizeof( pre ) / sizeof( pre[0] );
Node *root = constructTree(pre, size);
printf("Inorder traversal of the constructed tree: \n");
printInorder(root);
return 0;
}