Documentation
¶
Index ¶
- func Clear[K cmp.Ordered, V any](t *Tree[K, V])
- func Delete[K cmp.Ordered, V any](t *Tree[K, V], key K) bool
- func InOrder[K cmp.Ordered, V any](t *Tree[K, V]) iter.Seq[Node[K, V]]
- func Insert[K cmp.Ordered, V any](t *Tree[K, V], key K, value V) bool
- func Len[K cmp.Ordered, V any](t *Tree[K, V]) int
- func Range[K cmp.Ordered, V any](t *Tree[K, V], from, to K) iter.Seq[Node[K, V]]
- func Rank[K cmp.Ordered, V any](t *Tree[K, V], key K) int
- type Node
- func Ceiling[K cmp.Ordered, V any](t *Tree[K, V], key K) (*Node[K, V], bool)
- func Floor[K cmp.Ordered, V any](t *Tree[K, V], key K) (*Node[K, V], bool)
- func Higher[K cmp.Ordered, V any](t *Tree[K, V], key K) (*Node[K, V], bool)
- func Kth[K cmp.Ordered, V any](t *Tree[K, V], k int) (*Node[K, V], bool)
- func Lower[K cmp.Ordered, V any](t *Tree[K, V], key K) (*Node[K, V], bool)
- func Max[K cmp.Ordered, V any](t *Tree[K, V]) (*Node[K, V], bool)
- func Min[K cmp.Ordered, V any](t *Tree[K, V]) (*Node[K, V], bool)
- func Predecessor[K cmp.Ordered, V any](n *Node[K, V]) (*Node[K, V], bool)
- func Search[K cmp.Ordered, V any](t *Tree[K, V], key K) (*Node[K, V], bool)
- func Successor[K cmp.Ordered, V any](n *Node[K, V]) (*Node[K, V], bool)
- type Tree
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Delete ¶
Delete removes a node with the given key from the red-black tree.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
rbts.Insert(tree, 10, "ten")
rbts.Delete(tree, 10)
fmt.Println(rbts.Len(tree))
}
Output: 0
func InOrder ¶
InOrder returns an iterator for in-order traversal of the tree.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
rbts.Insert(tree, 20, "")
rbts.Insert(tree, 10, "")
rbts.Insert(tree, 30, "")
for n := range rbts.InOrder(tree) {
fmt.Print(n.Key(), " ")
}
fmt.Println()
}
Output: 10 20 30
func Insert ¶
Insert inserts a new key-value pair into the red-black tree. Returns true if inserted, false if replaced.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
rbts.Insert(tree, 10, "ten")
rbts.Insert(tree, 5, "five")
rbts.Insert(tree, 15, "fifteen")
fmt.Println(rbts.Len(tree))
}
Output: 3
func Len ¶
Len returns the number of nodes in the tree.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
fmt.Println(rbts.Len(tree))
}
Output: 0
func Range ¶
Range returns an iterator over nodes with keys in [from, to).
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
rbts.Insert(tree, 10, "")
rbts.Insert(tree, 20, "")
rbts.Insert(tree, 30, "")
for n := range rbts.Range(tree, 15, 25) {
fmt.Print(n.Key(), " ")
}
fmt.Println()
}
Output: 20
func Rank ¶
Rank returns the number of nodes with keys less than the given key.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
rbts.Insert(tree, 10, "")
rbts.Insert(tree, 20, "")
rank := rbts.Rank(tree, 15)
fmt.Println(rank)
}
Output: 1
Types ¶
type Node ¶
Node represents a node in a red-black tree.
func Ceiling ¶ added in v0.0.3
Ceiling returns the node with the smallest key greater than or equal to the given key.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
for _, v := range []int{10, 20, 30} {
rbts.Insert(tree, v, "")
}
n, _ := rbts.Ceiling(tree, 15)
fmt.Println(n.Key())
n, _ = rbts.Ceiling(tree, 20)
fmt.Println(n.Key())
}
Output: 20 20
func Floor ¶ added in v0.0.3
Floor returns the node with the greatest key less than or equal to the given key.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
for _, v := range []int{10, 20, 30} {
rbts.Insert(tree, v, "")
}
n, _ := rbts.Floor(tree, 25)
fmt.Println(n.Key())
n, _ = rbts.Floor(tree, 20)
fmt.Println(n.Key())
}
Output: 20 20
func Higher ¶ added in v0.0.3
Higher returns the node with the smallest key greater than the given key.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
for _, v := range []int{10, 20, 30} {
rbts.Insert(tree, v, "")
}
n, _ := rbts.Higher(tree, 15)
fmt.Println(n.Key())
n, _ = rbts.Higher(tree, 20)
fmt.Println(n.Key())
}
Output: 20 30
func Kth ¶
Kth returns the node with the given 0-based rank (k).
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
rbts.Insert(tree, 10, "")
rbts.Insert(tree, 20, "")
n, _ := rbts.Kth(tree, 1)
fmt.Println(n.Key())
}
Output: 20
func Lower ¶ added in v0.0.3
Lower returns the node with the greatest key less than the given key.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
for _, v := range []int{10, 20, 30} {
rbts.Insert(tree, v, "")
}
n, _ := rbts.Lower(tree, 15)
fmt.Println(n.Key())
n, _ = rbts.Lower(tree, 20)
fmt.Println(n.Key())
}
Output: 10 10
func Max ¶
Max returns the node with the maximum key in the tree.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
rbts.Insert(tree, 20, "")
rbts.Insert(tree, 30, "")
max, ok := rbts.Max(tree)
if ok {
fmt.Println(max.Key())
}
}
Output: 30
func Min ¶
Min returns the node with the minimum key in the tree.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
rbts.Insert(tree, 20, "")
rbts.Insert(tree, 10, "")
min, ok := rbts.Min(tree)
if ok {
fmt.Println(min.Key())
}
}
Output: 10
func Predecessor ¶
Predecessor returns the in-order predecessor node of n, if any.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
rbts.Insert(tree, 20, "")
rbts.Insert(tree, 10, "")
node, _ := rbts.Search(tree, 20)
pred, ok := rbts.Predecessor(node)
if ok {
fmt.Println(pred.Key())
}
}
Output: 10
func Search ¶
Search finds a node with the given key in the red-black tree.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
rbts.Insert(tree, 20, "twenty")
node, found := rbts.Search(tree, 20)
fmt.Println(found, node.Value())
}
Output: true twenty
func Successor ¶
Successor returns the in-order successor node of n, if any.
Example ¶
package main
import (
"fmt"
rbts "github.com/byExist/redblacktrees"
)
func main() {
tree := rbts.New[int, string]()
rbts.Insert(tree, 20, "")
rbts.Insert(tree, 30, "")
node, _ := rbts.Search(tree, 20)
succ, ok := rbts.Successor(node)
if ok {
fmt.Println(succ.Key())
}
}
Output: 30