HASH TABLE NGHĨA LÀ GÌ

Giới thiệu

Thuật toán liên quan đến hash table được ứng dụng ở hầu hết các ngôn ngữ, là một trong những nền tảng về thuật toán và cấu tạo dữ liệu.

Bạn đang xem: Hash Table nghĩa là gì

Trong computing, hash table là một cấu trúc dữ liệu dùng làm lưu theo các cặp key value, nó sử dụng hash function để đo lường và tính toán tới một index, nơi tàng trữ một bucket các giá trị rồi từ này sẽ retrieve ra value, kim chỉ nam là nhằm độ tinh vi ở nút O(n)

Giải thích:

Có thể lấy một ví dụ đơn giản dễ dàng là câu hỏi lấy sách sinh hoạt thư viện, mỗi cuốn sách vào thư viện đều có môt quality number, đầy đủ cuốn sách này sẽ thu xếp trong cùng một địa chỉ (call number) toạ lạc bên trong thư viện, chúng ta sẽ phụ thuộc call number và chất lượng number nhằm tìm ra được cuốn sách đang cất ở đâu

Id của sách sẽ được sinh ra dựa trên một function điện thoại tư vấn là hash function,

*

 

Hash function rất có thể được implement theo rất nhiều cách, phương pháp cơ bạn dạng nhất dựa vào mã ascii của kí tự.

Ví dụ như dựa trên công thức

s.char
At(0) * 31n-1 + s.char
At(1) * 31n-2 + ... + s.char
At(n-1)

Ở đây s là string đề xuất chuyển, n là độ lâu năm của nó, ví dụ:

"ABC" = "A" * 312 + "B" * 31 + "C" = 65 * 312 + 66 * 31 + 67 = 64578

Giả sử họ lưu một bảng string “abcdef”, “bcdefa”, “cdefab” , “defabc” .Mã ASCII a, b, c, d, e, với f là 97, 98, 99, 100, 101, 102 , như ta thấy các thành phần phía trên đều phải có cùng số kí tự còn chỉ khác ở sự hoán vị.Thì index của bộ phận đó sẽ bằng tổng (mã ascii của kí từ bỏ * vị trí của kí tự kia trong chuỗi)

Chúng ta sẽ dùng mã ASCII để sinh ra các đoạn hash code cho một trong những phần tử, tổng tất các mã ascii hoàn toàn có thể xác định được kí trường đoản cú là 2069, nên chúng chia lại cho con số đó để chế tác index

String Hash function Index abcdef (971 + 982 + 993 + 1004 + 1015 + 1026)%2069 38bcdefa (981 + 992 + 1003 + 1014 + 1025 + 976)%2069 23cdefab (991 + 1002 + 1013 + 1024 + 975 + 986)%2069 14defabc (1001 + 1012 + 1023 + 974 + 985 + 996)%2069 11

*

 

Giải quyết collision

Vấn vạc sinh những trường hòa hợp trùng vị trí (index) nếu thuật toán hash ko được xuất sắc và hâù như không tồn tại thuật toán hash như thế nào thực sự hoàn hảo và tuyệt vời nhất để sinh ra quality key nếu tàng trữ một lượng lớn tài liệu , để giải quyết vấn đề này chúng ta dùng linked danh sách để lưu trữ thêm một tầng nữa các thành phần cho index đó.

Có thể coi vấn đề sinh hash là để tạo thành key
Hash cùng lưu vào vào mảng quý giá nơi chứa một linked các mục có thành phần chứa key thật của chúng ta, sau khi đến đó họ sẽ cần sử dụng key thật nhằm retrieve giá chỉ trị

 

*

 

 

*

 

Đây là đoạn code bản thân thấy khá xuất xắc và dễ hiểu về implement hastable:

import Linked
List from "../linked-list/Linked
List";// Hash table size directly affects on the number of collisions.// The bigger the hash table form size the less collisions you"ll get.// For demonstrating purposes hash table kích thước is small to lớn show how collisions// are being handled.const default
Hash
Table
Size = 32;export mặc định class Hash
Table /** *
param number hash
Table
Size */ constructor(hash
Table
Size = default
Hash
Table
Size) // Create hash table of certain form size and fill each bucket with empty linked list. This.buckets = Array(hash
Table
Size).fill(null).map(() => new Linked
List()); // Just to keep track of all actual keys in a fast way. This.keys = ; /** * Converts key string to hash number. * *
return number */ hash(key) const hash = Array.from(key).reduce( (hash
Accumulator, key
Symbol) => (hash
Accumulator + key
Symbol.char
Code
At(0)), 0, ); // Reduce hash number so it would fit hash table size. Return hash % this.buckets.length; /** *
param * value */ set(key, value) const key
Hash = this.hash(key); this.keys = key
Hash; const bucket
Linked
List = this.bucketsHash>; const node = bucket
Linked
List.find( callback: node
Value => node
Value.key === key ); if (!node) // Insert new node. Bucket
Linked
List.append( key, value ); else // Update value of existing node. Node.value.value = value; /** *
return * */ delete(key) const key
Hash = this.hash(key); delete this.keys; const bucket
Linked
List = this.bucketsHash>; const node = bucket
Linked
List.find( callback: node
Value => node
Value.key === key ); if (node) return bucket
Linked
List.delete(node.value); return null; /** *
return * */ get(key) const bucket
Linked
List = this.buckets; const node = bucket
Linked
List.find( callback: node
Value => node
Value.key === key ); return node ? node.value.value : undefined; /** *
return string<> */ get
Keys() return Object.keys(this.keys); Hy vọng các bạn hiểu hơn về hash map , hash table, thứ mà lại dân DEV họ hầu như xài cả ngày (hoho)

 

Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access of data becomes very fast if we know the index of the desired data.

