简体   繁体   中英

Recursively put InOrder Binary Search Tree data into an array in C

I am trying to recursively insert the data stored in each node of my binary search tree into an array, sorted via the InOrder logic of the code.

This is the snippet of the two functions I am using. The "bst_getordered" one's parameters and type cannot be changed, only its contents. The "node_getordered" can be changed in any way necessary.

void bst_getordered(bst* b, void* v)
{
    int i = 0;
    if (b == NULL || v == NULL) {
      return;
    }
    node_getordered(b->top, (char*) v, i);
}

int node_getordered(bstnode* node, char* v, int i)
{
   if (node == NULL) {
       return i;
   }
   node_getordered(node->left, v, i);
   v[i] = (char) node->data;
   i++;
   node_getordered(node->right, v, i);
   return i;
}

It should store all data in the tree, sorted, into the array. However it does not do it and I cannot figure out why... I think I am supposed to use a double pointer to increment the address but I can't figure out how to go about it... my syntax knowledge is lacking in that field...

[EDIT 1] This is a snippet of the test program I use to make sure the code works:

void test_getordered(void)
{
       int i, sc;
       char words1[WORDS][STRSIZE] = {"it", "is", "a", "truth", 
       "universally", "acknowledged", "that",  "a", "single", "man", "in", 
       "possession", "of", "a", "good", "fortune", "must", "be", "in", 
       "want", 
       "of", "a", "wife"};
       char words2[WORDS][STRSIZE];
       bst* b = bst_init(STRSIZE, mystrcmp, myprintstr);
       bst_insertarray(b, words1, WORDS);
       assert(bst_size(b)==18);
       bst_getordered(b, words2);
       printf("%lu %s\n", sizeof(words2), words2[0]);
       for(i=0; i<17; i++){
                 sc = strcmp(words2[i], words2[i+1]);
                 assert(sc<0);
       }
       bst_free(&b);
       assert(b==NULL);
}

[EDIT 2] These are the structures of my node and tree. I suspect I need to increment the counter by the tree->elsz somehow in order to properly traverse through the array:

struct bstnode {
   void* data;
   struct bstnode* left;
   struct bstnode* right;
};
typedef struct bstnode bstnode;

struct bst {
   bstnode* top;
   /* Data element size, in bytes */
   int elsz;
};
typedef struct bst bst;

You can send the address of i variable and increment the value of it after populating that index.

void bst_getordered(bst* b, void* v)
{
    if (b == NULL || v == NULL) {
        return;
    }
    int i = 0;
    node_getordered(b->top, (char*) v, &i);
}

void node_getordered(bstnode* node, char* v, int* i)
{
    if (node == NULL) {
        return;
    }
    node_getordered(node->left, v, i);
    v[*i] = (char) node->data;
    (*i)++;
    node_getordered(node->right, v, i);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM