英文:
Issues with searching within a binary tree
问题
以下是您要翻译的代码部分:
tree* find_node(tree *root, unsigned int index){
if(root==NULL){
return NULL;
}else if(root->value==index){
return root;
}else if(root->value<=index){
find_node(root->left, index);
}else if(root->value>index){
find_node(root->right, index);
}
}
void create_node_link(tree **root, int value, int index, int link_type){
tree *new_node=malloc(sizeof(tree));
if(new_node!=NULL){
new_node->left=NULL;
new_node->right=NULL;
new_node->value=value;
}
tree *position=find_node(*root, index);
if(position==NULL){
printf("NOT FOUND");
getch();
return;
}
if(link_type==1)
link_left(position, new_node);
else if(link_type==2)
link_right(position, new_node);
viz_tree(*root);
}
希望这对您有所帮助。
英文:
I am trying to add nodes to a binary tree, by searching the node to which the new node is supposed to be linked, sometimes my function works if I keep going to the left branch only, or to the right branch, if I have added nodes to the left one then trying to add nodes to the right, my function finds the right node but return root
does not get triggered and recursion continues till the root is NULL;
I am using GCC on Ubuntu, if it has anything to do with it.
Here is my code:
tree* find_node(tree *root, unsigned int index){
if(root==NULL){
return NULL;
}else if(root->value==index){
return root;
}else if(root->value<=index){
find_node(root->left, index);
}else if(root->value>index){
find_node(root->right, index);
}
}
void create_node_link(tree **root, int value, int index, int link_type){
tree *new_node=malloc(sizeof(tree));
if(new_node!=NULL){
new_node->left=NULL;
new_node->right=NULL;
new_node->value=value;
}
tree *position=find_node(*root, index);
if(position==NULL){
printf("NOT FOUND");
getch();
return;
}
if(link_type==1)
link_left(position, new_node);
else if(link_type==2)
link_right(position, new_node);
viz_tree(*root);
}
I just can't wrap my head around why return root
doesn't terminate the function.
My tree is stored in the following way:
typedef struct my_tree tree; // in the header file
then in the header_name.c the struct looks like this:
struct my_tree{
unsigned int value;
tree *left;
tree *right;
};
The root is initialized the following way:
tree *root=create_node(1); //in the main program
tree *create_node(int value){ //function in header.c file
tree* result=malloc(sizeof(tree));
if(result!=NULL){
result->left=NULL;
result->right=NULL;
result->value=value;
}
return result;
}
答案1
得分: 1
由于递归调用的返回值从未被使用。初始调用只能返回NULL
或root
,但永远不会返回递归调用中的值。你应该返回这些值以在初始调用中获得该值:
tree* find_node(tree *root, unsigned int index) {
if (root == NULL) {
return NULL;
} else if (root->value == index) {
return root;
} else if (root->value < index) { // 永远不会相等,所以使用 < 而不是 <=
return find_node(root->left, index);
}
return find_node(root->right, index); // 不需要 if 语句
}
英文:
Because the recursice calls' return values never get used. The initial call can only return either NULL
or root
, but never the values in the recursive calls. You should return those to get that value in the initial call:
tree* find_node(tree *root, unsigned int index) {
if (root == NULL) {
return NULL;
} else if (root->value == index) {
return root;
} else if (root->value < index) { // can never be equal, so < over <=
return find_node(root->left, index);
}
return find_node(root->right, index); // no if needed
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论