Chapter15 - Chapter 15 Hashing Imagine that a professor...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 15 – Hashing Imagine that a professor needs to be able to quickly retrieve information upon students in his class (by student number). He wants to be able to enter a student number and get a near-instantaneous display of the student’s vital data (number of classes attended, number of times student has fallen asleep in class, etc.). The professor could organize his database using one of the techniques we’ve considered thus far (array, linked list, binary tree). A binary tree would allow data to be retrieved reasonably quickly (presuming it was balanced), as would a sorted array. As a professor’s time is very valuable, however, he wants something even better. This is a fast access problem - he wants the ultimate in retrieval performance, and needs to use “hashing”. As cost is no object, the professor decides to organize his data base as an array of 1,000,000 pointers (one for each possible student number, assuming 6-digit student numbers). Each array element is either NULL (meaning “no such student”) or points to a “studentData” object. class StudentData { public: int number; string name; int classes_attended; // … etc. }; class DataBase { private: StudentData* array [1000000]; // array elements are of type studentData* // (i.e. the array is made up on pointers) public: DataBase (); // constructor ~DataBase (); // destructor // returns false if the student is not in the data base // otherwise returns true and "returns" the data for the student bool retrieve (int studentNumber, StudentData &data); // the student number is part of the student data void insert (StudentData &data); };
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The constructor must initialize the array by filling it with NULL's. DataBase::DataBase () { int i; for (i = 0; i < 1000000; i++) { array[i] = NULL; } } Retrieval is straightforward. The student's number is used as an index into the array. If the array element is NULL, the student is not part of the data base. Otherwise the student's data is obtained by dereferencing the pointer. bool DataBase::retrieve (int studentNumber, StudentData &data) { if (array[studentNumber] == NULL) return false; // no such student data = *(array[studentNumber]); // copy data for student return true; } Unless the student is already in the database, insertion involves creating a new StudentData object. When a student is already in the data base, the insert function (below) simply copies the new student data supplied into the pre-existing object. void DataBase::insert (StudentData &data) { if (array[data.number] == NULL) { // student not already in data base array[data.number] = new StudentData; // create object } *(array[data.number]) = data; // fill in or update object } In this simple case, it is even possible to easily output all of the information stored in the database in order (e.g. starting with the lowest student number). This is not normally the case when hashing is used. The scheme works well, but, one day, the inevitable happens.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

Chapter15 - Chapter 15 Hashing Imagine that a professor...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online