Documentation
¶
Overview ¶
Package avl provides an AVL tree implementation
Index ¶
- type Tree
- func (t *Tree[V]) Delete(val V) *Tree[V]
- func (t *Tree[V]) Height() int
- func (t *Tree[V]) Inorder() iter.Seq[V]
- func (t *Tree[V]) Insert(val V) *Tree[V]
- func (t *Tree[V]) Left() *Tree[V]
- func (t *Tree[V]) Levelorder() iter.Seq[V]
- func (t *Tree[V]) Max() *Tree[V]
- func (t *Tree[V]) Min() *Tree[V]
- func (t *Tree[V]) Postorder() iter.Seq[V]
- func (t *Tree[V]) Preorder() iter.Seq[V]
- func (t *Tree[V]) Right() *Tree[V]
- func (t *Tree[V]) Search(val V) *Tree[V]
- func (t Tree[V]) String() string
- func (t Tree[V]) StringOrder(order func() iter.Seq[V]) string
- func (t *Tree[V]) Value() V
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Tree ¶
type Tree[V constraints.Ordered] struct { // contains filtered or unexported fields }
Tree represents a node in the AVL tree.
func New ¶
func New[V constraints.Ordered](vals ...V) *Tree[V]
New creates a new AVL tree. If values are provided, they are added to the tree, and the tree is initialized.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/avl"
)
func main() {
b := avl.New(3, 2, 1, 4, 5)
fmt.Println(b)
}
Output: ^[1 2 3 4 5]
func (*Tree[V]) Delete ¶
Delete deletes a value from the AVL tree and returns the new tree.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/avl"
)
func main() {
b := avl.New(3, 2, 1, 4, 5)
b.Delete(3)
fmt.Println(b)
}
Output: ^[1 2 4 5]
func (*Tree[V]) Inorder ¶
Inorder returns an iter.Seq[V] that traverses the tree in inorder.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/avl"
)
func main() {
b := avl.New(3, 2, 1, 4, 5)
fmt.Print(b)
}
Output: ^[1 2 3 4 5]
func (*Tree[V]) Insert ¶
Insert inserts a value into the AVL tree and returns the new tree.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/avl"
)
func main() {
b := avl.New(3, 2, 1, 4, 5)
b.Insert(0)
fmt.Println(b)
}
Output: ^[0 1 2 3 4 5]
func (*Tree[V]) Levelorder ¶
Levelorder returns an iter.Seq[V] that traverses the tree in levelorder.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/avl"
)
func main() {
b := avl.New(3, 2, 4, 1, 5)
fmt.Print(b.StringOrder(b.Levelorder))
}
Output: ^[3 2 4 1 5]
func (*Tree[V]) Postorder ¶
Postorder returns an iter.Seq[V] that traverses the tree in postorder.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/avl"
)
func main() {
b := avl.New(3, 2, 4, 1, 5)
fmt.Print(b.StringOrder(b.Postorder))
}
Output: ^[1 2 5 4 3]
func (*Tree[V]) Preorder ¶
Preorder returns an iter.Seq[V] that traverses the tree in preorder.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/avl"
)
func main() {
b := avl.New(3, 2, 4, 1, 5)
fmt.Print(b.StringOrder(b.Preorder))
}
Output: ^[3 2 1 4 5]
func (*Tree[V]) Search ¶
Search searches for a value in the AVL tree and returns the node that contains it if it is found.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/avl"
)
func main() {
b := avl.New(3, 2, 1, 4, 5)
fmt.Println(b.Search(4))
}
Output: ^[3 4 5]
func (Tree[V]) String ¶
String returns a string representation of the tree in inorder.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/containers/avl"
)
func main() {
b := avl.New(3, 2, 1, 4, 5)
fmt.Println(b)
}
Output: ^[1 2 3 4 5]
func (Tree[V]) StringOrder ¶
StringOrder returns a string representation of the tree in the specified order. The order func can be tree.Preorder, tree.Inorder, tree.Postorder, or tree.Levelorder.