![]() |
| | #1 |
| Registered User Join Date: Apr 2002
Posts: 2
| read records fron file into a binary tree *This is a sample tree program for our final project. *We will try to modify it to meet our specs *Kirsten ************************************************** **** */ #include "defs.h" int main() { Tree t = NULL; //these are the variables being read in to the patient record(s) int i; char fname[10]; char lname[10]; int DOB; int SSN; char doc[10]; char ins[10]; char allergy[15]; char history[15]; char meds[15]; //these are the values needed for the search to work Position search_val; int key_SSN; (void) printf("Welcome to the sample tree program!!\n"); (void) printf("Please enter values for the patient records.\n"); (void) printf("Enter none when finished.\n"); for (i = 0; i < 2; i++) { (void) printf("the fname to enter is: \n"); (void) scanf("%s", &fname); //enter the values (void) printf("the lname to enter is: \n"); (void) scanf("%s", &lname); (void) printf("the DOB to enter is: \n"); (void) scanf("%d", &DOB); (void) printf("the SSN to enter is: \n"); (void) scanf("%d", &SSN); (void) printf("the doctor name to enter is: \n"); (void) scanf("%s", &doc); (void) printf("the insurance provider is: \n"); (void) scanf("%s", &ins); (void) printf("the allergy to enter is: \n"); (void) scanf("%s", &allergy); (void) printf("the patient condition to enter is: \n"); (void) scanf("%s", &history); (void) printf("the patient's current medication is: \n"); (void) scanf("%s", &meds); t = insert(t, fname, lname, DOB, SSN, doc, ins, allergy, history, meds); printf("insert successful\n"); printf("t->fname = %s\n", t->fname); } //(void) printf("Please enter the patient social security // # to search: "); //(void) scanf("%d", &key_SSN); //enter a number to be searched //search_val = search(key_SSN, t); //if (search_val == NULL) //if its not found, 0 is returned // printf("This value was not on the tree.\n"); //else //{ //if it is found, print it // printf("patient record was found.\n"); //printf("first name: %s\n", search_val->fname); //printf("last name: %s\n", search_val->lname); //printf("date of birth: %d\n", search_val->DOB); //printf("social security #: %d\n", search_val->SSN); //printf("primary physician: %s\n", search_val->doc); //printf("insurance provider: %s\n", search_val->ins); //printf("patient allergies: %s\n", search_val->allergy); //printf("patient conditions: %s\n", search_val->history_cond); //printf("patient medication(s): %s\n", search_val->meds); //} (void) printf("The tree values are: \n"); inorder(t); //traverse the tree inorder and print values printf("****"); return 0; } Functions called form main: #include "defs.h" //TreeNode* create(int value) //{ //TreeNode *tptr = malloc(sizeof(TreeNode)); //if (tptr != NULL) //create a new node and initialize //{ //its values //tptr->data = value; //tptr->left = NULL; //tptr->right = NULL; //} //return tptr; //} void destroy(TreeNode* tptr) { free (tptr); //not sure about this yet } Tree insert(Tree root, string t_fname, string t_lname, int t_DOB, int t_SSN, string t_doc, string t_ins, string t_allergy, string t_history, string t_meds) { TreeNode* temp; if (root == NULL) { temp = malloc(sizeof(TreeNode)); temp->fname = t_fname; temp->lname = t_lname; temp->DOB = t_DOB; temp->SSN = t_SSN; temp->doc = t_doc; temp->ins = t_ins; temp->allergy = t_allergy; temp->history_cond = t_history; temp->meds = t_meds; temp->left = NULL; temp->right = NULL; } else { if (t_SSN < root->SSN) { printf("insert root->left\n"); root->left = insert(root->left, t_fname, t_lname, t_DOB, t_SSN, t_doc, t_ins, t_allergy, t_history, t_meds); } else if (t_SSN > root->SSN) { printf("insert root->right\n"); root->right = insert(root->right, t_fname, t_lname, t_DOB, t_SSN, t_doc, t_ins, t_allergy, t_history, t_meds); } } return temp; } Position search(int key_SSN, Tree root) { if (root != NULL) { if (key_SSN == root->SSN) return root; else if (key_SSN < root->SSN) return search(key_SSN, root->left); else if (key_SSN > root->SSN) return search(key_SSN, root->right); } } void inorder(Tree t) { printf("@@@@@"); if (t != NULL) { inorder(t->left); printf("patient first name:%s\n", t->lname); printf("patient SSN: %d\n", t->SSN); inorder(t->right); } } definition file: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stddef.h> /*notes on definitins in the structure fname = first name lname = last name DOB = date of birth SSN = socsecnum doc = primary physician ins = insurance */ typedef char* string; typedef struct TreeNode { string fname; string lname; int DOB; int SSN; string doc; string ins; string allergy; string history_cond; string meds; struct TreeNode* left; struct TreeNode* right; }TreeNode; typedef TreeNode* Tree; typedef TreeNode* Position; //function prototypes //TreeNode* create(int); void destroy(TreeNode*); Tree insert(TreeNode*, char[], char[], int, int, char[], char[], char[], char[], char[]); Position search(int, TreeNode*); void inorder(Tree); |
| Kirsten is offline | |
| | #2 |
| Code Goddess Join Date: Sep 2001
Posts: 9,664
| >I am running out of time because my project is due in a week!! Hahaha! I wish I could have a week to write a simple binary tree. ![]() >I keep on getting a "segmentation fault - core dumped" You're accessing a pointer that points to memory you don't own, a good idea would be stepping through your code and looking at the values. It's easy to see what goes wrong that way. You don't really need to cast printf and scanf as void, especially with so many calls. It just makes things harder for most people to read. And you really should avoid scanf when reading string data anyway. ![]() I'll be of more help when you use either the code or php tags to make your code at least a little bit readable. -Prelude
__________________ My best code is written with the delete key. |
| Prelude is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Can we have vector of vector? | ketu1 | C++ Programming | 24 | 01-03-2008 05:02 AM |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |
| Read in binary file ( pointers ) | Giant | C Programming | 41 | 06-23-2005 04:54 AM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C Programming | 3 | 03-04-2005 02:46 PM |
| binary tree node structure | Kirsten | C Programming | 2 | 04-26-2002 08:02 PM |