A set is a data structure that can store elements and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests an element for membership in a set. This structed is often used to ensure that no duplicates are present in a collection.
A set is a data structure that can store elements and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests an element for membership in a set. This structed is often used to ensure that no duplicates are present in a collection.
@ -96,8 +95,6 @@ func main() {
set.Empty() // true
set.Empty() // true
set.Size() // 0
set.Size() // 0
}
}
```
```
#####TreeSet
#####TreeSet
@ -125,7 +122,6 @@ func main() {
set.Empty() // true
set.Empty() // true
set.Size() // 0
set.Size() // 0
}
}
```
```
####Lists
####Lists
@ -220,8 +216,6 @@ func main() {
list.Add("a") // ["a"]
list.Add("a") // ["a"]
list.Clear() // []
list.Clear() // []
}
}
```
```
#####DoublyLinkedList
#####DoublyLinkedList
@ -257,8 +251,6 @@ func main() {
list.Add("a") // ["a"]
list.Add("a") // ["a"]
list.Clear() // []
list.Clear() // []
}
}
```
```
@ -279,14 +271,13 @@ type Interface interface {
// Clear()
// Clear()
// Values() []interface{}
// Values() []interface{}
}
}
```
```
#####LinkedListStack
#####LinkedListStack
This stack structure is based on a linked list, i.e. each previous element has a point to the next.
This stack structure is based on a linked list, i.e. each previous element has a point to the next.
All operations are guaranted constant time performance, except _Values()_, which is as always of linear time performance.
All operations are guaranteed constant time performance, except _Values()_, which is as always of linear time performance.
```go
```go
package main
package main
@ -307,7 +298,6 @@ func main() {
stack.Empty() // true
stack.Empty() // true
stack.Size() // 0
stack.Size() // 0
}
}
```
```
#####ArrayStack
#####ArrayStack
@ -335,8 +325,6 @@ func main() {
stack.Empty() // true
stack.Empty() // true
stack.Size() // 0
stack.Size() // 0
}
}
```
```
####Maps
####Maps
@ -361,7 +349,7 @@ type Interface interface {
#####HashMap
#####HashMap
Map structure based on hash tables, more exactly, Go's map. Keys are unordered.
Map structure based on hash tables, more exactly, Go's map. Keys are unordered.
All operations are guaranted constant time performance, except _Key()_ and _Values()_ retrieval that of linear time performance.
All operations are guaranted constant time performance, except _Key()_ and _Values()_ retrieval that of linear time performance.
@ -384,12 +372,11 @@ func main() {
m.Empty() // true
m.Empty() // true
m.Size() // 0
m.Size() // 0
}
}
```
```
#####TreeMap
#####TreeMap
Map structure based on our red-black tree implementation. Keys are ordered with respect to the passed comparator.
Map structure based on our red-black tree implementation. Keys are ordered with respect to the passed comparator.
_Put()_, _Get()_ and _Remove()_ are guaranteed log(n) time performance.
_Put()_, _Get()_ and _Remove()_ are guaranteed log(n) time performance.
@ -414,8 +401,6 @@ func main() {
m.Empty() // true
m.Empty() // true
m.Size() // 0
m.Size() // 0
}
}
```
```
####Trees
####Trees
@ -432,7 +417,7 @@ type Interface interface {
// Values() []interface{}
// Values() []interface{}
}
}
```
```
#####RedBlackTree
#####RedBlackTree
A red–black tree is a binary search tree with an extra bit of data per node, its color, which can be either red or black. The extra bit of storage ensures an approximately balanced tree by constraining how nodes are colored from any path from the root to the leaf. Thus, it is a data structure which is a type of self-balancing binary search tree.
A red–black tree is a binary search tree with an extra bit of data per node, its color, which can be either red or black. The extra bit of storage ensures an approximately balanced tree by constraining how nodes are colored from any path from the root to the leaf. Thus, it is a data structure which is a type of self-balancing binary search tree.
@ -487,9 +472,10 @@ func main() {
tree.Empty() // true
tree.Empty() // true
tree.Size() // 0
tree.Size() // 0
}
}
```
```
Extending the red-black tree's functionality has been demonstrated in the following [example](https://github.com/emirpasic/gods/blob/master/examples/redblacktreeextended.go).
#####BinaryHeap
#####BinaryHeap
A binary heap is a heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints:
A binary heap is a heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints:
@ -558,7 +544,7 @@ Return values:
-1, if a <b
-1, if a <b
0, if a == b
0, if a == b
1, if a > b
1, if a > b
Comparator signature:
Comparator signature:
type Comparator func(a, b interface{}) int
type Comparator func(a, b interface{}) int
@ -640,13 +626,13 @@ func byID(a, b interface{}) int {
Collections and data structures found in other languages: Java Collections, C++ Standard Template Library (STL) containers, Qt Containers, etc.
Collections and data structures found in other languages: Java Collections, C++ Standard Template Library (STL) containers, Qt Containers, etc.
## Goals
## Goals
**Fast algorithms**:
**Fast algorithms**:
- Based on decades of knowledge and experiences of other libraries mentioned above.
- Based on decades of knowledge and experiences of other libraries mentioned above.
**Memory efficient algorithms**:
**Memory efficient algorithms**:
- Avoiding to consume memory by using optimal algorithms and data structures for the given set of problems, e.g. red-black tree in case of TreeMap to avoid keeping redundant sorted array of keys in memory.
- Avoiding to consume memory by using optimal algorithms and data structures for the given set of problems, e.g. red-black tree in case of TreeMap to avoid keeping redundant sorted array of keys in memory.
**Easy to use library**:
**Easy to use library**:
- Well-structued library with minimalistic set of atomic operations from which more complex operations can be crafted.
- Well-structured library with minimalistic set of atomic operations from which more complex operations can be crafted.
**Stable library**:
**Stable library**:
- Only additions are permitted keeping the library backward compatible.
- Only additions are permitted keeping the library backward compatible.
**Solid documentation and examples**:
**Solid documentation and examples**:
- Learning by example.
- Learning by example.
**Production ready**:
**Production ready**:
- Still waiting for the project to mature and be used in some heavy back-end tasks.
- Used in production.
There is often a tug of war between speed and memory when crafting algorithms. We choose to optimize for speed in most cases within reasonable limits on memory consumption.
There is often a tug of war between speed and memory when crafting algorithms. We choose to optimize for speed in most cases within reasonable limits on memory consumption.