【问题标题】:Flattening a binary search tree into a list将二叉搜索树展平为列表
【发布时间】:2015-12-10 17:25:06
【问题描述】:
typedef struct node *blah;

int *breath_search(struct node *root){

    int *numbers = calloc(20,sizeof(*numbers)); int listpointer = 0;
    struct node **currentlist = calloc(20,sizeof(struct node*));
    struct node **updatedlist = calloc(20,sizeof(struct node*));
    currentlist[0] = root;
    int iterations = 1;

    int depths = 3;
    while(depths){


        int i = 0; int j;
        for(j=0;j<iterations;j++){
            if(currentlist[j] == NULL){
                updatedlist[i] = NULL; i++;
                updatedlist[i] = NULL; i++;
                numbers[listpointer] = 0; listpointer++;
            }
            else if(currentlist[j] != NULL){
                updatedlist[i] = currentlist[j]->left; i++;
                updatedlist[i] = currentlist[j]->right; i++;
                numbers[listpointer] = (int) alpabatise(currentlist[j]->newitem.key); listpointer++;
            }
        }

        currentlist = updatedlist;
        updatedlist = (blah[])     {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
        iterations = iterations*2;

        depths--;

    }

    return numbers;
}

我已经查看这段代码几个小时了,但它为什么不起作用是没有意义的。 我打算给函数一个节点,它会返回一个指针给我一个包含二叉树中所有数字的列表。

我的二叉树是这样的

        231
     /      \
    82      247
   /  \     /  \
  80  137 NULL 263

我的函数只返回一个指向列表的指针

231,82,247,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

我期待

231,82,247,80,137,0,263,0,0,0,0,0,0...

【问题讨论】:

  • 你有什么证据表明它正在尝试处理根目录下的任何节点?
  • 我有一个节点列表current list(最初是根)然后我将这些节点左右的所有节点存储在updated list这个新的更新列表然后变成current list每次都继续创建current list,所有节点都低于先前的深度,除非我的逻辑是错误的
  • 我看的不多,但要检索该列表,您可以简单地执行“中序遍历”并将数据附加到列表中。
  • 这能编译吗?? updatedlist = (blah[]) {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}; 行我认为这不会编译。我尝试了这样的事情,但它没有编译。检查这个 :: ideone.com/ahyPcm 而不是分配一个新数组到 updatedList 尝试再次 calloc 更新列表
  • @user007 是的!成功了,谢谢!但我不知道为什么那条线会导致错误,我只是将它们全部初始化为 NULL,我会记住这一点。你也可以把你的评论作为答案,这样我就可以投票了

标签: c binary-tree binary-search-tree breadth-first-search


【解决方案1】:

我相信你的代码中的错误是行 ::

updatedlist = (blah[]) {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};

我怀疑这是一个有效的语法。由于您正在尝试分配一个新数组,您可以在其中保存刚刚访问的节点的子节点,因此建议 calloc 一个新数组,然后您可以在代码中使用它。

所以,上面提到的那一行应该改成这个::

updatedlist = calloc(20,sizeof(struct node*));

在分配这么多内存时,您应该考虑的几点是,释放不再使用的内存,因为 C 没有明确地为您执行此操作,您需要自己处理,以避免任何内存泄漏。 由于在while 循环的每次迭代之后,currentList 是无用的,您应该添加一个语句(在将updatedList 分配给currentList 之前)

free(currentList);

并在程序结束时释放updatedList

其次,您当前正在做的事情类似于二叉树的级别顺序遍历。因此,您可以尝试使用 STL queue,并且不需要像您正在做的那样创建和交换数组。像这样的东西::

int *breath_search(struct node *root){

    int *numbers = calloc(20,sizeof(*numbers));
    int listpointer = 0;
    queue<node*> q;
    q.push(root);
    int iterations = 1;

    int depths = 3;
    while(depths){
        int i = 0, j;
        for(j=0; j<iterations; j++){
            node* currentNode = q.pop();
            if(currentNode == NULL){
                q.push(NULL);
                q.push(NULL);
                numbers[listpointer] = 0;
                listpointer++;
            }
            else if(currentNode != NULL){
                q.push(currentNode->left);
                q.push(currentNode->right);
                numbers[listpointer] = (int) alpabatise(currentlist[j]->newitem.key);
                listpointer++;
            }
        }
        iterations = iterations*2;
        depths--;
    }
    return numbers;
}

我相信这将是一种更好的方法,因为您不必继续分配和释放内存,因此可以减少开销。我用的是STL队列,你绝对可以用自己的队列。

【讨论】:

    猜你喜欢
    • 2019-04-12
    • 2016-06-05
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    相关资源
    最近更新 更多