我有一個家庭作業,從我這裏要求創建一個二叉搜索樹的結構,其中二叉搜索樹的節點是另一個二叉搜索樹。第一個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