hi to all user i am new in file handling , i know file handling but not much , my instructor taught me that use binary search tree in a file to search key value ,key value(like any thing such that record can be easily make out and comparision can be done ) there is a file which contains RRN (relative record no) and key and two more RRN . one RRN's key value is greatter than the given key value and another one's key value is less than the given key value .How to implement this methods .

If you have a key pair value, then surely the best possible structure to use would be a hash table over a binary tree? Give some sample output of the file, please.

If the file is already sorted, then just read it into memory and use bsearch() to find the item you need. You will need to write the compare() function that you pass to bsearch().

If the file is not sorted, then read it into memory and use qsort() to sort it, and bsearch() as above to find records within it. You should be able to use the same compare() function for both.

These are C language functions. If you need C++ specific stuff, then just read the data into an appropriate structure and add it to std::map or std::multimap template instances using the key value as the map key.

Finally, if your professor wants you to deal directly with the file data and not read it into memory first, then you need to answer the questions: is the file already sorted, and are the records and fields of them of fixed length? How you deal with the file depends upon the answers to those questions.

This article has been dead for over six months. Start a new discussion instead.