dancks 4 Light Poster

I wrote this test program to test the binary tree for a particular purpose. The intent is to make 2 new tree nodes with new data and destroy the data of the parent data, as its no longer needed (essentially its supposed to imitate "splitting" but my design calls for some data to be the same in both lists, hence creating and destroy every time I "split" a node. The other stuff (RELATION from relation.h and related functions), I tested with valgrind. I know for a fact it works and doesn't leak. I'd rather not post it to keep the question small. I can't figure out what I did wrong here: I used malloc so scope shouldn't be an issue. I passed a double pointer (though I shouldn't have to) in case there was a problem with passing

table.h:

typedef struct tnn {
    int bottom;
    RELATION * table;
    struct tnn *left;
    struct tnn *right;
} TABLE_MAP;
TABLE_MAP * getnewtablemap(RELATION * psh)
{
    TABLE_MAP * nnew = 0;
    nnew = (TABLE_MAP *)malloc(sizeof(TABLE_MAP));
    if(!nnew)
    {
        printf("couldn't allocate %d bytes\n",(int)sizeof(TABLE_MAP));
    }
    nnew->bottom=1;
    nnew->table=psh;
    nnew->left=0;
    nnew->right=0;
    return nnew;
}
void splittablemap(TABLE_MAP **old,TABLE_MAP *left,TABLE_MAP * right)
{
    (*old)->bottom=0;
    destroyrelation2(&((*old)->table));
    (*old)->left = left;
    (*old)->right = right;
}
void printtablemap(TABLE_MAP *a,char *depth)
{
    int i=0;
    while(depth[i]){i++;}
    depth[i] = '\t';
    depth[++i] = '\0';
    if(a->left)
    {
        printtablemap(a->left,depth);
    }
    if(a->bottom)
    {
        printf("%sNODE: Relation: ",depth);
        printrelation2(a->table);
        printf("\n");
    }
    else
    {
        printf("%sNODE\n",depth);
    }
    if(a->right)
    {
        printtablemap(a->right,depth);
    }
    i=0;
    while(depth[i]){i++;}
    depth[--i] = '\0';
}

test thingamajig.c:

#include <stdio.h>
#include <math.h>
#include "relation.h"
#include "table.h"
#define TEST_DEPTH 3
char readfrom[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void recurse(TABLE_MAP ** dostuff,int depth)
{
    if(depth<TEST_DEPTH)
    {
        RELATION *s1=0;
        RELATION *s2=0;
        int size1=rand()%5;
        int size2=rand()%5;
        for(i=0;i<size1;i++)
        {
            pushlist(&s1,getnewrelation(readfrom[rand()%26]));
        }
        for(i=0;i<size2;i++)
        {
            pushlist(&s2,getnewrelation(readfrom[rand()%26]));
        }
    TABLE_MAP *t1 = getnewtablemap(s1);
    TABLE_MAP *t2 = getnewtablemap(s2);
    splittablemap(dostuff, t1, t2);
        recurse(&((*dostuff)->left),depth+1);
        recurse(&((*dostuff)->right),depth+1);
    }
}
int main(int argc, char *argv[])
{
    TABLE_MAP * start = getnewtablemap(createrelation("ABCDEFG"));
    recurse(&start);
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    char deep[50]; deep[0] = '\0';
    printtablemap(start,deep);
    destroytablemap(&start);
    return 0;
}

result:

            NODE: Relation: SFRZ
        NODE
            NODE: Relation: 
    NODE
            NODE: Relation: ZL
        NODE
            NODE: Relation: 
NODE
            NODE: Relation: FP
        NODE
            NODE: Relation: 
    NODE
            NODE: Relation: HLQD
        NODE
            NODE: Relation: QRC

should look like:

            NODE: Relation: SFRZ
        NODE
            NODE: Relation: AZ
    NODE
            NODE: Relation: ZL
        NODE
            NODE: Relation: ZAZ
NODE
            NODE: Relation: FP
        NODE
            NODE: Relation: AZ
    NODE
            NODE: Relation: HLQD
        NODE
            NODE: Relation: QRC

Only the bottom nodes should have data, and every bottom node should have data. The data itself is random. So my program is leaking the data that's supposed to be held by the tree.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.