Data Structure and Algorithm Practice Series Articles (8) Hash

hash

hash

Goal: For faster search—O(1), exchange space for time, and calculate the spatial position faster

definition

A collection of "key-value pairs"

two key questions

  • Hash Function What is a good hash function

  • how to resolve conflict

conflict resolution
  • open addressing

  • split chain

  • other (public overflow)

open addressing

h(x) = x mod 11

x={12,2,17,28,6,23} h(x) = {1,2,6,6,1}

In this way, there will be conflicts. When there is a conflict, the data can be put into the subsequent coordinate points for linear detection and square detection. . .

split chain

h(x) = x mod 11

x={12,2,17,28,6,23} h(x) = {1,2,6,6,1}

It is to make a linked list node at this node. Insertion and deletion of singly linked list

C implementation
// 开放定址法,删除时时不能真正的删,只能逻辑的删除,让其标记
#include <stdio.h>
#include <stdio.h>
// 标记的状态
enum GridStatus{
    
    Active,Removed,Empty};
typedef enum GridStatus Status;
struct HashTable{
    
    
    int *key; // key
    Status *status; // 标记的状体
    int size; // 表里的大小
    int remains; // 表中剩余的大小
};
typedef struct HashTable *HT;
HT initHashTable(int size){
    
    
    HT h;
    h= (HT) malloc(sizeof(struct HashTable));
    if(!h) return NULL;
    h->size = h->remains =size;
    h->key = (int *)malloc(sizeof(Status)* size);
    if(!h->status){
    
     free(h->key);free(h); return NUll;}
    for(int i=0;i<size;i++){
    
    
        h->status[i]=Empty;
    }
    return h;
}
// hash函数
int hash(int x,int p){
    
    
    return x%p;
}
int isFull(const HT h){
    
    
    return h->remains == 0;
}
// 插入
int insertX(int x, HT h){
    
    
    if(isFull(h)) return 0;
    int pos=hash(x,h->size);
    while(h->status[pos] == Active){
    
    
        pos = (pos+1) % h -> size;
    }
    h->key[pos] = x;
    h->status[pos] =Active;
    h->remains--;
    return 1;
}
// 查找
int findX(int x,HT h){
    
    
  int pos,index;
  index=pos= hash(x,h->size);
  while(h-status[pos]!=Empty){
    
    
      if(h->key[pos] == x && h-status[pos] == Active) return pos;
      pos= (pos+1)% h->size; // 循环一圈
      if(pos == index) break;
      return -1;
  }
}
//remove
int removeX(int x, HT h){
    
    
    int pos = findX(x,h);
    if(pos==-1) return 0;
    // 只做标记,不修改其中的值,这是绝大部分就是这样的。
    h->status[pos] = Empty; 
}
void printHT(const HT h){
    
    
    for(int i=0;i<h-size;i++){
    
    
        printf("%4d",i){
    
    
    }
    putchar('\n');
    for(int i=0;i<h-size;i++){
    
    
        if(h->status[i] == Active) printf("%4d",h->key[i]);
        else if(h->status[i]==Removed) printf("   X");
        else printf("   -");
    }
    putchar('\n');
}
int main(){
    
    
    HT h=initHashTable(11);
    insertX(5,h);
    return 0;
}
C++
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
int main() {
    unordered_set<int> s;
    unordered_map<int, int> m;
    m[101] =33;
    m[-10] = 2233;
    m.inser(pair<int,int>(22,33));
    return 0;
}
Java
Map-->HashMap; 键值对映射
Set--->HashSet;

The hashmap in JAVA is an array + linked list before jdk1.8, and an array + linked list + red-black tree is used after jdk1.8. When the number of elements is 8, the linked list is converted into a red-black tree, so When the number of hashmap elements is around 8, performance will be consumed. The number of initial value elements of hashMap is 16, the self-increment factor of hashMap is 0.75, and when the number of elements is 32, the initial capacity is 32/0.75=.

Python
d = dict() # 字典
d[100] =50

Guess you like

Origin blog.csdn.net/qq_37256896/article/details/117074046