Thursday, September 1, 2011

Tree Traversals without using Recursion


/* Inorder traversal with out using recursion */
int top = -1;
struct node *arr[100];
struct node
{
  char data;
  struct node *left;
  struct node *right;
  int visited;
};
static int postIndex = 6;
int search(char in[],int inStrt, int inEnd, char data);
struct node *buildTree(char in[], char pre[], int inStrt, int inEnd);
struct node* newNode(char data);


int search(char arr[], int inStrt, int inEnd, char data)
{
  int i;
  for(i = inStrt; i <= inEnd; i++)
  {
     if(arr[i] == data)
     {
        return i;
     }
  }
}

struct node *buildTree(char post[], char in[], int inStrt, int inEnd)
{
  if(inStrt > inEnd)
     return NULL;
  // pick the last node from post-order traversal
  // which happens to be the root
  struct node *tNode = newNode(post[postIndex--]);
  // if this node has no childern then return
  if(inStrt == inEnd)
     return tNode;
  // find the index of this node in in-order traversal
  int inIndex = search(in, inStrt, inEnd, tNode->data);
  tNode->right = buildTree(post, in, inIndex+1, inEnd);
  tNode->left = buildTree(post, in, inStrt, inIndex -1);
  return tNode;
}

struct node* newNode(char data)
{
  struct node *node = (struct node *)malloc(sizeof(struct node));
  node->data = data;
  node->left = NULL;
  node->right = NULL;
  node->visited = 0;
  return node;
} 
void push (struct node *root)
{
  arr[++top] = root;
}
struct node *pop(void)
{
  return arr[top--];
}
/* Inorder traversal without using recursion */
void printInorder(struct node *root)
{
  struct node *temp;
  if(root == NULL)
    return;
  // Push all node onto the stack
  while(root != NULL)
  {
    push(root);
    root = root->left;
  }
  while(top != -1)
  {
    temp = pop();
    printf("%c \t", temp->data);
    temp = temp->right;
    while(temp != NULL)
    {
       push(temp);
       temp = temp->left;
    }
  }
  printf("top inorder : %d\n", top);
}
/* Pre-order traversal without using recursion */
void printPreorder(struct node *root)
{
  struct node *temp;
  if(root == NULL)
    return 0;
  while(root != NULL)
  {
     printf("%c \t", root->data);
     push(root);
     root = root->left;
  }
  while(top != -1)
  {
     temp = pop();
     temp = temp->right;
     while(temp != NULL)
     {
        printf("%c\t", temp->data);
        push(temp);
        temp= temp->left;
     }
  }
}
/* Post-order traversal without using recursion */
void printPostorder(struct node *root)
{
  struct node *temp;
  if(root == NULL)
    return 0;
  while(root != NULL)
  {
     push(root);
     root = root->left;
  }
  while(top != -1)
  {
     temp = pop();
     push(temp);
     //printf("%c %d\n", temp->data, temp->visited);
     temp = temp->right;
     
     while(temp != NULL && temp->visited == 0)
     {
          temp->visited = 1;
          push(temp);
          temp= temp->left;
          
     } 
     
     temp = pop();
     printf("%c\t", temp->data);   
  }
}

main()
{
  char post[] = {'C', 'F', 'G', 'D', 'B', 'E', 'A'};
  char in[] = {'C', 'B', 'F', 'D', 'G', 'A', 'E'};
  int len = sizeof(post)/sizeof(post[0]);
  int i;
  struct node *root = buildTree(post, in, 0, len-1);
  printf("Print the in order traversal\n");
  printInorder(root);
  printf("Print the pre-order traversal\n");
  printPreorder(root);
  printf("\nPrint the post-order traversal\n");
  printPostorder(root);  
}

No comments:

Post a Comment