diff --git a/README.md b/README.md index 361086a..d83fe88 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -[](https://travis-ci.org/emirpasic/gods) [](https://godoc.org/github.com/emirpasic/gods) +[](https://godoc.org/github.com/emirpasic/gods) [](https://travis-ci.org/emirpasic/gods) [](https://github.com/emirpasic/gods/blob/enums/LICENSE) # GoDS (Go Data Structures) -Implementation of various data structures in Go. +Implementation of various data structures and algorithms in Go. ## Data Structures @@ -25,7 +25,14 @@ Implementation of various data structures in Go. - [BinaryHeap](#binaryheap) - [Functions](#functions) - [Comparator](#comparator) + - [Iterator](#iterator) + - [IteratorWithIndex](#iteratorwithindex) + - [IteratorWithKey](#iteratorwithkey) + - [Enumerable](#enumerable) + - [EnumerableWithIndex](#enumerablewithindex) + - [EnumerableWithKey](#enumerablewithkey) - [Sort](#sort) + - [Container](#container) ## Containers @@ -41,32 +48,27 @@ type Container interface { } ``` -Containers are either ordered or unordered: - -- Ordered containers: - - ArrayList - - SinglyLinkedList - - DoublyLinkedList - - TreeSet - - LinkedListStack - - ArrayStack - - TreeMap - - RedBlackTree - - BinaryHeap -- Unordered containers: - - HashSet - - HashMap - -All ordered containers have stateful iterators: - -- IteratorWithIndex (iterates over containers whose elements are referenced by an index) -- IteratorWithKey (iterates over containers whose elements are referenced by a key) +Containers are either ordered or unordered. All ordered containers provide [stateful iterators](iterator) and some of them allow [enumerable functions](#enumerable). + +| Container | Ordered | [Iterator](iterator) | [Enumerable](#enumerable) | Ordered by | +| :--- | :---: | :---: | :---: | :---: | +| [ArrayList](#arraylist) | yes | yes | yes | index | +| [SinglyLinkedList](#singlylinkedlist) | yes | yes | yes | index | +| [DoublyLinkedList](#doublylinkedlist) | yes | yes | yes | index | +| [HashSet](#hashset) | no | no | no | index | +| [TreeSet](#treeset) | yes | yes | yes | index | +| [LinkedListStack](#linkedliststack) | yes | yes | no | index | +| [ArrayStack](#arraystack) | yes | yes | no | index | +| [HashMap](#hashmap) | no | no | no | key | +| [TreeMap](#treemap) | yes | yes | yes | key | +| [RedBlackTree](#redblacktree) | yes | yes | yes | key | +| [BinaryHeap](#binaryheap) | yes | yes | yes | index | ### Lists -A list is a data structure that can store values and may have repeated values. There is no ordering in a list. The user can access and remove a value by the index position. +A list is a data structure that stores values and may have repeated values. -All lists implement the list interface with the following methods: +Implements [Container](#containers) interface. ```go type List interface { @@ -88,10 +90,9 @@ type List interface { #### ArrayList -This structure implements the List interface and is backed by a dynamic array that grows and shrinks implicitly (by 100% when capacity is reached). - -Direct access method _Get(index)_ is guaranteed a constant time performance. Remove is of linear time performance. Checking with _Contains()_ is of quadratic complexity. +A [list](#lists) backed by a dynamic array that grows and shrinks implicitly. +Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex) and [EnumerableWithIndex](#enumerablewithindex) interfaces. ```go package main @@ -126,9 +127,9 @@ func main() { #### SinglyLinkedList -This structure implements the _List_ interface and is a linked data structure where each value points to the next in the list. +A [list](#lists) where each element points to the next element in the list. -Direct access method _Get(index)_ and _Remove()_ are of linear performance. _Append_ and _Prepend_ are of constant time performance. Checking with _Contains()_ is of quadratic complexity. +Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex) and [EnumerableWithIndex](#enumerablewithindex) interfaces. ```go package main @@ -163,9 +164,9 @@ func main() { #### DoublyLinkedList -This structure implements the _List_ interface and is a linked data structure where each value points to the next and previous element in the list. +A [list](#lists) where each element points to the next and previous elements in the list. -Direct access method _Get(index)_ and _Remove()_ are of linear performance. _Append_ and _Prepend_ are of constant time performance. Checking with _Contains()_ is of quadratic complexity. +Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex) and [EnumerableWithIndex](#enumerablewithindex) interfaces. ```go package main @@ -200,9 +201,9 @@ func main() { ### Sets -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 has 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 container. -All sets implement the set interface with the following methods: +Implements [Container](#containers) interface. ```go type Set interface { @@ -220,9 +221,9 @@ type Set interface { #### HashSet -This structure implements the Set interface and is backed by a hash table (actually a Go's map). It makes no guarantees as to the iteration order of the set, since Go randomizes this iteration order on maps. +A [set](#sets) backed by a hash table (actually a Go's map). It makes no guarantees as to the iteration order of the set. -This structure offers constant time performance for the basic operations (add, remove, contains and size). +Implements [Set](#sets) interface. ```go package main @@ -247,9 +248,9 @@ func main() { #### TreeSet -This structure implements the Set interface and is backed by a red-black tree to keep the elements sorted with respect to the comparator. +A [set](#sets) backed by a [red-black tree](#redblacktree) to keep the elements ordered with respect to the [comparator](#comparator). -This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains). +Implements [Set](#sets), [IteratorWithIndex](#iteratorwithindex) and [EnumerableWithIndex](#enumerablewithindex) interfaces. ```go package main @@ -274,9 +275,10 @@ func main() { ### Stacks -The stack interface represents a last-in-first-out (LIFO) collection of objects. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to check whether the stack is empty and the size (number of elements). +A stack that represents a last-in-first-out (LIFO) data structure. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack. + +Implements [Container](#containers) interface. -All stacks implement the stack interface with the following methods: ```go type Stack interface { Push(value interface{}) @@ -291,11 +293,11 @@ type Stack interface { } ``` -#####LinkedListStack +#### LinkedListStack -This stack structure is based on a linked list, i.e. each previous element has a point to the next. +A [stack](#stacks) based on a [linked list](#singlylinkedlist). -All operations are guaranteed constant time performance, except _Values()_, which is as always of linear time performance. +Implements [Stack](#stacks) and [IteratorWithIndex](#iteratorwithindex) interfaces. ```go package main @@ -320,9 +322,9 @@ func main() { #### ArrayStack -This stack structure is back by ArrayList. +A [stack](#stacks) based on a [array list](#arraylist). -All operations are guaranted constant time performance. +Implements [Stack](#stacks) and [IteratorWithIndex](#iteratorwithindex) interfaces. ```go package main @@ -347,9 +349,10 @@ func main() { ### Maps -Structure that maps keys to values. A map cannot contain duplicate keys and each key can map to at most one value. +A Map is a data structure that maps keys to values. A map cannot contain duplicate keys and each key can map to at most one value. + +Implements [Container](#containers) interface. -All maps implement the map interface with the following methods: ```go type Map interface { Put(key interface{}, value interface{}) @@ -367,9 +370,9 @@ type Map interface { #### HashMap -Map structure based on hash tables, more exactly, Go's map. Keys are unordered. +A [map](#maps) based on hash tables. Keys are unordered. -All operations are guaranted constant time performance, except _Key()_ and _Values()_ retrieval that of linear time performance. +Implements [Map](#maps) interface. ```go package main @@ -379,7 +382,7 @@ import "github.com/emirpasic/gods/maps/hashmap" func main() { m := hashmap.New() // empty m.Put(1, "x") // 1->x - m.Put(2, "b") // 2->b, 1->x (random order) + m.Put(2, "b") // 2->b, 1->x (random order) m.Put(1, "a") // 2->b, 1->a (random order) _, _ = m.Get(2) // b, true _, _ = m.Get(3) // nil, false @@ -394,11 +397,9 @@ func main() { #### TreeMap -Map structure based on our red-black tree implementation. Keys are ordered with respect to the passed comparator. +A [map](#maps) based on [red-black tree](#redblacktree). Keys are ordered ordered with respect to the [comparator](#comparator). -_Put()_, _Get()_ and _Remove()_ are guaranteed log(n) time performance. - -_Key()_ and _Values()_ methods return keys and values respectively in order of the keys. These meethods are quaranteed linear time performance. +Implements [Map](#maps), [IteratorWithKey](#iteratorwithkey) and [EnumerableWithKey](#enumerablewithkey) interfaces. ```go package main @@ -429,7 +430,8 @@ func main() { A tree is a widely used data data structure that simulates a hierarchical tree structure, with a root value and subtrees of children, represented as a set of linked nodes; thus no cyclic links. -All trees implement the tree interface with the following methods: +Implements [Container](#containers) interface. + ```go type Tree interface { containers.Container @@ -442,9 +444,11 @@ type Tree interface { #### 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](#trees) 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. + +The balancing of the tree is not perfect but it is good enough to allow it to guarantee searching in O(log n) time, where n is the total number of elements in the tree. The insertion and deletion operations, along with the tree rearrangement and recoloring, are also performed in O(log n) time. [Wikipedia](http://en.wikipedia.org/wiki/Red%E2%80%93black_tree) -The balancing of the tree is not perfect but it is good enough to allow it to guarantee searching in O(log n) time, where n is the total number of elements in the tree. The insertion and deletion operations, along with the tree rearrangement and recoloring, are also performed in O(log n) time.[Wikipedia](http://en.wikipedia.org/wiki/Red%E2%80%93black_tree) +Implements [Tree](#trees) and [IteratorWithKey](#iteratorwithkey) interfaces.