【发布时间】: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