Pages

Showing posts with label zigzag order traverse of tree. Show all posts
Showing posts with label zigzag order traverse of tree. Show all posts

Monday, 27 August 2012

Zigzag traversal of tree or spiralorder traversal of binary tree




Idea:
maintain array keep all theelemnts in arry then traverse array in way to get spiral order 

example:
For the given tree,
(1)
(2) (3)
(4) (5) (6) (7)
(8) (9)
Spiral traversal is
1, 2, 3, 7, 6, 5, 4, 8, 9 ….

Array elements after inserting elements will be

1  null  2  3   null  7  6  5  4  null  8  9  null

In array after every level insert null  then traverse in forward direction till null and go next null traverse backward direction to null continue same process



Code:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define QUEUESIZE 20

struct treenode
{
    int data;
    struct treenode *left;
    struct treenode *right;
} *root=NULL;


//building tree

struct treenode * buildtree()
{
    struct treenode *temp1,*temp2,*temp3,*temp4,*temp5,*temp6;
     root=(struct treenode *)malloc(sizeof(struct treenode));
      temp1=(struct treenode *)malloc(sizeof(struct treenode));
       temp2=(struct treenode *)malloc(sizeof(struct treenode));
        temp3=(struct treenode *)malloc(sizeof(struct treenode));
         temp4=(struct treenode *)malloc(sizeof(struct treenode));
          temp5=(struct treenode *)malloc(sizeof(struct treenode));
           temp6=(struct treenode *)malloc(sizeof(struct treenode));
    
     root->data=4;
     root->left=temp1;
     temp1->data=5;
     root->right=temp2;
     temp2->data=6;
     temp1->left=temp3;
     temp3->data=7;
     temp3->left=temp3->right=NULL;
     temp1->right=temp4;
     temp4->data=8;
     temp4->left=NULL;
     temp4->right=NULL;
      temp2->left=temp5;
      temp5->data=6;
      temp2->right=temp6;
      temp6->data=10;
      temp5->left=temp5->right=temp6->left=temp6->right=NULL;
  printf("\n tree buildedsuccessfull"); 
       return root;
}

//print
void print(int *a[],int n)
{
    struct treenode *temp;
    int k=0,i,j,m;
    for(j=0;j<n;)
    {
        if(a[j]==NULL)
        {
            k++;
            j++;
            }
       
        else if(k%2==0)
        {
            temp=a[j];
            j++;
            printf("%d   ",temp->data);
          }   
        else if(k%2==1)
        {
            i=j;
            while(a[i]!=NULL)
            i++;
            for(m=i-1;m>=j;m--)
            {
                temp=a[m];
            printf("%d    ",temp->data);
             }
            j=i;
           
            }
       
        }
}
//zizag order

void zigzag(struct treenode *root)
{
int *array[QUEUESIZE],i=0,j=-1;
struct treenode *k;
array[++j]=root;
array[++j]=NULL;
 while(i<j)
 {
    if(root!=NULL)
    {
    if(root->left!=NULL)
    array[++j]=root->left;
   
    if(root->right!=NULL)
    array[++j]=root->right;
   
    root=array[++i];
    }
    else
    {
    array[++j]=NULL;
    root=array[++i];
    }
   }
  
print(array,j);
}//end

//main

void main()
{
    struct treenode *root;
    int i=1;
    while(i)
    {
        printf("\n enter 1 to build elements");
        printf("\n enter 2 print elements in zigzag order");
        scanf("%d",&i);
   
    switch(i)
    {
    case 1:
         root=buildtree();
         break;
       
    case 2:
        zigzag(root);
        break;   
    }
     }
}