Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Inserting Items

The procedure for inserting an item into a scatter table using open addressing is actually quite simple--find an unoccupied array location and then put the item in that location. To find an unoccupied array element, the array is probed according to a probing sequence. In this case, the probing sequence is linear probing. Program gif defines the routines needed to insert an item into the scatter table.

   program13984
Program: OpenScatterTable Class C, FindUnoccupied, and Insert Member Function Definitions

The function C defines the probing sequence. As it turns out, the implementation required for a linear probing sequence is trivial. The function C is the identity function.

The purpose of the private member function FindUnoccupied is to locate an unoccupied array position. The FindUnoccupied routine probes the array according the probing sequence determined by the C function. At most n+1 probes are made, where tex2html_wrap_inline61308 is the number of items in the scatter table. When using linear probing it is always possible to find an unoccupied cell in this many probes as long as the table is not full. Notice also that we do not search for an empty cell. Instead, the search terminates when a cell is found, the state of which is not occupied, i.e., empty or deleted. The reason for this subtlety has to do with the way items may be removed from the table. The FindUnoccupied routine returns a value between 0 and M-1, where tex2html_wrap_inline63192 is the length of the scatter table, if an unoccupied location is found. Otherwise, it returns M to indicate that an unoccupied cell was not found.

The Insert routine takes a reference to an Object and puts that object into the scatter table. It does so by calling FindUnoccupied to determine the location of an unoccupied entry in which to put the object. The state of the unoccupied entry is set to occupied and a pointer to the object is saved in the entry.

The running time of the Insert routine is determined by that of FindUnoccupied. The worst case running time of FindUnoccupied is O(n), where n is the number of items in the scatter table. Therefore, the running time of Insert is tex2html_wrap_inline62846.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.