Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/al-ce/go-avltree
Golang AVL tree package
https://github.com/al-ce/go-avltree
Last synced: 2 days ago
JSON representation
Golang AVL tree package
- Host: GitHub
- URL: https://github.com/al-ce/go-avltree
- Owner: al-ce
- License: gpl-3.0
- Created: 2024-12-05T01:24:25.000Z (22 days ago)
- Default Branch: main
- Last Pushed: 2024-12-05T05:13:25.000Z (22 days ago)
- Last Synced: 2024-12-24T00:56:02.521Z (3 days ago)
- Language: Go
- Size: 33.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-avltree
A golang [AVL tree](https://en.wikipedia.org/wiki/AVL_tree) implementation.
Accepts `Ordered` (integer, float, and string) types as defined by the [constraints](https://pkg.go.dev/golang.org/x/exp/constraints) package.## Example
```go
package mainimport (
"fmt"avl "github.com/al-ce/go-avltree"
)func main() {
tree := avl.NewAvlTree[int]()for i := 1; i <= 10; i++ {
tree.Add(i)
}tree.Remove(5)
tree.Contains(5) // false
tree.IsEmpty() // falseminVal, _ := tree.GetMin() // returns error if tree is empty
fmt.Println(minVal) // 1fmt.Println(tree.Size()) // 9
fmt.Println("Traverse (get slice)")
inorder := tree.InOrderTraverse() // returns []T
for _, val := range inorder {
fmt.Printf("%v ", val) // [1 2 3 4 6 7 8 9 10]
}
fmt.Println()// iter.Next() returns (val T, index int)
// When index == -1, the iterator has reached the end of the tree
iter := tree.NewIterator()fmt.Println("Iterator:")
for {
val, index := iter.Next()
if index == -1 {
break
}
fmt.Printf("%v ", val) // [1 2 3 4 6 7 8 9 10]
}
fmt.Println()tree.Clear()
tree.IsEmpty() // true// String example
stringTree := avl.NewAvlTree[string]()
p := []string{"tahini", "za'atar", "chickpeas"}
for _, v := range p {
stringTree.Add(v)
}
ordered := stringTree.InOrderTraverse()
fmt.Println(ordered) // [chickpeas tahini za'atar]
}
```