Thus, it becomes a data structure in which insertion and tìm kiếm operations are very fast irrespective of the form size of the data. Hash Table uses an array as a storage medium và uses hash technique to generate an index where an element is khổng lồ be inserted or is to lớn be located from.

Hashing

Hashing is a technique khổng lồ convert a range of key values into a range of indexes of an array. We"re going to lớn use modulo operator to lớn get a range of key values. Consider an example of hash table of kích cỡ 20, và the following items are to be stored. Công trình are in the (key,value) format.

*
(1,20)(2,70)(42,80)(4,25)(12,44)(14,32)(17,11)(13,78)(37,98)Sr.No.Key
Hash
Array Index
1 1 1 % đôi mươi = 1 1
2 2 2 % trăng tròn = 2 2
3 42 42 % trăng tròn = 2 2
4 4 4 % trăng tròn = 4 4
5 12 12 % đôi mươi = 12 12
6 14 14 % đôi mươi = 14 14
7 17 17 % trăng tròn = 17 17
8 13 13 % 20 = 13 13
9 37 37 % trăng tròn = 17 17

Linear Probing

As we can see, it may happen that the hashing technique is used lớn create an already used index of the array. In such a case, we can tìm kiếm the next empty location in the array by looking into the next cell until we find an empty cell. This technique is called linear probing.

Sr.No.Key
Hash
Array Index
After Linear Probing, Array Index
1 1 1 % trăng tròn = 1 1 1
2 2 2 % 20 = 2 2 2
3 42 42 % trăng tròn = 2 2 3
4 4 4 % 20 = 4 4 4
5 12 12 % đôi mươi = 12 12 12
6 14 14 % đôi mươi = 14 14 14
7 17 17 % trăng tròn = 17 17 17
8 13 13 % 20 = 13 13 13
9 37 37 % 20 = 17 17 18

Basic Operations

Following are the basic primary operations of a hash table.

Search − Searches an element in a hash table.

Xem thêm: Trò chơi bài pyramid hướng dẫn cách chơi, cách chơi bài pyramid để thắng

Insert − inserts an element in a hash table.

delete − Deletes an element from a hash table.

Data
Item

Define a data công trình having some data and key, based on which the search is lớn be conducted in a hash table.

struct Data
Item int data; int key;;

Hash Method

Define a hashing method lớn compute the hash code of the key of the data item.

 

int hash
Code(int key) return key % SIZE;

Search Operation

Whenever an element is lớn be searched, compute the hash code of the key passed and locate the element using that hash code as index in the array. Use linear probing to get the element ahead if the element is not found at the computed hash code.

 

Example

struct Data
Item *search(int key) //get the hash int hash
Index = hash
Code(key); //move in array until an empty while(hash
ArrayIndex> != NULL) if(hash
ArrayIndex>->key == key) return hash
ArrayIndex>; //go to next cell ++hash
Index; //wrap around the table hash
Index %= SIZE; return NULL;

Insert Operation

Whenever an element is lớn be inserted, compute the hash code of the key passed and locate the index using that hash code as an index in the array. Use linear probing for empty location, if an element is found at the computed hash code.

 

Example

void insert(int key,int data) struct Data
Item *item = (struct Data
Item*) malloc(sizeof(struct Data
Item)); item->data = data; item->key = key; //get the hash int hash
Index = hash
Code(key); //move in array until an empty or deleted cell while(hash
ArrayIndex> != NULL && hash
ArrayIndex>->key != -1) //go to next cell ++hash
Index; //wrap around the table hash
Index %= SIZE; hash
ArrayIndex> = item;

Delete Operation

Whenever an element is to be deleted, compute the hash code of the key passed và locate the index using that hash code as an index in the array. Use linear probing to get the element ahead if an element is not found at the computed hash code. When found, store a dummy cống phẩm there to lớn keep the performance of the hash table intact.

 

Example

struct Data
Item* delete(struct Data
Item* item) int key = item->key; //get the hash int hash
Index = hash
Code(key); //move in array until an empty while(hash
ArrayIndex> !=NULL) if(hash
ArrayIndex>->key == key) struct Data
Item* temp = hash
ArrayIndex>; //assign a dummy chiến thắng at deleted position hash
ArrayIndex> = dummy
Item; return temp; //go lớn next cell ++hash
Index; //wrap around the table hash
Index %= SIZE; return NULL; To know about hash implementation in C programming language, please click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Sa tế tiếng anh là gì

  • Module thời gian thực rtc ds1307 là gì

  • What is ""Đồ vệ sinh cá nhân"" in american english and, những Điều bạn cần biết

  • Draught beer là gì

  • x

    Welcome Back!

    Login to your account below

    Retrieve your password

    Please enter your username or email address to reset your password.