Binary search tree help needed

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2007
Posts: 20
Reputation: Lioshenka is an unknown quantity at this point 
Solved Threads: 0
Lioshenka Lioshenka is offline Offline
Newbie Poster

Binary search tree help needed

 
0
  #1
Jul 25th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Binary search tree help needed

 
1
  #2
Jul 26th, 2009
Originally Posted by Lioshenka View 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.
Well, I suppose the first thing you could code is the input function and get that out of the way...

Originally Posted by Lioshenka View Post
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.

Originally Posted by Lioshenka View Post
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?

Originally Posted by Lioshenka View Post
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
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 20
Reputation: Lioshenka is an unknown quantity at this point 
Solved Threads: 0
Lioshenka Lioshenka is offline Offline
Newbie Poster

Re: Binary search tree help needed

 
0
  #3
Jul 29th, 2009
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 20
Reputation: Lioshenka is an unknown quantity at this point 
Solved Threads: 0
Lioshenka Lioshenka is offline Offline
Newbie Poster

Re: Binary search tree help needed

 
0
  #4
Aug 2nd, 2009
Anyone, please?? How do I use binary tree with the structures?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC