哈希表实现大小调整功能

Hash Table implementing Resize function

本文关键字:调整 功能 实现 哈希表      更新时间:2023-10-16

我被分配了一项任务,调整哈希表的实现以允许动态调整大小。虽然我四处寻找线索和信息,但我仍然对哈希表的行为感到困惑,因此我应该如何调整大小。我被告知我需要添加一个大小调整函数,在插入和修改构造函数中调用它。我认为我已经做了正确的插入,似乎足够简单,但调整大小本身和构造函数是我正在努力。

编辑:所以我已经在调整大小函数,插入和构造函数工作,但我在resize()的for循环中得到一个错误,将旧数据插入到更大的表中,我不确定现在出了什么问题。什么好主意吗?

标题:

#ifndef TABLE1_H
#define TABLE1_H
#include <cstdlib>    // Provides size_t
#include <cassert>  // Provides assert
namespace main_savitch_12A
{
template <class RecordType>
class table
{
public:
size_t CAPACITY; 
    // CONSTRUCTOR
    table( );
    // MODIFICATION MEMBER FUNCTIONS
    void insert(const RecordType& entry);
    void remove(int key);
void resize( );
    // CONSTANT MEMBER FUNCTIONS
    bool is_present(int key) const;
    void find(int key, bool& found, RecordType& result) const;
    size_t size( ) const { return used; }
private:
    // MEMBER CONSTANTS -- These are used in the key field of special records.
    enum { NEVER_USED = -1 };
    enum { PREVIOUSLY_USED = -2 };
    // MEMBER VARIABLES
    RecordType *data; //[CAPACITY];
    size_t used;
    // HELPER FUNCTIONS
    size_t hash(int key) const;
    size_t next_index(size_t index) const;
    void find_index(int key, bool& found, size_t& index) const;
    bool never_used(size_t index) const;
    bool is_vacant(size_t index) const;
};

构造函数实现:

template <class RecordType>
table<RecordType>::table( )
{
    CAPACITY = 30;
    data = new RecordType[CAPACITY];
size_t i;
used = 0;
for (i = 0; i < CAPACITY; ++i)
    data[i].key = NEVER_USED;
}

调整实现:

template <class RecordType>
void table<RecordType>::resize( )
{
    RecordType *oldData = data;
    int oldSize = CAPACITY;
    CAPACITY *= CAPACITY;
    //create a new table
    RecordType *newData;
    newData = new RecordType[CAPACITY];
    size_t i;
    used = 0;
    for (i = 0; i < CAPACITY; i++)
        newData[i].key = NEVER_USED;
    //place data from old table to new, larger table.
    for (int i = 0; i < oldSize; i++)
    {
        insert(newData[hash(i)]); //this is where I'm having trouble.
    }
    data = newData;
    delete[] oldData;
}

插入实现:

template <class RecordType>
void table<RecordType>::insert(const RecordType& entry)
// Library facilities used: cassert
{
    bool already_present;   // True if entry.key is already in the table
    size_t index;        // data[index] is location for the new entry
    assert(entry.key >= 0);
    // Set index so that data[index] is the spot to place the new entry.
    find_index(entry.key, already_present, index);
    // If the key wasn't already there, then find the location for the new entry.
    if (!already_present)
    {
        if (size( ) >= CAPACITY)
        {
            resize( ); // resize the table.
            insert(entry); // reinsert entry into new table.
        }
        else if (size( ) < CAPACITY)
        {
            index = hash(entry.key);
            while (!is_vacant(index))
                index = next_index(index);
            ++used;
        }
    }
    data[index] = entry;
    size_t i;
    for (i=0; i<CAPACITY; i++) cout << data[i].key << ' ';
    cout << endl;
}

谢谢你的帮助!

目前,data是固定大小的数组。你需要一个可变大小的存储

RecordType data[CAPACITY];

因此,data需要是指针而不是固定数组,并且需要在构造函数中动态分配它。

当然,也改变CAPACITY从一个常量到一个变量(每个表,所以不是static -我会改变它的名字看起来不像一个宏/常量。

这位是"almost there":

table *newT;
newT = new table(newSize);

类型不应该是哈希表,而是RecordType

并且,正如注释所说,您需要将当前数据传输到新数据,然后使新表成为当前数据。最后,删除现在旧的数据。

编辑:我故意不为你写代码。你的任务是学习。大约20多年前,我编写了动态调整大小的哈希表,我认为我现在不需要练习它。你才是学习如何去做的人。