import "github.com/maksimru/go-hpds"
import "github.com/maksimru/go-hpds/priorityqueue"
arr := []IntPrioritizedValue{NewIntPrioritizedValue(21,5),NewIntPrioritizedValue(2,7),NewIntPrioritizedValue(432,1)}
pq := NewPriorityQueue(NewIntPrioritizedValueList(arr), NewIntPriorityComparator())
for !pq.IsEmpty() {
value := pq.Dequeue() //returns instance of IntPrioritizedValue value = 2, priority = 7
}
Methods
Object
Operation
Time Complexity
Space Complexity
Comment
PriorityQueue
Enqueue
O(log N)
O(1)
Adds element to the priority queue
PriorityQueue
Dequeue
O(log N)
O(1)
Removes element from the top of the priority queue
PriorityQueue
Top
O(1)
O(1)
Get top of priority queue
PriorityQueue
IsEmpty
O(1)
O(1)
Checks priority queue is empty
PriorityQueue
GetLength
O(1)
O(1)
Gets length of the priority queue
PriorityQueue
Clean
O(1)
O(1)
Removes all elements from the priority queue
PriorityQueue
NewPriorityQueue
O(N)
O(1)
Build priority queue
Trie
Usage
import "github.com/maksimru/go-hpds"
import "github.com/maksimru/go-hpds/trie"
var trie = NewTrie()
trie.Add("word", 1)
trie.Add("work", 2)
trie.Add("wor", 3)
trie.Add("war", 4)
trie.Add("won", 5)
v, s := trie.SearchPattern("w?r") //s will return 3 and 4
Methods
Object
Operation
Time Complexity
Space Complexity
Comment
-
NewTrie
O(1)
O(1)
Builds empty trie
Trie
GetRoot
O(1)
O(1)
Returns root trie node
Trie
Add
O(M)
O(M)
Adds word with custom value to the trie
Trie
Search
O(M)
O(1)
Searches word in trie using exact match
Trie
SearchPrefix
O(M+N*L)
O(1)
Searches words in trie using prefix
Trie
SearchPattern
O(M*A^X)
O(1)
Searches keyword in Trie using pattern match, "?" will match any single char
-
NewNode
O(1)
O(1)
Creates node
Node (Trie)
GetChar
O(1)
O(1)
Get character in specific node
Node (Trie)
GetValue
O(1)
O(1)
Get value in specific node
Node (Trie)
HasTerminator
O(1)
O(1)
Checks if node is the end of word
Node (Trie)
HasChildNodes
O(1)
O(1)
Checks if node has sub nodes
Union-Find (Disjoint-Set)
Usage
import "github.com/maksimru/go-hpds"
import "github.com/maksimru/go-hpds/unionfind"
union := NewUnionFind()
union.Union(1,2)
union.Union(2,3)
union.Union(4,3)
node := union.FindInSet(4) //returns Node with value 1 and rank 4, meaning that all four nodes are in union
Methods
Object
Operation
Time Complexity
Space Complexity
Comment
-
NewUnionFind
O(1)
O(1)
Builds empty union-find
UnionFind
FindInSet
O(M)
O(1)
Returns parent node of the union searching by value of its member
UnionFind
Union
O(M)
O(M)
Joins two unions using any values belonging to them
UnionFind
Has
O(1)
O(1)
Checks if specific union exists
-
NewNode
O(1)
O(1)
Creates node
Node (UnionFind)
GetRank
O(1)
O(1)
Get rank in specific node
Node (UnionFind)
GetValue
O(1)
O(1)
Get value of specific node
Testing
To run all tests in this module:
go test ./...
About
High performance and easy to use datastructure packages for Golang