Skip to content

Instantly share code, notes, and snippets.

@jerryan999
Last active November 26, 2021 09:40
Show Gist options
  • Save jerryan999/21191583759d00ac4314dee8ddf9283f to your computer and use it in GitHub Desktop.
Save jerryan999/21191583759d00ac4314dee8ddf9283f to your computer and use it in GitHub Desktop.
Hash Tables Implementation in Go
// 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()
}
@jerryan999
Copy link
Author

result

61 ->46 ->31 ->16 ->1 ->
62 ->47 ->32 ->17 ->2 ->
63 ->48 ->33 ->18 ->3 ->
64 ->49 ->34 ->19 ->4 ->
65 ->50 ->35 ->20 ->5 ->
66 ->51 ->36 ->21 ->6 ->
67 ->52 ->37 ->22 ->7 ->
68 ->53 ->38 ->23 ->8 ->
69 ->54 ->39 ->24 ->9 ->
70 ->55 ->40 ->25 ->10 ->
71 ->56 ->41 ->26 ->11 ->
72 ->57 ->42 ->27 ->12 ->
73 ->58 ->43 ->28 ->13 ->
74 ->59 ->44 ->29 ->14 ->```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment