Search code examples
data-structurestreestackiterationpreorder

preorder traversal using stack


i'm trying to implement binary tree, pre order traversal using stack. here its popping the last left node and after that root=root->right doesnt seem to work. Pls help. here 7 is being popped out and is being displayed and after the the program is ending.

all the functions are working yet not the desired output

            #include<stdio.h>
            #include<stdlib.h>
            #define maxsize 100
            int a[maxsize];
            int top=-1;
            struct node{
                int data;
                struct node *left, *right;
            };
            struct node *newNode(int data)
            {
                struct node *nn;
                nn=(struct node *)malloc(sizeof(struct node));
                if(!nn)
                    return;
                nn->data=data;
                nn->left=nn->right=NULL;
                return nn;
            };
            void push(struct node *root)
            {
                printf("pushcalled\n");
                if(top!=maxsize-1)
                    a[++top]=root;
            }
            int isempty()
            {
                return(top==-1);
            }
            struct node *pop()
            {
                printf("popcalled\n");
                if(top!=-1)
                {
                    return a[top];
                    top--;
                }
            }
            void deleteStack()
            {
                free(a[top--]);
            }
            void preorder(struct node *root)
            {
                while(1)
                {
                    while(root)
                    {
                        printf("%d\t",root->data);
                        push(root);
                        root=root->left;
                    }
                    printf("hello\n");
                    if(isempty())
                        break;
                    printf("hello\n");
                    root=pop();
                    printf("Popped data is:%d\n",root->data);
                    root=root->right;
                    printf("right data is:%d\n",root->data);
                }
                deleteStack();
            }
            int main()
            {
                int data;
                struct node *root=newNode(10);
                root->left = newNode(11);
                root->left->left = newNode(7);
                root->right = newNode(9);
                root->right->left = newNode(15);
                root->right->right = newNode(8);
                preorder(root);
                return 0;
            }

Solution

  • Your logic is correct but there are some errors in your code.

     root=root->right;
     printf("right data is:%d\n",root->data);
    

    you should check whether the root is null or not before you try to access root->data. That is why you are getting a segmentation fault. So put a condition if(root!=NULL) above printf() statement.

    Another mistake is in the implementation of stack's pop.

           struct node *pop()
           {
                printf("popcalled\n");
                if(top!=-1)
                {
                    return a[top];
                    top--;
                }
            }
    

    it should be like this,

            struct node *pop()
            {
                printf("popcalled\n");
                if(top!=-1)
                {
                    struct node* temp = a[top];
                    top--;
                    return temp;
                }
            }
    

    In your code, when you return a[top]; the line below it i.e. top--; never executes and the value of top remains same.