bst

package
v0.0.0-...-7ffa7f6 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2024 License: MIT Imports: 5 Imported by: 0

README

BST

Generic binary search tree data structure and algorithms

The bst package implements a binary search tree, with the following associative-array (map) operations:

  • Get
  • Put
  • Delete
  • Size

and the following ordered-collection operations:

  • VisitInOrder
  • Min
  • Max

Trees can be either unbalanced or balanced. Balanced trees are implemented using a Red-Black tree algorithm.

This implementation does not maintain parent pointers within the nodes of the tree. As such, many operations (in particular re-balancing after insertion and deletion) must remember the path from the root to an impacted node for the duration of the operation.

Documentation

Overview

Copyright (c) 2024 Thomas Mikalsen. Subject to the MIT License

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BST

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

func NewBST

func NewBST[K cmp.Ordered, V any](balance bool) *BST[K, V]

NewBST creates a new unbalanced or balanced tree.

func (*BST[K, V]) Delete

func (t *BST[K, V]) Delete(key K) (ok bool)

Delete removes the given key, and associated value, from the tree, and returns true if the key existed and false if it did not.

func (*BST[K, V]) Get

func (t *BST[K, V]) Get(key K) (val V, found bool)

Get returns the value associated with the given key. If not found, returns the zero value for the value type and ok=false.

func (*BST[K, V]) Max

func (t *BST[K, V]) Max() (maxKey K, ok bool)

Max returns the largest key in the tree.

func (*BST[K, V]) Min

func (t *BST[K, V]) Min() (minKey K, ok bool)

Min returns the smallest key in the tree.

func (*BST[K, V]) MustGet

func (t *BST[K, V]) MustGet(key K) (val V)

func (*BST[K, V]) Put

func (t *BST[K, V]) Put(key K, val V)

Put adds the given key value pair to the tree. If the key already exists in the tree, the given value replaces the existing value.

func (*BST[K, V]) Size

func (t *BST[K, V]) Size() int

Size returns the number of key/value pairs currently in the tree.

func (*BST[K, V]) VisitInOrder

func (t *BST[K, V]) VisitInOrder(v types.Visitor[K, V])

VisitInOrder performs an in-order traversal of all key/value pairs in the tree.

Jump to

Keyboard shortcuts

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