问题
I am following a book ,Problem Solving & Program Design in C, to learn C. In this book, they gave all necessary parts to build a binary search tree.... But, My implementation didn't work. Here is insertion part;
void
add_to_t(tree_node_t *oldTreep, // input/output - binary search tree
tree_element_t ele) // input - element to add
{
oldTreep = tree_insert(oldTreep, ele);
}
tree_node_t * tree_insert(tree_node_t *oldTreep, tree_element_t ele)
{
if(oldTreep == NULL){
oldTreep = TYPED_ALLOC(tree_node_t);
strcpy(oldTreep->element.name, ele.name);
strcpy(oldTreep->element.sName, ele.sName);
oldTreep->element.seatClass = ele.seatClass;
oldTreep->leftp = NULL;
oldTreep->rightp = NULL;
}
else if (strcmp(oldTreep->element.name, ele.name)==0){
/* duplicate key - no insertion */
}
else if (strcmp(oldTreep->element.name, ele.name)>0){
oldTreep->rightp = tree_insert(oldTreep->rightp, ele);
}
else
{
oldTreep->leftp = tree_insert(oldTreep->leftp, ele);
}
return(oldTreep);
}
My scan_passenger funvtion(I am passing ele from result of this function call);
void scan_passenger(tree_element_t *pass)
{
char passName[10], passSname[10];
int classNum;
printf("\nEnter the Name of passenger to add the binary search tree> ");
scanf("%s", passName);
printf("Enter the Surname of passenger to add the binary search tree> ");
scanf("%s", passSname);
printf("Enter the class number of passenger to add the binary search tree> ");
scanf("%d", &classNum);
strcpy(pass->name, passName);
strcpy(pass->sName, passSname);
pass->seatClass = classNum;
}
And my typdefs and headers if needs;
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define TYPED_ALLOC(type) (type *)malloc(sizeof (type))
typedef struct tree_element_s {
char name[10];
char sName[10];
int seatClass;
}tree_element_t;
typedef struct tree_node_s {
tree_element_t element;
struct tree_node_s *leftp, *rightp;
}tree_node_t;
My problem is that it doesn't create a root of binary search tree. When I try to add a new element to heap, it seems, it creates a new node. When I traced my code, it seems every instance of this functions returns NULL. I am trying to say every time when I call tree_insert, it goes first if statement(Thinks root is NULL)... Sorry for my bad English. And I may made some mistake when talking about coding terminology(it may because, I returned to study C from that book after absence of 1 year.So I may mixed them) Thanks in advance.
回答1:
In add_to_t, you update oldTreep, but since it is a local variable, the new value gets lost as soon as you leave the function.
You could, e.g., return the new value of oldTreep to the caller, which updates his oldTreep with the return value
tree_node_t
add_to_t(tree_node_t *oldTreep, // input/output - binary search tree
tree_element_t ele) // input - element to add
{
return tree_insert(oldTreep, ele);
}
...
myRootOfTheTree = add_to_t (myRootOfTheTree, element);
Another solution is to always have one dummy element in the tree.
来源:https://stackoverflow.com/questions/12127335/binary-search-tree-insertion-doesnt-work