Mam pracę domową, która zadawać mi stworzyć struct z binarne drzewo poszukiwań gdzie jego węzeł drzewa binarnego wyszukiwania to kolejny wyszukiwania binarne drzewo. Pierwszy BST ma nazwiska uczniów, a drugi ma imiona i id. Także jeśli ktoś ma to samo nazwisko z innym studentem I nie musi tworzyć inny węzeł „nazwisko”, ale muszę tworzyć wewnątrz istniejącego węzła „nazwisko”, kolejny „imię i id” węzła. Być bardziej specyficznym:
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;
Moim głównym problemem jest to, jak stworzyć inny węzeł nameANDid dla każdego FirstName znalazłem bo z następującego kodu tworzę 2 Jedna BST dla nazwisk i drugą dla nazw, ale chciałbym być taki jak na przykład: Jeśli mam tych studentów
Stallone Sylvester 11111111
Stallone Noah 22222222
Norris Chuck 33333333
Hogan Hulk 44444444
Hogan Daniel 55555555
Chcę, aby przechowywać je w ten sposób: .........
Stallone Sylvester 11111111
Noah 22222222
Norris Chuck 33333333
Hogan Hulk 44444444
Daniel 55555555
Zamiast tego biorę coś takiego: ...........
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
Położę tu kilka funkcji, aby być bardziej konkretny
Funkcja obciążenia ładuje nazwiska z dokumentu 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);
}
gdzie
struct node temp;//just a node pointer
struct node *top=NULL; //shows the top of the tree
Funkcja AddNode jest: ...
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;
}
A add_node_nameANDid () funkcja jest podobna do poprzedniej funkcji, ale ma pewne zmienne zmianie:
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;
}
Niestety dla kodu źródłowego ogromne, że po prostu przesłać ale byłoby to bardzo trudne do wyjaśnienia bez tego.
Myślę, że mam dwa problemy, ale nie mam wiedzy, aby je rozwiązać.
PIERWSZY: trzeba utworzyć inny firstname BST dla każdego węzła nazwisku i myślę, że nie robię tego, ale nie wiem jak to zrobić ...
Jakieś sugestie?













