1

i was trying to do a post order traversal using stack.....but i am gettin an error of type invalid operands to binary,,,,,,,,,please tell me how to overcome this situation. below is the code.

#include <stdio.h>
#include <malloc.h>

struct node
{
    struct node *left;
    char data;
    struct node *right;
};

struct node *buildtree(int);
void post_order(struct node*);

char  a[] = {'a','b','c','d','e','f','g','\0','\0','h','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'};

int main()
{
    struct node *root;
    root = buildtree(0);
    printf("pre order traversal:\n");
    post_order(root);
}

struct node *buildtree(int n)
{
    struct node *temp = NULL;
    if(a[n] != '\0')
    {
        temp = (struct node*)malloc(sizeof(struct node));
        temp->left = buildtree(2*n+1);
        temp->data = a[n];
        temp->right = buildtree(2*n+2);
    }

    return temp;
}

void post_order(struct node *root)
{
    struct node* stack[40];
    struct node* ptr;
    int top = 1;
    stack[1] = NULL;
    ptr = root;
    while(ptr != NULL)
    {
        top = top + 1;
        stack[top] = ptr;

        if((ptr->right) != NULL)
        {
            top = top + 1;
            stack[top] = -1 * (ptr->right);//how can i assign negative values on the stack.
        }

        ptr = ptr->left;
    }

    ptr = stack[top];
    top = top - 1;

    while(ptr > 0)
    {
        printf("%c", ptr->data);
        ptr = stack[top];
        top = top - 1;
    }

    if(ptr < 0)
    {
        ptr = (-1) * (ptr);
        while(ptr != NULL)
        {
            top = top + 1;
            stack[top] = ptr;
            if(ptr->right != NULL)
            {
                top = top + 1;
                stack[top] = (-1) * (ptr->right);
            }

            ptr = ptr->left;
        }
    }
}
sandyroddick
  • 77
  • 4
  • 16
  • 1
    In the future please make an effort to properly (and cleanly) format your code. Not only is it easier to read, it makes others far more likely to reply. – Chris Eberle Aug 03 '11 at 05:31
  • @sandyroddick - Pointer doesn't allow multiplication/division. It works only for addition/subtraction. However, whether it is returning to a safe location or not depends on the sequence length you are operating on. – Mahesh Aug 03 '11 at 05:49
  • @mahesh : then how will i assign negative values to stack contents..please help me – sandyroddick Aug 03 '11 at 06:02
  • @Mahesh pointer multiplication and division are fine, they're just meaningless for most purposes. The problem with this code is the stack pointers are being overloaded to be a tuple of pointer and boolean value, assuming the top most bit of the pointer is never used. – Steve-o Aug 03 '11 at 06:11

1 Answers1

0

Struct node is an user defined object and you are trying to apply the multiplication operator over it. Declare an integer array or integer of stack and convert the pointer to an integer and push on to the stack. While popping convert the integer to the struct node* as like below.

int x = reinterpret_cast<int>(<your struct node pointer>);
x= x* -1;
push(x);

in popping

int y = pop();
y= y*-1;
struct node *n = reinterpret_cast<struct BTNode*>(y);

In this way you can work out on this problem.

sunil
  • 1