Thread: binary tree node structure

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    Angry binary tree node structure

    This is a program that is supposed to create a binary search tree containing patient records. I can get the tree to work correctly using the social security number as the key field, however the other fields in the stricture do not seem to be linked with the key field. I there a limit on the number of data fields that can be contained in a tree node? how can i fix this problem? project is due in 5 days, please help. I have posted my code below, along with some output:

    ****this is the main program*******
    #include "defs.h"

    int main()
    {
    Tree root = NULL;
    TreeNode* temp;
    //these are the variables being read in to the patient record(s)
    int i;
    unsigned long SSN;
    char fname[8];
    char lname[10];
    int DOB;
    char doc[6];
    char ins[9];
    char allergy[15];
    char med_history[15];
    char dates[9];

    //these are the values needed for the search to work
    Position search_val;
    unsigned long key_SSN;

    (void) printf("Welcome to the sample tree program!!\n");

    (void) printf("Please enter values for the patient records.\n");

    for (i = 0; i < 3; i++)
    {
    temp = malloc(sizeof(TreeNode));

    (void) printf("the SSN number to enter is: \n");
    (void) scanf("%lu", &SSN);
    temp->SSN = SSN;

    (void) printf("the first name is: \n");
    (void) scanf("%s", &fname);
    temp->fname = fname;

    (void) printf("the last name is: \n");
    (void) scanf("%s", &lname);
    temp->lname = lname;

    (void) printf("the DOB is: \n");
    (void) scanf("%d", &DOB);
    temp->DOB = DOB;

    (void) printf("the primary physician is: \n");
    (void) scanf("%s", &doc);
    temp->doc = doc;

    (void) printf("the insurance provider is: \n");
    (void) scanf("%s", &ins);
    temp->ins = ins;

    (void) printf("the allergies are: \n");
    (void) scanf("%s", &allergy);

    (void) printf("the med history is: \n");
    (void) scanf("%s", &med_history);
    temp->med_history = med_history;

    (void) printf("the med dates are: \n");
    (void) scanf("%s", &dates);
    temp->dates = dates;

    temp->left = NULL;
    temp->right = NULL;

    root = insert(root, temp);
    printf("insert successful\n");
    printf("root->SSN = %lu\n", root->SSN);
    printf("root->name = %s\n", root->fname);
    printf("root->lname = %s\n", root->lname);
    }

    (void) printf("Please enter the patient social security
    # to search: ");
    (void) scanf("%lu", &key_SSN); //enter a number to be searched

    search_val = search(key_SSN, root);
    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("social security num: %lu\n", search_val->SSN);
    printf("patient name is: %s\n", search_val->fname);
    }

    (void) printf("The tree values are: \n");
    inorder(root); //traverse the tree inorder and print values
    printf("****\n");
    return 0;
    }
    ************************************************

    ******these are the functions called from main***********
    #include "defs.h"

    void destroy(TreeNode* root)
    {
    free (root); //not sure about this yet
    }
    ************************************************

    TreeNode* insert(TreeNode* root, TreeNode* temp)
    {
    if (root == NULL)
    {
    root = temp;
    }
    else
    {
    if (temp->SSN < root->SSN)
    root->left = insert(root->left, temp);

    else if (temp->SSN > root->SSN)
    root->right = insert(root->right, temp);
    }

    return root;
    }
    ************************************************

    Position search(unsigned long 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));

    else
    return NULL;
    }
    }
    **************************************************

    void inorder(Tree root)
    {
    if (root != NULL)
    {
    inorder(root->left);
    printf("patient SSN: %lu\n", root->SSN);
    printf("patient lname is: %s\n", root->lname);
    printf("\n");
    inorder(root->right);
    }

    }
    **************************************************

    *************this is the 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
    {
    unsigned long SSN;
    string fname;
    string lname;
    int DOB;
    string doc;
    string ins;
    string allergy;
    string med_history;
    string dates;
    struct TreeNode* left;
    struct TreeNode* right;
    }TreeNode;

    typedef TreeNode* Tree;
    typedef TreeNode* Position;

    //function prototypes
    void destroy(TreeNode*);
    Tree insert(TreeNode*, TreeNode*);
    Position search(unsigned long, Tree);
    void inorder(Tree);

    **************************************************
    *********this is the output from running the program*********
    Welcome to the sample tree program!!

    Please enter values for the patient records.

    the SSN number to enter is:

    111111111

    the first name is:

    kird stem n

    the last name is:

    smith

    the DOB is:

    083079

    the primary physician is:

    none

    the insurance provider is:

    none

    the allergies are:

    none

    the med history is:

    none

    the med dates are:

    none

    insert successful

    root->SSN = 111111111

    root->name = kirsten

    root->lname = smith

    the SSN number to enter is:

    123456789

    the first name is:

    lisa

    the last name is:

    jones

    the DOB is:

    050480

    the primary physician is:

    Fox

    the insurance provider is:

    CDPHP

    the allergies are:

    none

    the med history is:

    paxil

    the med dates are:

    09/99-present

    insert successful

    root->SSN = 111111111

    root->name = lisa

    root->lname = jones

    the SSN number to enter is:

    222222222

    the first name is:

    larry

    the last name is:

    hobbs

    the DOB is:

    123178

    the primary physician is:

    Saha

    the insurance provider is:

    MVP

    the allergies are:

    beesting

    the med history is:

    zoloft

    the med dates are:

    08/89-09/98

    insert successful

    root->SSN = 111111111

    root->name = larry

    root->lname = hobbs

    Please enter the patient social security

    # to search: 123456789

    patient record was found.

    social security num: 123456789

    patient name is: larry

    The tree values are:

    patient SSN: 111111111

    patient lname is: hobbs



    patient SSN: 123456789

    patient lname is: hobbs



    patient SSN: 222222222

    patient lname is: hobbs
    ************************************************** *

    sorry about so many file!! running this on a UNIX machine with the
    cc compiler. Maybe this will make it a little clearer.
    Last edited by Kirsten; 04-26-2002 at 07:30 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    PLEASE use code tags. That is a nightmare to read. What is that anyway, like four different files? (RE: #include all over the place)

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    useless comment:
    OW my eyes, they hurt
    like looking over my 52 page report on achondroplasia all over again.

    I am going to code it right now:
    Code:
    #include "defs.h" 
    
    int main() 
    { 
    	Tree root = NULL; 
    	TreeNode* temp; 
    //these are the variables being read in to the patient record(s) 
    	int i; 
    	unsigned long SSN; 
    	char fname[8]; 
    	char lname[10]; 
    	int DOB; 
    	char doc[6]; 
    	char ins[9]; 
    	char allergy[15]; 
    	char med_history[15]; 
    	char dates[9]; 
    	
    	//these are the values needed for the search to work 
    	Position search_val; 
    	unsigned long key_SSN; 
    	
    	(void) printf("Welcome to the sample tree program!!\n"); 
    	
    	(void) printf("Please enter values for the patient records.\n"); 
    	
    	for (i = 0; i < 3; i++) 
    	{ 
    		temp = malloc(sizeof(TreeNode)); 
    
    		(void) printf("the SSN number to enter is: \n"); 
    		(void) scanf("%lu", &SSN); 
    		temp->SSN = SSN; 
    		
    		(void) printf("the first name is: \n"); 
    		(void) scanf("%s", &fname); 
    		temp->fname = fname; 
    	
    		(void) printf("the last name is: \n"); 
    		(void) scanf("%s", &lname); 
    		temp->lname = lname; 
    	
    		(void) printf("the DOB is: \n"); 
    		(void) scanf("%d", &DOB); 
    		temp->DOB = DOB; 
    		
    		(void) printf("the primary physician is: \n"); 
    		(void) scanf("%s", &doc); 
    		temp->doc = doc; 
    		
    		(void) printf("the insurance provider is: \n"); 
    		(void) scanf("%s", &ins); 
    		temp->ins = ins; 
    		
    		(void) printf("the allergies are: \n"); 
    		(void) scanf("%s", &allergy); 
    		
    		(void) printf("the med history is: \n"); 
    		(void) scanf("%s", &med_history); 
    		temp->med_history = med_history; 
    	
    		(void) printf("the med dates are: \n"); 
    		(void) scanf("%s", &dates); 
    		temp->dates = dates; 
    		
    		temp->left = NULL; 
    		temp->right = NULL; 
    		
    		root = insert(root, temp); 
    		printf("insert successful\n"); 
    		printf("root->SSN = %lu\n", root->SSN); 
    		printf("root->name = %s\n", root->fname); 
    		printf("root->lname = %s\n", root->lname); 
    	} 
    	
    	(void) printf("Please enter the patient social security 
    	# to search: "); 
    	(void) scanf("%lu", &key_SSN); //enter a number to be searched 
    	
    	search_val = search(key_SSN, root); 
    	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("social security num: %lu\n", search_val->SSN); 
    		printf("patient name is: %s\n", search_val->fname); 
    	} 
    
    	(void) printf("The tree values are: \n"); 
    	inorder(root); //traverse the tree inorder and print values 
    	printf("****\n"); 
    	return 0; 
    } 
    /************************************************/
    
    /******these are the functions called from main***********/ 
    #include "defs.h" 
    
    void destroy(TreeNode* root) 
    { 
    	free (root); //not sure about this yet 
    } 
    /************************************************/ 
    
    TreeNode* insert(TreeNode* root, TreeNode* temp) 
    { 
    	if (root == NULL) 
    	{ 
    		root = temp; 
    	} 
    	else 
    	{ 
    		if (temp->SSN < root->SSN) 
    		root->left = insert(root->left, temp); 
    	
    		else if (temp->SSN > root->SSN) 
    			root->right = insert(root->right, temp); 
    	} 
    
    	return root; 
    } 
    /************************************************/ 
    
    Position search(unsigned long 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)); 
    	
    		else 
    		return NULL; 
    	} 
    } 
    /**************************************************/
    
    
    void inorder(Tree root) 
    	{ 
    	if (root != NULL) 
    	{ 
    		inorder(root->left); 
    		printf("patient SSN: %lu\n", root->SSN); 
    		printf("patient lname is: %s\n", root->lname); 
    		printf("\n"); 
    		inorder(root->right); 
    	} 
    
    } 
    /**************************************************/
    /*************this is the 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 
    { 
    	unsigned long SSN; 
    	string fname; 
    	string lname; 
    	int DOB; 
    	string doc; 
    	string ins; 
    	string allergy; 
    	string med_history; 
    	string dates; 
    	struct TreeNode* left; 
    	struct TreeNode* right; 
    }TreeNode; 
    
    typedef TreeNode* Tree; 
    typedef TreeNode* Position; 
    
    //function prototypes 
    void destroy(TreeNode*); 
    Tree insert(TreeNode*, TreeNode*); 
    Position search(unsigned long, Tree); 
    void inorder(Tree);
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM
  3. delete node for a binary tree
    By AmazingRando in forum C Programming
    Replies: 4
    Last Post: 10-27-2003, 10:45 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM