Member Avatar for Lioshenka

Hello all,

in time of need I always come here to cry for help)

Anyways. What I need to do is to store data about people in a binary tree. They need to come from a file, but for now I can hardcode them in.

To put it simple, I will have 10 people (probably defined as structures with name, surname, age, phone number variables) where surname acts as a key.

I am totally new to binary trees - does it mean I will need to have a tree of structures? I have some sample code, but it only works for numbers, how can I modify it so it will take structures?

How do I go about it? General tips and even code would be great!

Also, I found some sample code and it has weird "->" symbols - what do they mean in my context?

Basically, I need a good push.

Thanks!

Hello all,

in time of need I always come here to cry for help)

Anyways. What I need to do is to store data about people in a binary tree. They need to come from a file, but for now I can hardcode them in.

Well, I suppose the first thing you could code is the input function and get that out of the way...

To put it simple, I will have 10 people (probably defined as structures with name, surname, age, phone number variables) where surname acts as a key.

I am totally new to binary trees - does it mean I will need to have a tree of structures? I have some sample code, but it only works for numbers, how can I modify it so it will take structures?

Yes. How to modify your code? Replace the integer data field with your structure data. The integer information is already defined in a structure, is it not? Just replace it with all the fields you need to keep track of.

How do I go about it? General tips and even code would be great!

Use your editor... Since you posted nothing, I can only guess -- or use my psychic powers. Which would you like me to do? :icon_wink:

Also, I found some sample code and it has weird "->" symbols - what do they mean in my context?

A structure element is referenced using a dot, as in person.age , get the age field from the person structure. But if the structure is a pointer, you use ->. In this case if person is your structure, and p_person points to the person structure, get the age with p_person->age

commented: Thanks +3
Member Avatar for Lioshenka
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    
    typedef struct{
            char dummy[256];
            char surname[225];
            char name[256];
            int ID;
            char house[256];
            char street[256];
            char town[256];
            char county[256];
            char area[256];
            char country[256];
            char postcode[256];
            int phone;
            char email[256];
            char boattype[256];
            char boatname[256];
            }user;

FILE *file;
int i=0;
char number_of_people[256];
int nop;
char yetdummy[256]; 
char yetdummy2[256]; 


 file = fopen("members.data","r");
 if(file==NULL) {
    printf("Error: can't open file.\n");
    return 1;
  }
  else {
    printf("File opened successfully.\n");
         }
 fscanf(file,"%s \n", number_of_people);
 //printf("%s \n", number_of_people);

 nop = atoi(number_of_people);
 //printf("%d\n", nop);
 user usera[nop]; 
  for (i=0; i<nop; i++){
      fgets(usera[i].dummy, 256, file);        
      fgets(usera[i].surname, 256, file);  
      fgets(usera[i].name, 256, file);  
      fgets(yetdummy, 256, file);  
      usera[i].ID = atoi(yetdummy);      
      fgets(usera[i].house, 256, file);  
      fgets(usera[i].street, 256, file);  
      fgets(usera[i].town, 256, file); 
      fgets(usera[i].county, 256, file);  
      fgets(usera[i].area, 256, file);  
      fgets(usera[i].country, 256, file);  
      fgets(usera[i].postcode, 256, file);  
      fgets(yetdummy2, 256, file); 
      usera[i].phone = atoi(yetdummy2);
        
      fgets(usera[i].email, 256, file);  
      fgets(usera[i].boattype, 256, file);  
      fgets(usera[i].boatname, 256, file);        
     
     printf("Name is %s\n", usera[i].name);      
      printf("ID is %d\n", usera[i].ID);    
 }
 
  fclose(file);

}

This is my code so far. I got it to read members from file and it works fine and reads them all in an array of structures. You can probably see how stupid I am in programming if you look at my code)))

What can I do next? This is the sample code I was talking about earlier, I think it is very similar to what I am looking for, but I can not see how the student data is fed into the program or how the key variable is defined...

#include<stdlib.h>
#include<stdio.h>

typedef struct student_data STUDENT_DATA;

struct student_data {
 int student_ID;
 int student_grade;
 STUDENT_DATA *left, *right;
};

STUDENT_DATA *new_student, *cur_student;

if ((new_student = malloc(sizeof(STUDENT_DATA))) == NULL) { abort(); }

new_student->student_ID = newID;
new_student->student_size = newsize;
new_student->left = NULL;
new_student->right = NULL;

if (!students) { students = new_student; return; }

cur_student = students;
while (cur_student) { 

if (newID == cur_student->student_ID) { abort(); }

if (newID < cur_student->student_ID) {
 if (cur_student->left == NULL) {
 cur_student->left = newstudent;
 return 1;
 }
 cur_student = cur_student->left;
 
 } else {
 if (cur_student->right == NULL) {
 cur_student->right = newstudent;
 return 1;
 }
 cur_student = cur_student->right;
 }
}

Thanks a lot!

Oh, and by the way I use Dev-C++

Member Avatar for Lioshenka

Anyone, please?? How do I use binary tree with the structures?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.