avl

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2024 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package avl provides an AVL tree implementation

Index

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

func (t *Tree[V]) Delete(val V) *Tree[V]

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]) Height

func (t *Tree[V]) Height() int

Height returns the height of the tree node.

func (*Tree[V]) Inorder

func (t *Tree[V]) Inorder() iter.Seq[V]

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

func (t *Tree[V]) Insert(val V) *Tree[V]

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]) Left

func (t *Tree[V]) Left() *Tree[V]

Left returns the left child of the tree node.

func (*Tree[V]) Levelorder

func (t *Tree[V]) Levelorder() iter.Seq[V]

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]) Max

func (t *Tree[V]) Max() *Tree[V]

Max returns the node with the maximum value in the tree.

func (*Tree[V]) Min

func (t *Tree[V]) Min() *Tree[V]

Min returns the node with the minimun value in the tree.

func (*Tree[V]) Postorder

func (t *Tree[V]) Postorder() iter.Seq[V]

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

func (t *Tree[V]) Preorder() iter.Seq[V]

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]) Right

func (t *Tree[V]) Right() *Tree[V]

Right returns the right child of the tree node.

func (*Tree[V]) Search

func (t *Tree[V]) Search(val V) *Tree[V]

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

func (t Tree[V]) String() 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

func (t Tree[V]) StringOrder(order func() iter.Seq[V]) string

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.

func (*Tree[V]) Value

func (t *Tree[V]) Value() V

Value returns the value of the tree node.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL