Skip to content

Commit c6b8e19

Browse files
feat: implement division method
# Perubahan yang diberikan ### Menambahkan hash function menggunakan division method `division method` adalah salah satu hash function yang digunakan untuk memetakan value pada hash table ### Perubahan yang dilakuan 1.**Membuat direktori baru** ```bash ├── asset │   ├── hash_table_chaining.png │   ├── hash_table_collision.png │   ├── hash_table_visualisasi.png │   └── tabel_hash_kode_demo.png ├── hash_function │   └── division_method.cpp ├── hash_table.cpp └── readme.md ``` Menambahkan dir `hash_function` berisi program hash function yang dipakai pada hash table 2.**Menambahkan program division method** contoh output: ```bash demo insert 0-> nullptr 1->1 -> nullptr 2-> nullptr 3->3 -> nullptr 4-> nullptr 5->5 -> nullptr 6-> nullptr 7-> nullptr 8-> nullptr 9-> nullptr 10->10 -> nullptr demo search ditemukan ``` # Checklist: ##### Umum: - [x] Saya menambah algoritma terbaru. - [x] Saya memperbaiki dokumentasi. - [x] Saya menambah dokumentasi. ##### Contributor Requirements (Syarat Kontributor) dan Lain-Lain: - [x] Saya sudah membaca (I have read) [CONTRIBUTING](https://github.com/bellshade/CPP/blob/main/CONTRIBUTING.md) dan sudah menyetujui semua syarat. - [x] Saya telah menambahkan komentar kode yang memberikan penjelasan maksud dari kode yang saya buat. - [x] Saya menggunakan bahasa Indonesia untuk memberikan penjelasan dari kode yang saya buat. # Environment Saya menggunakan (I'm using): - ``OS`` = `linux` - ``g++`` = `15.2.1` # Link Issues Issues : # This Commit License https://github.com/bellshade/CPP/blob/main/license Co-authored-by: bellshadebot <[email protected]>
1 parent 0ba10e2 commit c6b8e19

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/**
2+
* @brief division method adalah salah satu hash_function yang digunakan
3+
* untuk memetakan value ke key tertentu pada hash table.
4+
* Division method dirumuskan dengan:
5+
* h(k) = k % m
6+
* dimana:
7+
* h(k) = nilai hash(index bucket)
8+
* k = key(nilai yang akan di hash)
9+
* m = besar hash table(jumlah bucket)
10+
*
11+
* @details pemilihan nilai m
12+
* disarankan agar memilih nilai m prima agar distribusi nilai hash merata
13+
* tidak terfokus pada nilai tertentu.
14+
*/
15+
16+
#include <iostream>
17+
18+
class HashTable {
19+
private:
20+
struct Node {
21+
int val;
22+
Node* next;
23+
// constructor list
24+
explicit Node(int val) {
25+
this->val = val;
26+
this->next = nullptr;
27+
}
28+
};
29+
30+
private:
31+
int m;
32+
Node** table;
33+
34+
public:
35+
// default constructor
36+
HashTable() : m(0) {
37+
table = new Node*[m];
38+
table[m] = nullptr;
39+
}
40+
41+
explicit HashTable(int size) : m(size) {
42+
table = new Node*[m];
43+
// isi index
44+
for (int i = 0; i < m; i++) {
45+
table[i] = nullptr;
46+
}
47+
}
48+
49+
HashTable(const HashTable& others) {
50+
m = others.m;
51+
table = new Node*[m];
52+
for (int i = 0; i < m; i++) {
53+
table[i] = nullptr;
54+
Node* curr = others.table[i];
55+
Node** tail = &table[i]; // store memory address to tail
56+
while (curr != nullptr) {
57+
*tail = new Node(curr->val);
58+
tail = &((*tail)->next);
59+
curr = curr->next;
60+
}
61+
}
62+
}
63+
64+
~HashTable() { clear(); }
65+
66+
public:
67+
int hash(int key) { return key % m; }
68+
69+
/**
70+
* @brief method untuk insert value pada hash table
71+
* cara kerja:
72+
* - hash key untuk menentukan index hash
73+
* - insert value pada key dengan menggunakan konsep
74+
* insert at head linked list
75+
* - update head
76+
* @param key nilai yang akan di insert
77+
* @details Complexity
78+
* Time Complexity O(1),Space Complexity O(1)
79+
*/
80+
void insert(int key) {
81+
// ambil index key
82+
int idx = hash(key);
83+
// buat node baru
84+
Node* new_node = new Node(key);
85+
// lakukan chaining pada index hash key
86+
new_node->next = table[idx];
87+
// update head baru
88+
table[idx] = new_node;
89+
}
90+
91+
/**
92+
* @brief method untuk searching value
93+
* cara kerja
94+
* - hash value untuk menemukan index hash
95+
* - cari value pada index hash dengan loop linked list
96+
* - kembalikan true jika ditemukan,sebaliknya return false
97+
* @param value nilai yang akan dicari
98+
* @details complexity method
99+
* time complexity O(1),space complexity O(1)
100+
*/
101+
bool search(int value) {
102+
int index = hash(value);
103+
Node* curr = table[index];
104+
while (curr != nullptr) {
105+
if (curr->val == value) {
106+
return true;
107+
}
108+
curr = curr->next;
109+
}
110+
return false;
111+
}
112+
113+
/**
114+
* @brief method untuk hapus value
115+
* karena menggunakan separate chaining maka memakai eager deletion
116+
* cara kerja
117+
* - hash value untuk menemukan index hash
118+
* - cari value pada index hash dengan loop linked list
119+
* - hapus value dan kembalikan true jika ditemukan,sebaliknya return false
120+
* @param value nilai yang akan dicari
121+
* @details complexity method
122+
* time complexity O(1),space complexity O(1)
123+
*/
124+
bool deletion(int value) {
125+
int index = hash(value);
126+
Node* curr = table[index];
127+
Node* prev = nullptr;
128+
while (curr != nullptr) {
129+
if (curr->val == value) {
130+
if (prev == nullptr) {
131+
table[index] = curr->next; // handle head deletion
132+
} else {
133+
prev->next = curr->next;
134+
}
135+
delete curr;
136+
return true;
137+
}
138+
prev = curr;
139+
curr = curr->next;
140+
}
141+
return false;
142+
}
143+
144+
public:
145+
void print_table() const noexcept {
146+
for (int i = 0; i < m; i++) {
147+
std::cout << i << "->";
148+
Node* curr = table[i];
149+
while (curr != nullptr) {
150+
std::cout << curr->val << " ->";
151+
curr = curr->next;
152+
}
153+
std::cout << " nullptr" << std::endl;
154+
}
155+
}
156+
157+
void clear() const noexcept {
158+
for (int i = 0; i < m; i++) {
159+
Node* curr = table[i];
160+
while (curr != nullptr) {
161+
Node* temp = curr;
162+
curr = curr->next;
163+
delete temp;
164+
}
165+
}
166+
delete[] table;
167+
}
168+
};
169+
170+
int main() {
171+
// pilih m prima agar distribusi merata
172+
HashTable hashMap(11);
173+
std::cout << "demo insert" << std::endl;
174+
hashMap.insert(10);
175+
hashMap.insert(5);
176+
hashMap.insert(1);
177+
hashMap.insert(3);
178+
hashMap.print_table();
179+
180+
std::cout << "demo search" << std::endl;
181+
bool s = hashMap.search(10);
182+
if (s) {
183+
std::cout << "ditemukan" << std::endl;
184+
} else {
185+
std::cout << "tidak ditemukan" << std::endl;
186+
}
187+
188+
// demo copy constructor
189+
HashTable hash = hashMap;
190+
hash.print_table();
191+
return 0;
192+
}

0 commit comments

Comments
 (0)