您的位置:首页 > 其它

哈希表(hashtable)

2016-04-08 17:24 239 查看
HashTable: 哈希表/散列表,是根据关键字(key)直接访问数据的数据结构。

它通过一个关键值的函数将所需的数据映射到表中的位置来访问数据,这个映射函数叫做散列函数,存放记录的数组叫做散列表。

构造哈希表的几种方法
1.直接定址法--取关键字的某个线性函数为散列地址,Hash(Key)= Key 或 Hash(Key)= A*Key + B,A、B为常数。
2.除留余数法--取关键值被某个不大于散列表长m的数p除后的所得的余数为散列地址。Hash(Key)= Key % P。
3.平方取中法
4.折叠法
5.随机数法
6.数学分析法

哈希冲突/哈希碰撞

不同的Key值经过哈希函数Hash(Key)处理以后可能产生相同的值哈希地址,我们称这种情况为哈希冲突。任意的散列函数都不能避免产生冲突。

处理哈希冲突的闭散列方法有:1.线性探测 2.二次探测

线性探测实现代码如下:

#include <iostream>
using namespace std;

enum Status
{
EXIT,
DELETE,
EMPTY,
};

template<class K>
class HashTable
{
public:
HashTable()
:_table(NULL)
, _size(0)
, _cpacity(0)
, _status(0)
{}

HashTable(size_t capacity)
:_table(new K[capacity])
, _size(0)
, _capacity(capacity)
, _status(new Status[capacity])
{
for (size_t i = 0; i < _capacity; i++)
{
_status[i] = EMPTY;
}
}

~HashTable()
{
if (_table)
{
delete[] _table;
}
if (_status)
{
delete[] _status;
}
}

public:
bool Insert(const K& x)
{
if ((_size * 10 / _capacity) > 8)
{
HashTable ht(_capacity * 2);
int i = 0;
Swap(ht);
while (i < ht._capacity)
{
if (ht._status[i] == EXIT)
{
Insert(ht._table[i]);
}
i++;
}
}
int index = HashFunc(x);
while (_status[index] == EXIT)
{
index++;
if (index == _capacity)
{
index = 0;
}
}
_table[index] = x;
_status[index] = EXIT;
_size++;
return true;
}

bool Remove(const K& x)
{
int index = HashFunc(x);
while (_status[index] != EMPTY)
{
if (_table[index] == x)
{
_status[index] = DELETE;
return true;
}
index++;
if (index == _capacity)
{
index = 0;
}
}
return false;
}

bool Find(const K& x)
{
int index = HashFunc(x);
while (_status[index] != EMPTY)
{
if (_table[index] == x)
{
return true;
}
index++;
if (index == _capacity)
{
index = 0;
}
}
return false;
}

void Print()
{
for (size_t i = 0; i < _capacity; ++i)
{
cout << _status[i] << ":";
cout << _table[i] << " ";
}
cout << endl;
}

protected:

void Swap(HashTable<int>& ht)
{
swap(_table, ht._table);
swap(_status, ht._status);
swap(_size, ht._size);
swap(_capacity, ht._capacity);
}

size_t HashFunc(const K& x)
{
return (x % _capacity);
}

private:
K* _table;
size_t _size;
size_t _capacity;
Status* _status;
};

int main()
{
HashTable<int> ht1(10);
ht1.Insert(89);
ht1.Insert(18);
ht1.Insert(49);
ht1.Insert(58);
ht1.Insert(9);
ht1.Insert(55);
ht1.Insert(34);
ht1.Insert(44);
ht1.Insert(99);
ht1.Insert(22);
ht1.Print();
ht1.Remove(49);
ht1.Print();
ht1.Find(58);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: