943,822 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1010
  • C RSS
Jul 25th, 2009
0

Binary search tree help needed

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Lioshenka is offline Offline
31 posts
since May 2007
Jul 26th, 2009
1

Re: Binary search tree help needed

Click to Expand / Collapse  Quote originally posted by 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.
Well, I suppose the first thing you could code is the input function and get that out of the way...

Click to Expand / Collapse  Quote originally posted by Lioshenka ...
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.

Click to Expand / Collapse  Quote originally posted by Lioshenka ...
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?

Click to Expand / Collapse  Quote originally posted by Lioshenka ...
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
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Jul 29th, 2009
0

Re: Binary search tree help needed

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int main()
  6. {
  7.  
  8. typedef struct{
  9. char dummy[256];
  10. char surname[225];
  11. char name[256];
  12. int ID;
  13. char house[256];
  14. char street[256];
  15. char town[256];
  16. char county[256];
  17. char area[256];
  18. char country[256];
  19. char postcode[256];
  20. int phone;
  21. char email[256];
  22. char boattype[256];
  23. char boatname[256];
  24. }user;
  25.  
  26. FILE *file;
  27. int i=0;
  28. char number_of_people[256];
  29. int nop;
  30. char yetdummy[256];
  31. char yetdummy2[256];
  32.  
  33.  
  34. file = fopen("members.data","r");
  35. if(file==NULL) {
  36. printf("Error: can't open file.\n");
  37. return 1;
  38. }
  39. else {
  40. printf("File opened successfully.\n");
  41. }
  42. fscanf(file,"%s \n", number_of_people);
  43. //printf("%s \n", number_of_people);
  44.  
  45. nop = atoi(number_of_people);
  46. //printf("%d\n", nop);
  47. user usera[nop];
  48. for (i=0; i<nop; i++){
  49. fgets(usera[i].dummy, 256, file);
  50. fgets(usera[i].surname, 256, file);
  51. fgets(usera[i].name, 256, file);
  52. fgets(yetdummy, 256, file);
  53. usera[i].ID = atoi(yetdummy);
  54. fgets(usera[i].house, 256, file);
  55. fgets(usera[i].street, 256, file);
  56. fgets(usera[i].town, 256, file);
  57. fgets(usera[i].county, 256, file);
  58. fgets(usera[i].area, 256, file);
  59. fgets(usera[i].country, 256, file);
  60. fgets(usera[i].postcode, 256, file);
  61. fgets(yetdummy2, 256, file);
  62. usera[i].phone = atoi(yetdummy2);
  63.  
  64. fgets(usera[i].email, 256, file);
  65. fgets(usera[i].boattype, 256, file);
  66. fgets(usera[i].boatname, 256, file);
  67.  
  68. printf("Name is %s\n", usera[i].name);
  69. printf("ID is %d\n", usera[i].ID);
  70. }
  71.  
  72. fclose(file);
  73.  
  74. }

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...
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3.  
  4. typedef struct student_data STUDENT_DATA;
  5.  
  6. struct student_data {
  7. int student_ID;
  8. int student_grade;
  9. STUDENT_DATA *left, *right;
  10. };
  11.  
  12. STUDENT_DATA *new_student, *cur_student;
  13.  
  14. if ((new_student = malloc(sizeof(STUDENT_DATA))) == NULL) { abort(); }
  15.  
  16. new_student->student_ID = newID;
  17. new_student->student_size = newsize;
  18. new_student->left = NULL;
  19. new_student->right = NULL;
  20.  
  21. if (!students) { students = new_student; return; }
  22.  
  23. cur_student = students;
  24. while (cur_student) {
  25.  
  26. if (newID == cur_student->student_ID) { abort(); }
  27.  
  28. if (newID < cur_student->student_ID) {
  29. if (cur_student->left == NULL) {
  30. cur_student->left = newstudent;
  31. return 1;
  32. }
  33. cur_student = cur_student->left;
  34.  
  35. } else {
  36. if (cur_student->right == NULL) {
  37. cur_student->right = newstudent;
  38. return 1;
  39. }
  40. cur_student = cur_student->right;
  41. }
  42. }

Thanks a lot!

Oh, and by the way I use Dev-C++
Last edited by Lioshenka; Jul 29th, 2009 at 6:26 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
Lioshenka is offline Offline
31 posts
since May 2007
Aug 2nd, 2009
0

Re: Binary search tree help needed

Anyone, please?? How do I use binary tree with the structures?
Reputation Points: 10
Solved Threads: 0
Light Poster
Lioshenka is offline Offline
31 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Forum courtesy: Solved Threads
Next Thread in C Forum Timeline: char pointer to char





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC