Last active
November 26, 2021 09:40
-
-
Save jerryan999/21191583759d00ac4314dee8ddf9283f to your computer and use it in GitHub Desktop.
Hash Tables Implementation in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// implementing a hash table in go | |
package main | |
import "fmt" | |
// A programmer from China, who loves Go, Python, AI, Machine Learning, and Data Science. To support me join Medium: https://jerryan.medium.com/membership | |
const SIZE = 15 | |
// single linked list | |
type Node struct { | |
Val int | |
Next *Node | |
} | |
// HASH table | |
type HashTable struct { | |
Table map[int]*Node | |
Size int | |
} | |
// NewHashTable() returns a new hash table with the given size. | |
func NewHashTable(size int) *HashTable { | |
return &HashTable{ | |
Table: make(map[int]*Node, size), | |
Size: size, | |
} | |
} | |
// traverse hash table | |
// The traverse() function is used for printing all of the values in the hash table. | |
// The function visits each of the linked lists of the hash table and prints the stored values, linked list by linked list. | |
func (h *HashTable) Traverse() { | |
for i := 0; i < h.Size; i++ { | |
node := h.Table[i] | |
for node != nil { | |
fmt.Printf("%d ->", node.Val) | |
node = node.Next | |
} | |
fmt.Println() | |
} | |
} | |
// lookup function | |
func (h *HashTable) Lookup(key int) bool { | |
// Get the hash of the key. | |
hash := hash(key, h.Size) | |
// Get the bucket of the hash. | |
bucket := h.Table[hash] | |
// Traverse the linked list. | |
for bucket != nil { | |
if bucket.Val == key { | |
return true | |
} | |
bucket = bucket.Next | |
} | |
return false | |
} | |
// Add() adds a new key-value pair to the hash table. | |
// note: current implementation of the Add function does not check for duplicate values | |
func (h *HashTable) Add(key int, value int) { | |
// Get the hash of the key. | |
hash := hash(key, h.Size) | |
// Get the bucket of the hash. | |
bucket := h.Table[hash] | |
// Create a new node with the key and value. | |
node := &Node{ | |
Val: value, | |
Next: bucket, | |
} | |
// Add the node to the bucket. | |
h.Table[hash] = node | |
} | |
// | |
// hash function | |
func hash(key int, size int) int { | |
// The hashFunction() uses the modulo operator. | |
// The main reason for choosing the modulo operator is because this particular hash table has to cope with integer values. If you were dealing with strings or floating-point numbers, then you would use a different logic in your hash function. | |
return key % size | |
} | |
func main() { | |
// create hash table | |
table := NewHashTable(SIZE) | |
// add values to hash table | |
for i := 0; i < SIZE*5; i++ { | |
table.Add(i, i) | |
} | |
// traverse hash table | |
table.Traverse() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
result