avl

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2025 License: MIT Imports: 1 Imported by: 1

README

PkgGoDev License Go Version Tag

CI Go Report Card Maintainability Code Coverage Issues

avl

Generic AVL Tree implementation for golang

features

  • simple API
  • generic
  • range-iter support
  • zero-dependency
  • zero-alloc
  • 100% test-covered

example

package main

import (
    "fmt"

    "github.com/s0rg/avl"
)

func main() {
    tree := avl.New[int, string]()

    tree.Add(4, "four")
    tree.Add(2, "two")
    tree.Add(5, "five")
    tree.Add(1, "one")
    tree.Add(3, "three")

    tree.Del(1)

    if val, ok := tree.Get(2); ok {
        fmt.Println("value of 2:", val)
    }

    for k, v := range tree.Iter {
        fmt.Println("key", k, "value", v)
    }
}

benchmarks

cpu: AMD Ryzen 5 5500U with Radeon Graphics
BenchmarkTree/AddInc-12             6420224       185.7 ns/op        47 B/op          0 allocs/op
BenchmarkTree/AddDec-12             7155193       209.3 ns/op        47 B/op          0 allocs/op
BenchmarkTree/Get-12                22292175           49.07 ns/op        0 B/op          0 allocs/op
BenchmarkTree/Del-12                592457149           2.018 ns/op       0 B/op          0 allocs/op
PASS
ok      github.com/s0rg/avl 7.579s

license

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tree

type Tree[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

AVL Tree.

func New

func New[K cmp.Ordered, V any]() (rv *Tree[K, V])

New creates empty AVL tree.

func (*Tree[K, V]) Add

func (t *Tree[K, V]) Add(key K, value V)

Add places key and value into tree, if key exists - its value will be replaced.

func (*Tree[K, V]) Clear

func (t *Tree[K, V]) Clear()

Clear drops tree contents, by resetting root.

func (*Tree[K, V]) Del

func (t *Tree[K, V]) Del(key K)

Del removes key from tree.

func (*Tree[K, V]) Get

func (t *Tree[K, V]) Get(key K) (rv V, ok bool)

Get obtains value for specified key.

func (*Tree[K, V]) Has

func (t *Tree[K, V]) Has(key K) (ok bool)

Has checks for key existence in tree.

func (*Tree[K, V]) Iter

func (t *Tree[K, V]) Iter(cb func(K, V) bool)

Iter implements range-iter interface.

func (*Tree[K, V]) Len added in v1.1.0

func (t *Tree[K, V]) Len() (rv int)

Len returns number of elements in tree.

Jump to

Keyboard shortcuts

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