txt如何单独单独选择一列

Prerequisite: Hashing data structure

先决条件: 哈希数据结构

单独链接 (Separate chaining)

In separate chaining, we maintain a linked chain for every index in the hash table. So whenever there is a Collison the linked list is extended for that particular location of the hash table.

在单独的链接中 ,我们为哈希表中的每个索引维护一个链接链。 因此,只要有Collison,链表都会扩展到哈希表的特定位置。

We can visualize the separate chaining method with the following example,

通过以下示例,我们可以可视化单独的链接方法:

Key set: {123, 456, 763, 656, 908, 238, 231}
Hash function: f(x) = x%10

Step 1: Inserting 123 in the hash map. So, location is 3.

步骤1:在哈希图中插入123。 因此,位置为3。

Index	keys
0	NULL
1	NULL
2	NULL
3	123->NULL
4	NULL
5	NULL
6	NULL
7	NULL
8	NULL
9	NULL

Step 2: Inserting 456 in the hash map. So, location is 3.

步骤2:在哈希图中插入456。 因此,位置为3。

Index	keys
0	NULL
1	NULL
2	NULL
3	123->NULL
4	NULL
5	NULL
6	456->NULL
7	NULL
8	NULL
9	NULL

Step 3: Inserting 763 in the hash map. So, location is 3.

步骤3:在哈希图中插入763。 因此,位置为3。

Index	keys
0	NULL
1	NULL
2	NULL
3	123->763->NULL
4	NULL
5	NULL
6	456->NULL
7	NULL
8	NULL
9	NULL

Step 4: Inserting 656 in the hash map. So, location is 6.

步骤4:在哈希图中插入656。 因此,位置为6。

Index	Keys
0	NULL
1	NULL
2	NULL
3	123->763->NULL
4	NULL
5	NULL
6	456->656->NULL
7	NULL
8	NULL
9	NULL

Step 5: Inserting 908 in the hash map. So, location is 8.

步骤5:在哈希图中插入908。 因此,位置为8。

Index	Keys
0	NULL
1	NULL
2	NULL
3	123->763->NULL
4	NULL
5	NULL
6	456->656->NULL
7	NULL
8	908->NULL
9	NULL

Step 6: Inserting 238 in the hash map. So, location is 8.

步骤6:在哈希图中插入238。 因此,位置为8。

Index	Keys
0	NULL
1	NULL
2	NULL
3	123->763->NULL
4	NULL
5	NULL
6	456->656->NULL
7	NULL
8	908->238->NULL
9	NULL

Step 7: Inserting 231 in the hash map. So, location is 8.

步骤7:在哈希图中插入231。 因此,位置为8。

Index	Keys
0	NULL
1	231->NULL
2	NULL
3	123->763->NULL
4	NULL
5	NULL
6	456->656->NULL
7	NULL
8	908->238->NULL
9	NULL

So, the above thing is called as separate chaining as we have chained the collided elements within the hash table.

因此,由于我们在哈希表中链接了冲突的元素,因此上述事情称为单独链接。

Performance analysis:

性能分析:

Load factor = n/m, 
    n = number of keys inserted, 
    m = number of indices in the hash table() size of hash table

Search complexity = O(load factor)
Insertion complexity = O(1)
Deletion complexity = O(load factor)

单独链接的优缺点 (Advantage and disadvantages of separate chaining)

Advantages are,

优点是

  • We can add as many keys as we want to add

    我们可以添加任意数量的键

  • It's simpler than open addressing to implement

    它比开放地址更容易实现

Disadvantages are,

缺点是

  • It uses extra spaces for links

    它为链接使用多余的空间

  • If the collision rate is high, the search complexity increases as load factor increases

    如果冲突率很高,则搜索复杂度会随着负载因子的增加而增加

单独链接的C ++实现 (C++ implementation of separate chaining)

//separate chaining
#include <bits/stdc++.h>
using namespace std;

class node {
public:
    int data;
    node* next;

    node()
    {
        data = 0;
        next = NULL;
    }
    node(int x)
    {
        data = x;
        next = NULL;
    }
};

node* add(node* head, int data)
{
    if (head == NULL) {
        head = new node(data);
        return head;
    }

    node* temp = head;
    while (temp->next) {
        temp = temp->next;
    }
    temp->next = new node(data);
    return head;
}

void print(node* head)
{

    if (!head) {
        cout << "NULL\n";
        return;
    }
    node* temp = head;
    while (temp) {
        cout << temp->data << "->";
        temp = temp->next;
    }
    cout << "NULL\n";
}

int main()
{
    //set of input numbers
    vector<int> arr{ 123, 456, 763, 656, 908, 238, 231 };

    //initialize the hash table
    //each entry of the hash table is a linkedlist
    vector<node*> hash(10); //size of hashtable is 10

    //using hash function f(x)=no of digits in x
    for (int a : arr) {
        hash[a % 10] = add(hash[a % 10], a);
    }

    cout << "Hash table is:\n";
    for (int i = 0; i < 10; i++) {
        cout << i << "---- ";
        print(hash[i]);
    }

    return 0;
}

Output:

输出:

Hash table is:
0---- NULL
1---- 231->NULL
2---- NULL
3---- 123->763->NULL
4---- NULL
5---- NULL
6---- 456->656->NULL
7---- NULL
8---- 908->238->NULL
9---- NULL


翻译自: https://www.includehelp.com/data-structure-tutorial/hashing-separate-chaining-for-collision-resolution.aspx

txt如何单独单独选择一列

更多推荐