-1

I am trying to traverse a binary tree built with the input data from keyboard. Data is inserted to the binary tree successfully. I have a switch statement, where 'case 3' should traverse (and print) the binary tree with non-recursive Inorder traversal algorithm. However when 'case 3' is called, it gives an EXC_BAD_ACCESS error, which never makes any sense to me. I would be more than happy if someone help me out with this one.

(RootPtr is the top -Level 0- node of the binary tree defined globally; and GetNodeS is basically an initializer function (using malloc) for type StackPtr pointers.)

Thank you all in advance.

Here is the relevant code:

These are struct definitions,

Typedef struct treeItem
{
    int data;
    struct treeItem *left;
    struct treeItem *right;

}Tree , *TreePtr;

typedef struct stackItem
{
    TreePtr t;
    struct stackItem *next;

}Stack , *StackPtr;

These are Push and Pop functions,

void PushS (TreePtr TreePointer)
{
    StackPtr TemPtr;
    GetNodeS(&TemPtr);

    (*TemPtr).t = TreePointer;
    (*TemPtr).next = S;
    S = TemPtr;
}

void PopS(TreePtr TreePointer)
{
    StackPtr TemPtr;
    GetNodeS(&TemPtr);

    if (S != NULL)
    {
        TreePointer = (*S).t;
        TemPtr = S;
        S = (*S).next;

        FreeNodeS(TemPtr);
    }    
    else
        printf("\nEmpty stack!");

}

This is the traversing function,

void iterative (TreePtr TemPtr)
{
    DONE = 0;
    TemPtr = RootPtr;
    S = NULL;

    if(items==0)
    {
        printf("\nTree is empty.\n\n");
        DONE = 1;
    }
    else
    {
        printf("\nIterative nonrecursive inorder traversal:");

        while (DONE == 0)
        {
            if(TemPtr != NULL)
            {
                PushS(TemPtr);
                TemPtr = (*TemPtr).left;
            }
            else
                if(S != NULL)
                {
                    PopS(TemPtr);
                    printf(" %d", (*TemPtr).data); //the line I get the ERROR
                    TemPtr = (*TemPtr).right;
                }
                else
                {
                    DONE = 1;
                }
        }
    }
}

And this is the switch case where I try to call iterative traversing function,

     case 3:
            TreePtr TemPtr;
            GetNode(&TemPtr);

            iterative(TemPtr);

            break;
Basak Oguz
  • 17
  • 7
  • 1
    Post a [MCVE] in your question please. – πάντα ῥεῖ Jan 11 '16 at 13:31
  • In order to be complete and minimal I had to divide my code into blocks and provide only the ones that are relevant to the ERROR. Actual code is about 400 lines, which I believe is not even near to the one I posted. – Basak Oguz Jan 11 '16 at 13:35
  • Why are you ignoring `iterative`'s parameter? – molbdnilo Jan 11 '16 at 13:37
  • You posted what _you think_ is relevant for the error. Anyways it looks we cannot really diagnose what your problem is from these lines. What's that mysterious `GetNode()` for example? – πάντα ῥεῖ Jan 11 '16 at 13:37
  • 2
    It looks like you think that `PopS` modifies its argument; it doesn't. It also looks odd that `PopS` creates a new stack node. I suspect that you haven't tested your stack implementation very thoroughly before using it for the traversal. – molbdnilo Jan 11 '16 at 13:44
  • @molbdnilo Hello. I could not understand what you mean by 'ignore'? I used TemPtr as the parameter for `iterative`. @πάνταῥεῖ Actually I explained the `GetNodeS()` function as it is a simple initializing function (using malloc) for StackPtr type pointers. I thought it would be clear that `GetNode()` is also an initializing function but for TreePtr type pointers, when I use it with a TreePtr type pointer. – Basak Oguz Jan 11 '16 at 13:49
  • @BasakOguz You immediately overwrite it, `TemPtr = RootPtr;`, so the value passed in is ignored and serves no purpose. Since it serves no purpose, you should use a local variable instead. – molbdnilo Jan 11 '16 at 13:52
  • @molbdnilo But it is initialized as an empty and temporary pointer with type TreePtr. I want it to traverse the tree starting at the root node, that is why I initiliazied it as `TemPtr = RootPtr`. – Basak Oguz Jan 11 '16 at 13:59
  • @BasakOguz There is no point in passing that parameter unless its value is relevant to `iterative` (and it isn't, apparently). The variable in `case 3` does nothing apart from leaking memory. – molbdnilo Jan 11 '16 at 14:06

1 Answers1

0

You are dereferencing a null pointer on the line where you get the error. That is the cause of the EXC_BAD_ACCESS error.

if(TemPtr != NULL)
{
    ...
}
else // TemPtr must be NULL here

PopS(TemPtr); does not change TempPtr as it is passed by value.

Then here:

printf(" %d", (*TemPtr).data);

*TemPtr is a null pointer dereference.

PeterSW
  • 4,921
  • 1
  • 24
  • 35