Documentation
¶
Index ¶
- type Ord
- type Tree
- func (root *Tree[V]) Delete(val V) *Tree[V]
- func (root *Tree[V]) Enumerate(start int, seq iter.Seq[V]) iter.Seq2[int, V]
- func (root *Tree[V]) Inorder() iter.Seq[V]
- func (root *Tree[V]) Insert(val V) *Tree[V]
- func (root *Tree[V]) Levelorder() iter.Seq[V]
- func (root *Tree[V]) Postorder() iter.Seq[V]
- func (root *Tree[V]) Preorder() iter.Seq[V]
- func (root *Tree[V]) Search(val V) bool
- func (root *Tree[V]) String() string
- func (root *Tree[V]) StringSeq(seq iter.Seq[V]) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Ord ¶
type Ord constraints.Ordered
type Tree ¶
Tree is an AVL tree implementation
func NewBst ¶
NewBst creates a new AVL tree. If vals are provided, they are added to the tree, and the tree is initialized.
Example ¶
package main import ( "fmt" bt "github.com/elordeiro/go/container/bst" ) func main() { b := bt.NewBst(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 root
Example ¶
package main import ( "fmt" bt "github.com/elordeiro/go/container/bst" ) func main() { b := bt.NewBst(3, 2, 1, 4, 5) b.Delete(3) fmt.Println(b) }
Output: /\[1 2 4 5]
func (*Tree[V]) Enumerate ¶
Enumerate returns an iter.Seq2[int, V] that traverses the tree in the given order
Example ¶
package main import ( "fmt" bt "github.com/elordeiro/go/container/bst" ) func main() { b := bt.NewBst(3, 2, 1, 4, 5) for i, v := range b.Enumerate(0, b.Inorder()) { fmt.Println(i, v) } }
Output: 0 1 1 2 2 3 3 4 4 5
func (*Tree[V]) Inorder ¶
Inorder returns an iter.Seq[V] that traverses the tree in inorder
Example ¶
package main import ( "fmt" bt "github.com/elordeiro/go/container/bst" ) func main() { b := bt.NewBst(3, 2, 1, 4, 5) fmt.Print(b.StringSeq(b.Inorder())) }
Output: /\[1 2 3 4 5]
func (*Tree[V]) Insert ¶
Insert inserts a value into the AVL tree and returns the new root
Example ¶
package main import ( "fmt" bt "github.com/elordeiro/go/container/bst" ) func main() { b := bt.NewBst(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" bt "github.com/elordeiro/go/container/bst" ) func main() { b := bt.NewBst(3, 2, 4, 1, 5) fmt.Print(b.StringSeq(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" bt "github.com/elordeiro/go/container/bst" ) func main() { b := bt.NewBst(3, 2, 4, 1, 5) fmt.Print(b.StringSeq(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" bt "github.com/elordeiro/go/container/bst" ) func main() { b := bt.NewBst(3, 2, 4, 1, 5) fmt.Print(b.StringSeq(b.Preorder())) }
Output: /\[3 2 1 4 5]
func (*Tree[V]) Search ¶
Search searches for a value in the AVL tree and returns true if it is found
Example ¶
package main import ( "fmt" bt "github.com/elordeiro/go/container/bst" ) func main() { b := bt.NewBst(3, 2, 1, 4, 5) fmt.Println(b.Search(3)) }
Output: true
func (*Tree[V]) String ¶
String returns a string representation of the tree in inorder
Example ¶
package main import ( "fmt" bt "github.com/elordeiro/go/container/bst" ) func main() { b := bt.NewBst(3, 2, 1, 4, 5) fmt.Println(b) }
Output: /\[1 2 3 4 5]
func (*Tree[V]) StringSeq ¶
StringOrder returns a string representation of the tree in the specified order (preorder, inorder, postorder, levelorder)
Example ¶
package main import ( "fmt" bt "github.com/elordeiro/go/container/bst" ) func main() { b := bt.NewBst(3, 2, 1, 4, 5) fmt.Println(b.StringSeq(b.Inorder())) }
Output: /\[1 2 3 4 5]