我有一个家庭作业,从我这里要求创建一个二叉搜索树的结构,其中二叉搜索树的节点是另一个二叉搜索树。第一个BST有学生的姓氏,另一个有姓氏和身份证。另外,如果某人与另一个学生姓氏相同,我不能创建另一个“姓氏”节点,但我必须在现有的“姓氏”节点内创建另一个“名字和ID”节点。更具体地讲:二进制搜索树内的二进制搜索树
typedef struct nameANDid{ //name and id nodes
char first[20];
int ID;
struct nameANDid *nleft;
struct nameANDid *nright;
}yohoho;
typedef struct node{ //surname nodes
char last[20];
struct nameANDid yohoho;
struct node *left;
struct node *right;
}node;
我的主要问题是如何创建的每个名字我用下面的代码,因为发现了一个不同的nameANDid节点创建2 BST一个姓氏,另一个名字,但我想要像例如: 如果我有这些学生
Stallone Sylvester 11111111
Stallone Noah 22222222
Norris Chuck 33333333
Hogan Hulk 44444444
Hogan Daniel 55555555
我想将它们存储这样的:.........
Stallone Sylvester 11111111
Noah 22222222
Norris Chuck 33333333
Hogan Hulk 44444444
Daniel 55555555
相反,这个我的助教柯类似:...........
Stallone Sylvester 11111111.
Noah 22222222
Chuck 33333333
Hulk 44444444
Daniel 55555555
Norris Sylvester 11111111.
Noah 22222222
Chuck 33333333
Hulk 44444444
Daniel 55555555
Hogan Sylvester 11111111.
Noah 22222222
Chuck 33333333
Hulk 44444444
Daniel 55555555
我会把在这里为了一些功能更具体
负载函数加载从txt文件的名称。
void loadData(struct node *temp){
int i;
FILE *fp;
fp=fopen(FILENAME,"r");
if (fp == NULL) printf("File does not exist\n");
for (i=0; i<5; i++){
fscanf(fp,"%s",&temp->last);
fscanf(fp,"%s",&temp->yohoho.first);
fscanf(fp,"%d",&temp->yohoho.ID);
top=add_node(top,temp); //this function create a surname node
}
fclose(fp);
printf("\n\nFile loaded\n");
}
其中
struct node temp;//just a node pointer
struct node *top=NULL; //shows the top of the tree
的addnode的功能是:...
struct node * add_node (struct node *top, struct node *temp){
struct node *newNode;
if (top == NULL){
newNode=(struct node *)malloc(sizeof(struct node));
temp->left=NULL;
temp->right=NULL;
if (memcpy(newNode,temp,sizeof(struct node)) == NULL){
printf("Node addition failed\n");
return NULL;}
else {
topname=add_node_nameANDid(topname,&temp->yohoho); //Call the add_node_nameANDid to create a new name node in the other tree
return newNode;}
}
else {
if (stricmp(temp->last,top->last) < 0){ //Insert node surname left
top->left=add_node(top->left,temp);}
else if (stricmp(temp->last,top->last) == 0){
topname=add_node_nameANDid(topname,&temp->yohoho); //Call the add_node_nameANDid to create a new name node in the other tree if i have the same surname
}
else {
top->right=add_node(top->right,temp);
}
return top;
}
return NULL;
}
而且add_node_nameANDid()函数是像以前的功能,但它有一些变量改变:
struct nameANDid * add_node_nameANDid (struct nameANDid *topname, struct nameANDid *temp2){
struct nameANDid *newNode_nameANDid;
if (topname == NULL){
newNode_nameANDid=(struct nameANDid *)malloc(sizeof(struct nameANDid));
temp2->nleft=NULL;
temp2->nright=NULL;
if (memcpy(newNode_nameANDid,temp2,sizeof(struct nameANDid)) == NULL){
printf("Node addition failed\n");
return NULL;}
else {
return newNode_nameANDid;}
}
else {
if (stricmp(temp2->first,topname->first) <= 0){
topname->nleft=add_node_nameANDid(topname->nleft,temp2);}
else {
topname->nright=add_node_nameANDid(topname->nright,temp2);}
return topname;
}
return NULL;
}
对不起f或者我刚刚上传的庞大源代码,但如果没有这个,很难解释。
我认为我有两个问题,但我没有解决这些问题的知识。
第一:我不得不为每个节点姓创建不同的名字BST,我认为我不这样做,但我不知道该怎么做......
有什么建议?
卫生署!很好的问题................................ – 2011-05-29 11:23:44
你编译过这些代码吗?它似乎充满了无法编译的错误。 – Hogan 2011-05-29 11:29:03
您应该更具体一些,找出问题并发布问题源代码和问题。这是太长了.. – 2011-05-29 11:47:36