开发者

Binary Search Tree Help with pointers between two Structures

I have a home work which is almost done but i have stuck somewhere.I have to warn that it is the first time i'm using pointers and all these weird stuff so i'm pretty lost. My purpose is to read from a txt students data list as (Surname Name ID). The trick is that i have to use one binary search tree to store the Surnames(i have done this) and to create inside the first tree another Binary search tree which stores the first names of the students and the ID (Partially complete). The problem is that when some student have the same surname and different first name i must not create a new node for the surnames but i have to put the new students first name and id inside an existing node of last name. It should be like: Cameron James 12131313

Andrew 17286378(his surname is also Cameron)

The code is:

typedef struct nameANDid{
    char first[20];
    int ID;
    struct node *nleft;
    struct node *nright;
}yohoho;
typedef struct node{  
   char last[20];  
   struct nameANDid yohoho;  
   struct node *left;
   struct node *right;
 }node;
 ///
 struct node temp;
 struct nameANDid temp2;
 struct node *top=NULL;
 struct nameANDid *topname=NULL;
 void loadData();
 struct nameANDid * add_node_nameANDid(struct nameANDid *, struct nameANDid *);
 /////
 struct node * add_node (struct node *, struct node *);
 struct node * search_node (struct node *, char *);
 void print_node (struct node *);
 void print_tree (struct node *);

In the main i call the loadData() to import the students

  loadData(&temp);

And the loadData() is

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<20; i++){       
    fscanf(fp,"%s",&temp->last);
    fscanf(fp,"%s",&temp->yohoho.first);
    fscanf(fp,"%d",&temp->yohoho.ID);     
    top=add_node(top,temp);
    }
fclose(fp);
printf("\n\nFile loaded\n");  
}

I call the add_node() which insert a new node in my main (Surnames) tree. This alo works..

 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 {      
     //printf("Node added\n");
     return newNode;}
   }
   else {   
      if (stricmp(temp->last,top->last) < 0){
  开发者_开发百科       // printf("left\n");
        top->left=add_node(top->left,temp);}
      else if (stricmp(temp->last,top->last) == 0){
        // printf("Last names are equal\n"); 
        topname=add_node_nameANDid(topname,temp2);} //Here is one of my problems
      else {
        // printf("right\n");
        top->right=add_node(top->right,temp);}
        // printf("Node added\n");   
        return top;
       } 
      return NULL;
  }   

My problem starts with (topname=add_node_nameANDid(topname,temp2);) which is a functon like add_node() but she adds new nameANDid nodes if the students have the same Surname.. I don't know what arguments to use...I hate pointers because i'm not experienced with their use (not wet at least)... And the add_node_nameANDid() is

   struct nameANDid * add_node_nameANDid (struct nameANDid *topname, struct nameANDid *temp){
     struct nameANDid *newNode_nameANDid;  
     if (topname == NULL){    
    newNode_nameANDid=(struct nameANDid *)malloc(sizeof(struct nameANDid));
    temp->nleft=NULL;
    temp->nright=NULL;
    if (memcpy(newNode_nameANDid,temp,sizeof(struct nameANDid)) == NULL){
       printf("Node addition failed\n");
       return NULL;}
    else {      
      //printf("Node added\n");
      return newNode_nameANDid;}
    }
    else {   
        if (stricmp(temp->first,topname->first) <= 0){
           // printf("leftname\n");
           topname->nleft=add_node_nameANDid(topname->nleft,temp);}
        else {
           // printf("rightname\n");
           topname->nright=add_node_nameANDid(topname->nright,temp);}
          // printf("Node added\n");   
          return topname;
        } 
        return NULL;
     }

In add_node_nameANDid() i tried to use similar variables to be easier to understand them.. How should i use the pointers in the add_node_nameANDid() because when i copmpile it it says [Warning] passing arg 1 of `add_node_nameANDid' from incompatible pointer type in line

 topname->nleft=add_node_nameANDid(topname->nleft,temp);}(in add_node_nameANDid())

or incompatible type for argument 2 of `add_node_nameANDid'

 topname=add_node_nameANDid(topname,temp2);}

when i call the add_node_nameANDid() from add_node().

Can please someone help me with this mess?


It looks like the problem is that you have used node * for both of the structures left and right. So what is happening is that you are copying a struct nameANDid into a struct node. I suggest that in nameANDid you need nleft and nright to be pointers to struct nameANDid, not struct node.

EDIT: There are various other problems, such as I think the intention would be to look into yohoho in struct node to get the binary tree of first names. Also add_node_nameANDid is setting temp->nleft and temp->nright to null, not sure this is correct.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