llrb

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: MIT Imports: 1 Imported by: 0

README

LLRB-Tree implementation for Go

PkgGoDev

This repository contains an implementation of a Left-Leaning Red-Black (LLRB) tree in Go. The LLRB tree is a self-balancing binary search tree that provides efficient operations for insertion, deletion, and search.

For more information and usage examples, please refer to the GoDoc documentation.

Documentation

Overview

Package llrb implements LLRB 2-3 trees, based on this paper:

A left-leaning red–black (LLRB) tree is a type of self-balancing binary search tree. It is a variant of the red–black tree and guarantees the same asymptotic complexity for operations, but is designed to be easier to implement.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompareFunc

type CompareFunc[T any] func(a, b T) int

CompareFunc is a function type that compares two values of type T and returns an integer. It should return a negative value if a < b, 0 if a == b, and a positive value if a > b.

type IterFunc

type IterFunc[T any] func(a T) bool

IterFunc is a function type that takes a value of type T and returns a boolean. It should return true to continue iteration, or false to stop iteration.

type LLRBMap

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

LLRBMap represents a left-leaning red-black tree map.

Example
package main

import (
	"fmt"

	"github.com/maolonglong/llrb"
)

func main() {
	m := llrb.NewMap[int, string]()

	m.Set(1, "apple")
	m.Set(2, "banana")
	m.Set(3, "cherry")

	v, ok := m.Get(2)
	if ok {
		fmt.Println(v)
	}

	m.Delete(3)

	m.Range(func(key int, value string) bool {
		fmt.Println(key, value)
		return true
	})
}
Output:

banana
1 apple
2 banana

func NewMap

func NewMap[K cmp.Ordered, V any]() *LLRBMap[K, V]

NewMap creates a new LLRBMap.

func (*LLRBMap[K, V]) Clear

func (m *LLRBMap[K, V]) Clear()

Clear removes all key-value pairs from the map, resulting in an empty map.

func (*LLRBMap[K, V]) Delete

func (m *LLRBMap[K, V]) Delete(key K) (V, bool)

Delete removes the key-value pair with the specified key from the map. It returns the value associated with the key and a boolean indicating if the key existed.

func (*LLRBMap[K, V]) Get

func (m *LLRBMap[K, V]) Get(key K) (V, bool)

Get retrieves the value associated with the specified key from the map. It returns the value and a boolean indicating if the key exists in the map.

func (*LLRBMap[K, V]) Has

func (m *LLRBMap[K, V]) Has(key K) bool

Has checks if the map contains the specified key. It returns true if the key exists in the map, false otherwise.

func (*LLRBMap[K, V]) Len

func (m *LLRBMap[K, V]) Len() int

Len returns the number of key-value pairs in the map.

func (*LLRBMap[K, V]) Range

func (m *LLRBMap[K, V]) Range(iter func(key K, value V) bool)

Range iterates over the key-value pairs in the map in ascending order of the keys. The provided callback function is called for each key-value pair. Iteration stops if the callback function returns false.

func (*LLRBMap[K, V]) Set

func (m *LLRBMap[K, V]) Set(key K, value V) (V, bool)

Set inserts or replaces a key-value pair in the map. It returns the previous value associated with the key and a boolean indicating if the key existed.

type LLRBSet

