0
votes

I'm writing a program to form hash tables of different sizes with linear probing. In my ADT for the linear probing I have a function def insert ( ) which is called from my main script from the following function:

def insertHashTable(  loadNum, hashTable  ):
    i = 0
    while i < ((loadNum*size)-1):
        hashTable.insert(data[i],data[i])
        i = i + 1
    return hashTable 

The error itself is from this function that is called from insertHashTable( )

def insert( self, key, value ):
    ( found, slot ) = self._findSlot( key ) #ERROR HERE
    if not found :
        self._table[slot] = _MapEntry( key, value ) #custom datatype
        self._count += 1
    return not found

I get the nonetype error on the second line of this code. Finally, _findSlot( ) is the following:

def _findSlot( self, key ):        
    startLoc = self._hash1( key )

    self.slotsAccessed += 1
    if self._table.__getitem__( startLoc ) == None:
        return (False, startLoc)

    else:
        c = 0
        while (c+startLoc) < (self._size -1):
            if self._table[startLoc+c] == None:
                return (False, startLoc+c) 
            elif self._table.__getitem__( startLoc ).key == key:
                return (True, startLoc+c) 
            c = c + 1
            self.slotsAccessed += c

I'm not sure why there would be such an error in the insertHashTable( ) function, seeing as there is no iteration happening with the key.

I do however know my hashtable has 'None' in every slot at initialization of the table, maybe there's some issue with that?

1
(c+startLoc) < (self._size -1) might be False in first iteration and you do not have a return case for it. I suppose you didn't consider that case.FatmaT

1 Answers

3
votes

Your _findSlot(self, key) function can complete without a return statement. (Specifically, this will happen if your while condition becomes false and the loop finishes.) In that case, the function will return None. If you try and assign (found, slot) the value None, you get the error Nonetype is not iterable.

You probably need to figure out what your function is supposed to return in the case where it is currently returning None.