哈希數據結構和代碼實現

在這裏插入圖片描述

主要結構體:

typedef enum
{
    EN_STATUS_EMPTY,
    EN_STATUS_DELETE,
    EN_STATUS_EXIST,
}ENUM_DATA_STATUS;

typedef int DATA_TYPE;

typedef struct
{
    DATA_TYPE   key;
    ENUM_DATA_STATUS status;
}STRU_HASH_NODE;

typedef struct
{
    STRU_HASH_NODE* hashNode;
    int size;
    int capacity;
}HASH_TABLE;

實現插入、刪除、查找、擴容、衝突解決等接口,用於理解哈希這種數據結構

#include "Hash.h"
#include <stdio.h>
#include <memory>

int InitHash(HASH_TABLE* table, int capacity)
{
    if(NULL == table)
    {
        return DA_ERROR;
    }
    table->size = 0;
    table->capacity = capacity;
    table->capacity = GetCapacity(table);

    table->hashNode = (STRU_HASH_NODE*)malloc(sizeof(STRU_HASH_NODE) * table->capacity);

    for (int i = 0; i < table->capacity; i++)
    {
        table->hashNode[i].status = EN_STATUS_EMPTY;
    }

    return DA_SUCCESS;
}

/*重新擴容時,哈希表大小*/
int GetCapacity(HASH_TABLE* table)
{
    //哈希表容量遞增表,一個合適的素數序列,減少衝突的可能
    const int nPrimeSize = 13;
    const int HashSize[] = 
    {
        7,13,17,101,
        211,307,401,503,
        601,701,809,907,
        997
    };

    for (int i = 0; i < nPrimeSize; i++)
    {
        if (table->capacity < HashSize[i])
        {
            return HashSize[i];
        }
    }

    return HashSize[nPrimeSize - 1];
}

bool CheckCapacity(HASH_TABLE* table)
{
    //檢查負載因子大小
    int check_capacity = (table->size * 10) / table->capacity;
    if (check_capacity >= 7)
    {
        return true;
    }
    return false;
}

/*哈希表擴容*/
int RecreateHashTable(HASH_TABLE *table)
{
    HASH_TABLE NewTable;
    //創建新的哈希表
    InitHash(&NewTable,table->capacity);

    for (int i = 0; i < table->capacity; i++)
    {
        if (EN_STATUS_EXIST == table->hashNode[i].status)
        {
            InsertHash(&NewTable,table->hashNode[i].key);
        }
    }

   if (table->hashNode != NULL)
   {
       free(table->hashNode);
       table->hashNode = NULL;
   }

   table->capacity = NewTable.capacity;
   table->size = NewTable.size;
   table->hashNode = NewTable.hashNode;
   return DA_SUCCESS;
}

/*除留餘數法*/
int HashFun(HASH_TABLE* table, DATA_TYPE data)
{
    return data % table->capacity;
}

/*衝突時,線性探測返回新的地址*/
int Collision(HASH_TABLE *table, int hash_addr)
{
    return (hash_addr + 1) % (table->capacity);
}

/*哈希表插入關鍵字*/
/*
step1:給出對應的哈希地址
step2:判斷當前哈希地址是否有衝突
step3:有衝突則採用衝突策略,否則將該元素放在該位置
*/
int InsertHash(HASH_TABLE *table, DATA_TYPE data)
{
    if (CheckCapacity(table))
    {
        printf("hash table need to recreate\n");
        RecreateHashTable(table);
    }

    int hash_addr = HashFun(table, data);
    int index = hash_addr;
      
    //衝突檢測以及衝突解決方法:線性探測法
    while (table->hashNode[index].status == EN_STATUS_EXIST)
    {
        if (table->hashNode[index].key == data)
        {
            return DA_ALREADY_EXIST;
        }

        index = Collision(table,index);
    }

    printf("key = %d hash addr=%d target= %d\n",data, hash_addr,index);

    table->hashNode[index].key = data;
    table->hashNode[index].status = EN_STATUS_EXIST;
    table->size += 1;
    return DA_SUCCESS;
}

/*哈希表關鍵字搜索*/
bool SearchHash(HASH_TABLE* table, DATA_TYPE data)
{
    int hash_addr = HashFun(table, data);
    int index = hash_addr;
    //線性探測法,查找
    while (table->hashNode[index].status == EN_STATUS_EXIST)
    {
        //相等則存在
        if (table->hashNode[index].key == data)
        {
            return true;
        }

        //線性探測下一個元素
        index = Collision(table,index);
    }
    
    return false;
}

/*刪除哈希關鍵字*/
/*
採用線性探測法處理散列時的衝突,
當從哈希表刪除一個記錄時,不應將這個記錄的所在位置置空,
否則影響其他記錄的查找
*/
bool RemoveHash(HASH_TABLE* table, DATA_TYPE data)
{
    int hash_addr = HashFun(table, data);

    int index = hash_addr;
    //線性探測法,查找
    while (table->hashNode[index].status == EN_STATUS_EXIST)
    {
        //相等則存在
        if (table->hashNode[index].key == data)
        {
            table->hashNode[index].status = EN_STATUS_DELETE;
            table->size--;
            return true;
        }
        //線性探測下一個元素
        index = Collision(table,index);
    }

    return false;
}

void DestoryHash(HASH_TABLE* table)
{
    table->capacity = 0;
    table->size = 0;
    free(table->hashNode);
    table->hashNode = NULL;
}

void PrintHash(HASH_TABLE* table)
{
   printf("哈希表容量爲%d, 元素個數爲%d\n",table->capacity,table->size);
   for (int i = 0; i < table->capacity; i++)
   {
       if (table->hashNode[i].status == EN_STATUS_EXIST)
       {
           printf("key=%d\t",table->hashNode[i].key);
       }
   }
   printf("\n");
   for (int i = 0; i < table->capacity; i++)
   {
       if (table->hashNode[i].status == EN_STATUS_DELETE)
       {
           printf("delete key=%d\t",table->hashNode[i].key);
       }
   }

   printf("\n");
}

void test_hash()
{
    HASH_TABLE  ht;
    InitHash(&ht, 0);//哈希表初始化
    printf("begin insert data....\n");

    InsertHash(&ht, 37);//插入數據
    InsertHash(&ht, 25);//插入數據
    InsertHash(&ht, 11);//插入數據
    InsertHash(&ht, 36);//插入數據
    InsertHash(&ht, 41);//插入數據
    InsertHash(&ht, 42);//插入數據

    PrintHash(&ht);//打印哈希表;

    printf("begin search data....\n");
    
    if (SearchHash(&ht,11))
    {
        printf("find  data success...\n");
    }
    else
    {
        printf("find data fail...\n");
    }

    printf("begin remove data....\n");

    RemoveHash(&ht, 11);//插入數據

    PrintHash(&ht);//打印哈希表;

    DestoryHash(&ht);
}

完整代碼參見github:

https://github.com/jinxiang1224/cpp/tree/master/DataStruct_Algorithm/hash

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章