type LLRBSet[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

LLRBSet represents a set data structure implemented using a Left-Leaning Red-Black Tree.

Example
package main

import (
	"fmt"

	"github.com/maolonglong/llrb"
)

func main() {
	s := llrb.NewSet[int]()

	s.Insert(1)
	s.Insert(2)
	s.Insert(2)
	s.Insert(3)

	fmt.Println(s.Len())

	fmt.Println(s.Has(2))

	s.Delete(2)
	fmt.Println(s.Len())

	s.Range(func(x int) bool {
		fmt.Println(x)
		return true
	})
}
Output:

3
true
2
1
3

func NewSet

func NewSet[T cmp.Ordered]() *LLRBSet[T]

NewSet creates a new LLRBSet.

func (*LLRBSet[T]) Clear

func (s *LLRBSet[T]) Clear()

Clear removes all values from the set, resulting in an empty set.

func (*LLRBSet[T]) Delete

func (s *LLRBSet[T]) Delete(item T) (exist bool)

Delete removes a value from the set. It returns true if the value existed in the set, false otherwise.

func (*LLRBSet[T]) Has

func (s *LLRBSet[T]) Has(item T) bool

Has checks if the set contains the specified value. It returns true if the value exists in the set, false otherwise.

func (*LLRBSet[T]) Insert

func (s *LLRBSet[T]) Insert(item T) (exist bool)

Insert inserts a value into the set. It returns true if the value already exists in the set, false otherwise.

func (*LLRBSet[T]) Len

func (s *LLRBSet[T]) Len() int

Len returns the number of values in the set.

func (*LLRBSet[T]) Range

func (s *LLRBSet[T]) Range(iter IterFunc[T])

Range iterates over the values in the set in ascending order. The provided callback function is called for each value. Iteration stops if the callback function returns false.

type LLRBTree

type LLRBTree[T any] struct {
	// contains filtered or unexported fields
}

LLRBTree is a Left-Leaning Red-Black (LLRB) implementation of 2-3 trees.

Example
package main

import (
	"fmt"

	"github.com/maolonglong/llrb"
)

func main() {
	// Create a new LLRBTree
	tree := llrb.NewOrdered[int]()

	// Insert some items into the tree
	tree.ReplaceOrInsert(5)
	tree.ReplaceOrInsert(2)
	tree.ReplaceOrInsert(7)
	tree.ReplaceOrInsert(1)
	tree.ReplaceOrInsert(4)

	// Iterate over the tree in ascending order
	tree.Ascend(func(item int) bool {
		fmt.Println(item)
		return true
	})
}
Output:

1
2
4
5
7

func New

func New[T any](compare CompareFunc[T]) *LLRBTree[T]

New creates a new LLRB-Tree with the given compare function.

func NewOrdered

func NewOrdered[T cmp.Ordered]() *LLRBTree[T]

NewOrdered creates a new LLRB-Tree for ordered types.

func (*LLRBTree[T]) Ascend

func (t *LLRBTree[T]) Ascend(iter IterFunc[T])

Ascend calls the iterator for every value in the tree within the range [first, last], until the iterator returns false.

func (*LLRBTree[T]) AscendGreaterOrEqual

func (t *LLRBTree[T]) AscendGreaterOrEqual(pivot T, iter IterFunc[T])

AscendGreaterOrEqual calls the iterator for every value in the tree within the range [pivot, last], until the iterator returns false.

func (*LLRBTree[T]) AscendLessThan

func (t *LLRBTree[T]) AscendLessThan(pivot T, iter IterFunc[T])

AscendLessThan calls the iterator for every value in the tree within the range [first, pivot), until the iterator returns false.

func (*LLRBTree[T]) AscendRange

func (t *LLRBTree[T]) AscendRange(greaterOrEqual, lessThan T, iter IterFunc[T])

AscendRange calls the iterator for every value in the tree within the range [greaterOrEqual, lessThan), until the iterator returns false.

func (*LLRBTree[T]) Clear

func (t *LLRBTree[T]) Clear()

Clear removes all items from the LLRB-Tree.

func (*LLRBTree[T]) Delete

func (t *LLRBTree[T]) Delete(item T) (deleted T, ok bool)

Delete removes an item equal to the passed-in item from the tree, returning it. If no such item exists, it returns (nil, false).

func (*LLRBTree[T]) DeleteMax

func (t *LLRBTree[T]) DeleteMax() (deleted T, ok bool)

DeleteMax removes the largest item in the tree and returns it. If no such item exists, it returns (nil, false).

func (*LLRBTree[T]) DeleteMin

func (t *LLRBTree[T]) DeleteMin() (deleted T, ok bool)

DeleteMin removes the smallest item in the tree and returns it. If no such item exists, it returns (nil, false).

func (*LLRBTree[T]) Descend

func (t *LLRBTree[T]) Descend(iter IterFunc[T])

Descend calls the iterator for every value in the tree within the range [last, first], until the iterator returns false.

func (*LLRBTree[T]) DescendGreaterThan

func (t *LLRBTree[T]) DescendGreaterThan(pivot T, iter IterFunc[T])

DescendGreaterThan calls the iterator for every value in the tree within the range [last, pivot), until the iterator returns false.

func (*LLRBTree[T]) DescendLessOrEqual

func (t *LLRBTree[T]) DescendLessOrEqual(pivot T, iter IterFunc[T])

DescendLessOrEqual calls the iterator for every value in the tree within the range [pivot, first], until the iterator returns false.

func (*LLRBTree[T]) DescendRange

func (t *LLRBTree[T]) DescendRange(lessOrEqual, greaterThan T, iter IterFunc[T])

DescendRange calls the iterator for every value in the tree within the range [lessOrEqual, greaterThan), until the iterator returns false.

func (*LLRBTree[T]) Get

func (t *LLRBTree[T]) Get(item T) (T, bool)

Get looks for the key item in the tree, returning it. It returns (zeroValue, false) if unable to find that item.

func (*LLRBTree[T]) Has

func (t *LLRBTree[T]) Has(item T) bool

Has returns true if the given key is in the tree.

func (*LLRBTree[T]) Len

func (t *LLRBTree[T]) Len() int

Len returns the number of items currently in the tree.

func (*LLRBTree[T]) ReplaceOrInsert

func (t *LLRBTree[T]) ReplaceOrInsert(item T) (prev T, exist bool)

ReplaceOrInsert adds the given item to the tree. If an item in the tree already equals the given one, it is removed from the tree and returned, and the second return value is true. Otherwise, (zeroValue, false) is returned.

Note: nil cannot be added to the tree (undefined behavior).

Jump to

Keyboard shortcuts

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