Thread: implementing a student database using a binary tree

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    1

    Post implementing a student database using a binary tree

    Hey guys,

    I'm quite new to C programming and I'm having trouble implementing a binary tree. The program is to ask the user to enter "1" as an input, and when 1 is entered a student (enrolled in no units and displayed in ascending numerical order of ID number) is added. After the student is added into the binary tree the current height of the tree is given.

    I understand simple binary tree's, however in this case having two structs really confuses me. One struct contains the relevant student information but the other structs data_item is a pointer to the student struct? The below code are the two structs used in the .h header file as well as the insert definition.

    Code:
    typedef struct student *student_ptr;struct student
    {
    int ID;
    char *name;
    int n_units;
    char *units[1000];
    };
    
    
    typedef struct node *node_ptr;
    struct node
    {
    student_ptr data_item;
    node_ptr left;
    node_ptr right;
    };
    
    node_ptr insert(int n, node_ptr tree);
    The rest of the code below is contained in the .c file. I'm trying to write the insert function, however as the struct node's data_item is a pointer to the struct student I get confused. I understand I need to change to 'int n' parameter as well?

    Code:
    node_ptr insert(int n, node_ptr tree)
    {
        if(!tree)
        {
            tree = (node_ptr) malloc(sizeof(struct node));
            tree->data_item = n;
            tree->left = tree->right = NULL;
        }
        else if(n < tree->data_item)
        {
            tree->left = insert(n, tree->left);
        }
        else if(n > tree->data_item)
        {
            tree->right = insert(n, tree->right);
        }
    
    
        return tree;
    }
    If someone could point me in the right direction that would be greatly appreciated!

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So in two places, change "int" for "student_ptr" and see what happens.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems implementing a binary tree
    By noops in forum C Programming
    Replies: 3
    Last Post: 07-22-2008, 02:39 AM
  2. Replies: 2
    Last Post: 08-01-2007, 01:08 PM
  3. A Student's Mark Database
    By playstationman1 in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2006, 01:26 PM
  4. Implementing a binary search
    By smitsky in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2004, 01:16 PM
  5. display tree data in binary tree format
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2003, 12:47 AM

Tags for this Thread