Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Iterator ¶
Iterator represents an iterator used to iterate items in the tree.
func (*Iterator[K, V]) Key ¶
func (it *Iterator[K, V]) Key() K
Key returns the key of the current item in the iteration.
func (*Iterator[K, V]) Next ¶
Next moves the iterator forward, returning false when there are no more items to iterate. Next must be called before trying to access the key or value through the iterator
type Node ¶
type Node[K cmp.Ordered, V any] interface { // Key returns the key for the node in the tree. Key() K // Value returns the values associated with the node in the tree. Value() V }
Node interface represents an entry in the tree.
type Tree ¶
Tree is a red-black tree which is a self-balancing binary search tree.
func (*Tree[K, V]) Add ¶
func (t *Tree[K, V]) Add(key K, value V)
Add adds the key/value pair to the tree. If the key already exists in the tree, the value associated with the key is updated to the new value.
Example ¶
package main import ( "fmt" "github.com/taylorza/go-generics/pkg/container/rbtree" ) func main() { tree := rbtree.New[int, string]() tree.Add(0, "Zero") tree.Add(1, "One") tree.Add(2, "Two") tree.Add(3, "Three") tree.Add(4, "Four") tree.Add(5, "Five") tree.Add(6, "Six") tree.Add(7, "Seven") tree.Add(8, "Eight") tree.Add(9, "Nine") for i := 0; i < 10; i++ { if s, ok := tree.Search(i); ok { fmt.Printf("%v - %v\n", i, s) } } }
Output: 0 - Zero 1 - One 2 - Two 3 - Three 4 - Four 5 - Five 6 - Six 7 - Seven 8 - Eight 9 - Nine
func (*Tree[K, V]) Iter ¶
Iter returns a new instance of an Iteraator that can be used to iterate through the items in the tree.
Example ¶
package main import ( "fmt" "github.com/taylorza/go-generics/pkg/container/rbtree" ) func main() { tree := rbtree.New[int, string]() tree.Add(0, "Zero") tree.Add(1, "One") tree.Add(2, "Two") tree.Add(3, "Three") tree.Add(4, "Four") tree.Add(5, "Five") tree.Add(6, "Six") tree.Add(7, "Seven") tree.Add(8, "Eight") tree.Add(9, "Nine") it := tree.Iter() for it.Next() { fmt.Printf("%v - %v\n", it.Key(), it.Value()) } }
Output: 0 - Zero 1 - One 2 - Two 3 - Three 4 - Four 5 - Five 6 - Six 7 - Seven 8 - Eight 9 - Nine
func (*Tree[K, V]) IterChan ¶
IterChan returns a channel used to iterate through all the items in the tree.