2

I'm having trouble with inserting into my expression tree. My Expression tree is made up of Expression nodes. These Expression nodes contain either an enum, Operation, or a string. Each Expression node also having a leftArgument/rightArgument pointers to Expressions (leftNode address and rightNode address).

Of course, my addNode function is clearly not implemented fully, however I expect this code to insert (5) Expression nodes, and each being inserted to the left, creating a very left-heavy tree.

However, it seems to only be rewriting the Expression node in the second level of my tree when executing. There is something wrong with my pointers/addresses within my main() or addNode() function and after hours, I can't seem to figure it out.

Any hints or observations would be greatly appreciated! Thank you so much.

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "expressions.h"

typedef enum Operation {
    FIRST = 1,
    REST = 2,
    CONS = 3,
} Operation;

union Data {
    Operation operation;
    char *string;
};

struct Expression {
    Data data;
    Expression *leftArgument;
    Expression *rightArgument;
};

char* eval(Expression e) {
    return "0";
}

Expression *addNode(Expression *e, Expression *tree) {
    if (tree == NULL) {
        tree = malloc(sizeof(Expression));
        tree->data = e->data;
        tree->leftArgument = NULL;
        tree->rightArgument = NULL;

        printf("added new node\n");
    } else if (tree->data.operation == FIRST || tree->data.operation == REST) {
        printf("%d\n", tree->data.operation);
        addNode(e, tree->leftArgument);
    } 

    return tree;
}

int main() {
    Expression *tree = NULL;

    printf("----------[INSERT]-----------\n");
    Expression e1;
    e1.data.operation = FIRST;
    tree = addNode(&e1, tree);
    printf("-----------------------------\n\n");

    printf("----------[INSERT]-----------\n");
    Expression e2;
    e2.data.operation = REST;
    tree = addNode(&e2, tree);
    printf("-----------------------------\n\n");

    printf("----------[INSERT]-----------\n");
    Expression e3;
    e3.data.operation = FIRST;
    tree = addNode(&e3, tree);
    printf("-----------------------------\n\n");

    printf("----------[INSERT]-----------\n");
    Expression e4;
    e4.data.operation = REST;
    tree = addNode(&e4, tree);
    printf("-----------------------------\n\n");
}
  • 1
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Sep 21 '21 at 02:49
  • @AndreasWenzel I'm currently using the gcc compiler and vim instead of an IDE with a debugger. But I was currently in the process of figuring out an online debugger when you were able to point out my bug. Thanks again! – jsmith003138 Sep 21 '21 at 03:08
  • 2
    I was able to spot your error without a debugger, because your program was relatively small. However, the larger your programs gets, the harder it gets to isolate the problem, and the more important it is to be able to use a debugger to find out what is going on. Therefore, I strongly suggest that you have a proper debugger set up in your programming environment. In the long run, it will certainly be worth it. I'm not sure whether online debuggers are good (I have had bad experience with them, but maybe I was just unlucky). – Andreas Wenzel Sep 21 '21 at 03:31

1 Answers1

0

In the recursive function call

addNode(e, tree->leftArgument);

you are discarding the return value which contains the new root of the tree.

You probably intended to write:

tree->leftArgument = addNode(e, tree->leftArgument);
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39