| | |
Binary search tree help needed
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2007
Posts: 20
Reputation:
Solved Threads: 0
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!
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.
•
•
•
•
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?
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: May 2007
Posts: 20
Reputation:
Solved Threads: 0
c Syntax (Toggle Plain Text)
#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...
c Syntax (Toggle Plain Text)
#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++
Last edited by Lioshenka; Jul 29th, 2009 at 6:26 pm.
![]() |
Similar Threads
- Binary Search Tree within in BT ? (C++)
- Reading a file into a binary search tree (C++)
- Binary Search Tree (C++)
- searching and inserting node in a binary search tree (C)
- recursive findaverage function for Binary search tree (C)
- Binary search tree removal (C++)
- Insertion in a binary search tree (C++)
Other Threads in the C Forum
- Previous Thread: Forum courtesy: Solved Threads
- Next Thread: char pointer to char
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






