Hash Table 哈希表
2014-12-11 10:26
190 查看
What's a Hash Table? Why we need a Hash Table?
By Using a Hash Table we can find element very quickly. For example, There are 20 random number in an array below.It's not a sorted array, So We can not use Binary Search to finding a number, When we need to find 118,
We need 12 comparisons! Finding number like 270, 198, we need even more comparison.
We change the way the 20 number store. We store them in 10 linked lists. There is a rule, If the number's last digit is 0, so we insert it in a linked list which index is 0. Look at the Figure 1 for more detail. like number 118, it's last digit is 8, so
we insert it in ninth linked list.
So in this way, We find 118 only need 1 comparison in the ninth linked list! Finding number like 270 or 198, we need just 2 comparisons.
Hash Function
Note that the basic hash table which we explained above used a modulo function. There are many way to hash data, but modulo is the most common. What's more, the modulo function often use a prime number. That was because you get fewer collisions when you moduloa key by a prime number. Having fewer collisions makes your table easier to work and more efficient. There is a completed mathematical reasoning behind this, but it is okay for us to assume that this is true for us.
A Hash Table Class Example
You need have some STD knowledge, We will use "list" in STD instead of using customized linked list. And we also use "find" and "erase" function.[cpp] view
plaincopy
#ifndef _HASHTABLE_
#define _HASHTABLE_
#include <list>
#include <algorithm>
using namespace std;
class HashTable{
private:
list<int> containers[10];
int HashFunction(const int& v) const;
int count;
public:
HashTable();
~HashTable();
void Insert(const int& e);
bool Find(const int& e) const;
bool Delete(const int& e);
int Count() const;
};
int HashTable::HashFunction(const int& v) const{
return v%10;
}
HashTable::HashTable(){
count = 0;
}
HashTable::~HashTable(){
count = 0;
for(int i = 0; i< 10; ++i){
containers[i].clear();
}
}
void HashTable::Insert(const int& e){
const int hashResult = HashFunction(e);
containers[hashResult].push_back(e);
++count;
}
bool HashTable::Find(const int& e) const{
const int hashResult = HashFunction(e);
list<int>::const_iterator itr = find(containers[hashResult].begin(),
containers[hashResult].end(), e);
if(itr != containers[hashResult].end()){
return true;
}else{
return false ;
}
}
bool HashTable::Delete(const int& e){
const int hashResult = HashFunction(e);
list<int>::iterator itr = find(containers[hashResult].begin(),
containers[hashResult].end(), e);
if(itr != containers[hashResult].end()){
containers[hashResult].erase(itr);
--count;
return true;
}else{
return false;
}
}
int HashTable::Count()const{
return count;
}
#endif
[cpp] view
plaincopy
#ifndef _HASHTABLE_
#define _HASHTABLE_
#include <list>
#include <algorithm>
using namespace std;
class HashTable{
private:
list<int> containers[10];
int HashFunction(const int& v) const;
int count;
public:
HashTable();
~HashTable();
void Insert(const int& e);
bool Find(const int& e) const;
bool Delete(const int& e);
int Count() const;
};
int HashTable::HashFunction(const int& v) const{
return v%10;
}
HashTable::HashTable(){
count = 0;
}
HashTable::~HashTable(){
count = 0;
for(int i = 0; i< 10; ++i){
containers[i].clear();
}
}
void HashTable::Insert(const int& e){
const int hashResult = HashFunction(e);
containers[hashResult].push_back(e);
++count;
}
bool HashTable::Find(const int& e) const{
const int hashResult = HashFunction(e);
list<int>::const_iterator itr = find(containers[hashResult].begin(),
containers[hashResult].end(), e);
if(itr != containers[hashResult].end()){
return true;
}else{
return false ;
}
}
bool HashTable::Delete(const int& e){
const int hashResult = HashFunction(e);
list<int>::iterator itr = find(containers[hashResult].begin(),
containers[hashResult].end(), e);
if(itr != containers[hashResult].end()){
containers[hashResult].erase(itr);
--count;
return true;
}else{
return false;
}
}
int HashTable::Count()const{
return count;
}
#endif
A more completed Hash Table class
[cpp] viewplaincopy
#ifndef _HASHTABLE_
#define _HASHTABLE_
#include <list>
#include <string>
#include <algorithm>
using namespace std;
class Entry{
private:
int m_key;
string m_data;
public:
friend bool operator == (const Entry& e1,const Entry& e2);
Entry(){}
Entry(const int& key,const string& data){
m_key = key;
m_data = data;
}
void setKey(const int& key){
m_key = key;
}
void setData(const string& data){
m_data = data;
}
int getKey()const{
return m_key;
}
string getData()const{
return m_data;
}
};
bool operator ==(const Entry& e1,const Entry& e2){
return (e1.m_key == e2.m_key);
}
class HashTable{
private:
list<Entry> containers[10];
int HashFunction(const int& v) const;
int count;
public:
HashTable();
~HashTable();
void Insert(const Entry& entry);
bool Find(const int& key,list<Entry>::const_iterator& itr) const;
bool Delete(const int& key);
int Count() const;
};
int HashTable::HashFunction(const int& v) const{
return v%10;
}
HashTable::HashTable(){
count = 0;
}
HashTable::~HashTable(){
count = 0;
for(int i = 0; i< 10; ++i){
containers[i].clear();
}
}
void HashTable::Insert(const Entry& entry){
const int hashResult = HashFunction(entry.getKey());
containers[hashResult].push_back(entry);
++count;
}
bool HashTable::Find(const int& key,list<Entry>::const_iterator& out) const{
const int hashResult = HashFunction(key);
Entry entry;
entry.setKey(key);
list<Entry>::const_iterator itr = find(containers[hashResult].begin(),
containers[hashResult].end(), entry);
if(itr != containers[hashResult].end()){
out = itr;
return true;
}else{
return false ;
}
}
bool HashTable::Delete(const int& key){
const int hashResult = HashFunction(key);
Entry entry;
entry.setKey(key);
list<Entry>::iterator itr = find(containers[hashResult].begin(),
containers[hashResult].end(), entry);
if(itr != containers[hashResult].end()){
containers[hashResult].erase(itr);
--count;
return true;
}else{
return false;
}
}
int HashTable::Count()const{
return count;
}
#endif
Test the completed hash table class
[cpp] viewplaincopy
// HashTableApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "HashTable.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HashTable h;
h.Insert(Entry(234,"one"));
h.Insert(Entry(567,"two"));
h.Insert(Entry(987,"three"));
h.Insert(Entry(222,"four"));
h.Insert(Entry(564,"five"));
h.Insert(Entry(111,"six"));
h.Delete(234);
list<Entry>::const_iterator itr;
if(h.Find(234,itr)){
cout <<"The data with key 234: " << itr->getData() << endl;
}else{
cout << "Can not find the data with key 234 " << endl;
}
if(h.Find(111,itr)){
cout <<"The data with key 111: " << itr->getData() << endl;
}else{
cout << "Can not find the data with key 111" << endl;
}
cout << "Total count: " << h.Count() << endl;
int i;
cin >> i;
return 0;
}
相关文章推荐
- Hash Table 哈希表介绍
- Hash Table 哈希表 C++ 例子
- 哈希表(hash table)的基本知识
- [wget学习笔记] 哈希表 hash table (ZZ)
- PHP关联数组与哈希表(hash table) 不指定
- 纸上谈兵: 哈希表 (hash table)
- hash table相关--引入哈希桶的概念来实现一个哈希表
- 哈希表(Hash Table)
- 哈希表(hash table)
- Hash Table (哈希表)
- 什么叫哈希表(Hash Table)
- hash table 哈希表(散列表)
- 一个哈希表Hash Table的例子,用于IPv4协议的IP地址查找
- C语言-简单哈希表(hash table)
- C++数据结构 之 哈希表(散列表)_Hash Table
- Hash Table 哈希表 C++ 例子
- Hash Table 哈希表 C++ 例子
- 哈希表(Hash Table)
- 【数据结构】哈希表 Hash Table
- PHP Perl 关联数组 哈希表 Hash Table