我有一个使用线性探测的哈希表。我被赋予了erase(int key)
使用以下准则编写函数的任务。
void erase(int key); Preconditions: key >= 0 Postconditions: If a record with the specified key exists in the table, then that record has been removed; otherwise the table is unchanged.
我也得到了一些完成任务的提示
重要的是要意识到插入函数将允许您向表中添加新条目,或更新表中的现有条目。
对于线性探测版本,请注意插入项目的代码有两个搜索。insert() 函数调用函数 findIndex() 来搜索表以查看该项目是否已经在表中。如果项目不在表中,则进行第二次搜索以找到表中的位置以插入项目。添加删除条目的功能将需要修改插入过程。搜索现有项目时,请确保搜索不会停止,因为该位置已被占用但由于该项目已被删除而现在为空。在搜索插入新项目的位置时,请使用第一个空位置 - 该位置是否曾被占用并不重要。
所以我开始写erase(key),我似乎遇到了提示所指的问题,但我不确定这意味着什么。我将在一秒钟内提供代码,但是我为测试我的代码所做的是设置哈希表,以便它会发生冲突,然后我删除该键并重新哈希表,但它不会进入正确的位置。
例如,我在哈希表中添加了一些元素:
The hash table is:
Index Key Data
0 31 3100
1 1 100
2 2 200
3 -1
4 -1
5 -1
6 -1
7 -1
8 -1
9 -1
10 -1
11 -1
12 -1
13 -1
14 -1
15 -1
16 -1
17 -1
18 -1
19 -1
20 -1
21 -1
22 -1
23 -1
24 -1
25 -1
26 -1
27 -1
28 -1
29 -1
30 -1
所以我所有的值都是空的,除了前 3 个索引。显然键 31 应该进入索引 1。但是由于键 1 已经存在,它会发生冲突并解决索引 0。然后我删除键 1 并重新哈希表,但键 31 保持在索引 0。
以下是可能值得一看的功能:
void Table::insert( const RecordType& entry )
{
bool alreadyThere;
int index;
assert( entry.key >= 0 );
findIndex( entry.key, alreadyThere, index );
if( alreadyThere )
table[index] = entry;
else
{
assert( size( ) < CAPACITY );
index = hash( entry.key );
while ( table[index].key != -1 )
index = ( index + 1 ) % CAPACITY;
table[index] = entry;
used++;
}
}
由于 insert 使用 findIndex,我也将其包括在内
void Table::findIndex( int key, bool& found, int& i ) const
{
int count = 0;
assert( key >=0 );
i = hash( key );
while ( count < CAPACITY && table[i].key != -1 && table[i].key != key )
{
count++;
i = (i + 1) % CAPACITY;
}
found = table[i].key == key;
}
这是我目前的擦除开始
void Table::erase(int key)
{
assert(key >= 0);
bool found, rehashFound;
int index, rehashIndex;
//check if key is in table
findIndex(key, found, index);
//if key is found, remove it
if(found)
{
//remove key at position
table[index].key = -1;
table[index].data = NULL;
cout << "Found key and removed it" << endl;
//reduce the number of used keys
used--;
//rehash the table
for(int i = 0; i < CAPACITY; i++)
{
if(table[i].key != -1)
{
cout << "Rehashing key : " << table[i].key << endl;
findIndex(table[i].key, rehashFound, rehashIndex);
cout << "Rehashed to index : " << rehashIndex << endl;
table[rehashIndex].key = table[i].key;
table[rehashIndex].data = table[i].data;
}
}
}
}
有人可以解释我需要做什么才能使其正确重新散列吗?我理解哈希表的概念,但我似乎在这里做错了什么。
编辑
根据用户的建议:
void Table::erase(int key)
{
assert(key >= 0);
bool found;
int index;
findIndex(key, found, index);
if(found)
{
table[index].key = -2;
table[index].data = NULL;
used--;
}
}
//modify insert(const RecordType & entry)
while(table[index].key != -1 || table[index].key != -2)
//modify findIndex
while(count < CAPACITY && table[i].key != -1
&& table[i].key != -2 && table[i].key != key)