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 the node with the specified key from the AVL tree. Returns true if the key existed and was deleted.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
avlts.Insert(tree, 10, "ten")
avlts.Delete(tree, 10)
fmt.Println(avlts.Len(tree))
}
Output: 0
func InOrder ¶
InOrder returns an iterator for in-order traversal of the AVL tree.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
avlts.Insert(tree, 20, "")
avlts.Insert(tree, 10, "")
avlts.Insert(tree, 30, "")
for n := range avlts.InOrder(tree) {
fmt.Print(n.Key(), " ")
}
fmt.Println()
}
Output: 10 20 30
func Insert ¶
Insert inserts a key-value pair into the AVL tree. Returns true if the key was inserted, or false if it replaced an existing key.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
avlts.Insert(tree, 10, "ten")
avlts.Insert(tree, 5, "five")
avlts.Insert(tree, 15, "fifteen")
fmt.Println(avlts.Len(tree))
}
Output: 3
func Len ¶
Len returns the number of nodes in the AVL tree.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
fmt.Println(avlts.Len(tree))
}
Output: 0
func Range ¶
Range returns an iterator for nodes with keys in the range [from, to).
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
avlts.Insert(tree, 10, "")
avlts.Insert(tree, 20, "")
avlts.Insert(tree, 30, "")
for n := range avlts.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"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
avlts.Insert(tree, 10, "")
avlts.Insert(tree, 20, "")
rank := avlts.Rank(tree, 15)
fmt.Println(rank)
}
Output: 1
Types ¶
type Node ¶
Node represents a node in the AVL tree.
func Ceiling ¶
Ceiling returns the node with the smallest key greater than or equal to the given key. Returns the node and true if such a key exists, or nil and false otherwise.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
for _, v := range []int{10, 20, 30} {
avlts.Insert(tree, v, "")
}
n, _ := avlts.Ceiling(tree, 15)
fmt.Println(n.Key())
n, _ = avlts.Ceiling(tree, 20)
fmt.Println(n.Key())
}
Output: 20 20
func Floor ¶
Floor returns the node with the largest key less than or equal to the given key. Returns the node and true if such a key exists, or nil and false otherwise.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
for _, v := range []int{10, 20, 30} {
avlts.Insert(tree, v, "")
}
n, _ := avlts.Floor(tree, 25)
fmt.Println(n.Key())
n, _ = avlts.Floor(tree, 20)
fmt.Println(n.Key())
}
Output: 20 20
func Higher ¶
Higher returns the node with the smallest key greater than the given key. Returns the node and true if such a key exists, or nil and false otherwise.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
for _, v := range []int{10, 20, 30} {
avlts.Insert(tree, v, "")
}
n, _ := avlts.Higher(tree, 15)
fmt.Println(n.Key())
n, _ = avlts.Higher(tree, 20)
fmt.Println(n.Key())
}
Output: 20 30
func Kth ¶
Kth returns the node with the given 0-based rank. Returns the node and true if such rank exists, or nil and false otherwise.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
avlts.Insert(tree, 10, "")
avlts.Insert(tree, 20, "")
n, _ := avlts.Kth(tree, 1)
fmt.Println(n.Key())
}
Output: 20
func Lower ¶
Lower returns the node with the largest key less than the given key. Returns the node and true if such a key exists, or nil and false otherwise.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
for _, v := range []int{10, 20, 30} {
avlts.Insert(tree, v, "")
}
n, _ := avlts.Lower(tree, 15)
fmt.Println(n.Key())
n, _ = avlts.Lower(tree, 20)
fmt.Println(n.Key())
}
Output: 10 10
func Max ¶
Max returns the node with the largest key in the AVL tree. Returns the node and true if the tree is not empty, or nil and false otherwise.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
avlts.Insert(tree, 20, "")
avlts.Insert(tree, 30, "")
max, ok := avlts.Max(tree)
if ok {
fmt.Println(max.Key())
}
}
Output: 30
func Min ¶
Min returns the node with the smallest key in the AVL tree. Returns the node and true if the tree is not empty, or nil and false otherwise.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
avlts.Insert(tree, 20, "")
avlts.Insert(tree, 10, "")
min, ok := avlts.Min(tree)
if ok {
fmt.Println(min.Key())
}
}
Output: 10
func Predecessor ¶
Predecessor returns the in-order predecessor of the given node, if any.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
avlts.Insert(tree, 20, "")
avlts.Insert(tree, 10, "")
node, _ := avlts.Search(tree, 20)
pred, ok := avlts.Predecessor(node)
if ok {
fmt.Println(pred.Key())
}
}
Output: 10
func Search ¶
Search finds and returns the node with the given key in the AVL tree. Returns the node and true if found, or nil and false otherwise.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
avlts.Insert(tree, 20, "twenty")
node, found := avlts.Search(tree, 20)
fmt.Println(found, node.Value())
}
Output: true twenty
func Successor ¶
Successor returns the in-order successor of the given node, if any.
Example ¶
package main
import (
"fmt"
avlts "github.com/byExist/avltrees"
)
func main() {
tree := avlts.New[int, string]()
avlts.Insert(tree, 20, "")
avlts.Insert(tree, 30, "")
node, _ := avlts.Search(tree, 20)
succ, ok := avlts.Successor(node)
if ok {
fmt.Println(succ.Key())
}
}
Output: 30