简体   繁体   English

将InOrder二进制搜索树数据递归放入C中的数组

[英]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. 我试图将存储在我的二进制搜索树的每个节点中的数据递归插入到数组中,并通过代码的InOrder逻辑进行排序。

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. “ bst_getordered”一个人的参数和类型不能更改,只能更改其内容。 The "node_getordered" can be changed in any way necessary. 可以以任何必要的方式更改“ node_getordered”。

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: [编辑1]这是我用来确保代码正常工作的测试程序的摘要:

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. [编辑2]这些是我的节点和树的结构。 I suspect I need to increment the counter by the tree->elsz somehow in order to properly traverse through the array: 我怀疑我需要以某种方式增加tree-> elsz的计数器,以正确遍历数组:

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. 您可以在填充该索引后发送i变量的地址并增加其值。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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