Ich habe eine Hausaufgaben, die von mir bitten, eine Struktur von binären Suchbaum zu schaffen, wo seine Knoten des binären Suchbaum eine andere binäre Suchbaum ist. Der erste BST hat die Namen von Studenten und der andere hat die Vornamen und id. Auch wenn jemand hat den gleichen Nachnamen mit einem anderen Studenten muss ich nicht einen anderen „Namen“ Knoten erstellen, aber ich habe in der bestehenden „Nachname“ Knoten einen anderen „Vorname und Id“ Knoten zu erstellen. Um genauer zu sein:
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;
Mein Hauptproblem ist, wie ein anderen nameANDid Knoten für jeden Vornamen erstellen Ich weil mit dem folgenden Code fand ich erstellen 2 BST ein für die Namen und eine andere für die Namen, aber ich würde wie zum Beispiel sein mag: Wenn ich diese Studenten habe
Stallone Sylvester 11111111
Stallone Noah 22222222
Norris Chuck 33333333
Hogan Hulk 44444444
Hogan Daniel 55555555
Ich möchte, dass sie wie folgt speichern: .........
Stallone Sylvester 11111111
Noah 22222222
Norris Chuck 33333333
Hogan Hulk 44444444
Daniel 55555555
Statt dessen nehme ich etwas wie: ...........
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
Ich werde einige Funktionen in Ordnung bringen hier genauer zu sein
Die Ladefunktion lädt die Namen aus einem txt-Dokument.
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);
}
woher
struct node temp;//just a node pointer
struct node *top=NULL; //shows the top of the tree
Die addnode Funktion ist: ...
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;
}
Und die add_node_nameANDid () Funktion ist wie die vorherige Funktion, aber es hat einige Variablen geändert:
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;
}
Sorry für die riesige Quellcode, ich lade gerade, aber es wäre sehr schwierig sein, ohne dass dies zu erklären.
Ich denke, dass ich habe zwei Probleme, aber ich habe nicht das Wissen, sich zu lösen.
FIRST: muß ich für jeden Nachnamen Knoten unterschiedlichen Vornamen BST erstellen, und ich denke, dass ich das nicht tun, aber ich weiß nicht, wie das zu tun ...
Irgendwelche Vorschläge?













