Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Binary Search Tree in C

Options
  • 09-04-2003 7:33pm
    #1
    Registered Users Posts: 6,315 ✭✭✭


    I have a project to do in C and im looking for a few pointers.

    I have to add the several of the follwing to a binary search tree and manipulate it.

    struct student_details
    {
    int number;
    char first_name[20];
    char last_name[20];
    char address[50];
    char course[10];
    int DOB[3];
    };


    Can anyone give any advice or where would i find a tutorial on binary search trees?

    We have to use inorder tranversal to pritn the students.

    I'm not asking anyone to do this project for me.


Comments

  • Registered Users Posts: 935 ✭✭✭Mixie


    Try this.


  • Closed Accounts Posts: 3,322 ✭✭✭Repli


    Mixie that explanation is done in C++

    We have richard lawlor for algorithms.. his website has a pretty good description of creating/adding to/removing from a binary tree:

    http://www.comp.dit.ie/rlawlor/Prob_Solv/Heap%20Data%20Structure.pdf


  • Registered Users Posts: 935 ✭✭✭Mixie


    Originally posted by Repli
    Can anyone give any advice or where would i find a tutorial on binary search trees?
    ...
    I'm not asking anyone to do this project for me
    Originally posted by Repli
    Mixie that explanation is done in C++

    Sorry Repli - I had intended it to be more of a reference/help site than a solution :)


  • Registered Users Posts: 6,315 ✭✭✭ballooba


    Thanks for the above lads. I now understand properly what a binary tree is at least. I'm having trouble with the below nnow though, any pointers? Its only saving the last student put into the BST.

    // this saves to a file that has already been created
    void save (NODEPTR root)
    {
    int count;// this is used for error checking
    if (root != NULL)
    {
    save(root->left);

    if ((fp == (fopen("file.dat", "r+b"))) == NULL)
    {
    puts("There was an error in opening the output file");
    fflush (stdin);
    scanf ("%c", &pause);
    menu();
    }

    count = fwrite(root, sizeof(int),SIZE, fp);
    if (count != SIZE)
    {
    puts("There was an error in writing to the file");
    fflush (stdin);
    scanf ("%c", &pause);
    menu();
    }
    fclose(fp);
    save(root->right);

    }

    }


  • Registered Users Posts: 1,931 ✭✭✭Zab


    Try using a+b instead of r+b.... or just ab.

    However, there is no need to open the file each time. You should just pass the file pointer into the save() method. Also, you shouldn't call menu() when there is an error. You should return an error condition which can be checked. You will have to check it within the save() method so that you know not to continue. If you do just call menu() then it will try to continue the save when the menu method exits.

    Zab.


  • Advertisement
  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    its only saving the last one because you are not chosing to open with append.
    it is overwriting the file every time it opens it.
    only open the file once


Advertisement