redblacktree

package
v0.0.0-...-d36ca2a Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedBlackTree

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

RedBlackTree is a struct for an Red Black Tree of any ordered type. This is a self-balancing binary tree. The difference between the heights of the left and right subtree cannot be more than one for all nodes. For more information about theory, see https://en.wikipedia.org/wiki/Red-black_tree.

func Insert

func Insert[T cmp.Ordered](tree *RedBlackTree[T], value T) *RedBlackTree[T]

Insert a value into the tree. If the tree is nil, it returns a new tree with the value. If the value is already in the tree, it returns the tree. If the value is less than the root value, it inserts the value in the left subtree. If the value is greater than the root value, it inserts the value in the right subtree. After inserting, it rebalances the tree. It returns the new root of the tree.

func NewRedBlackTree

func NewRedBlackTree[T cmp.Ordered]() *RedBlackTree[T]

NewRedBlackTree factory function that returns a new RedBlackTree

func (*RedBlackTree[T]) GetChild

func (tree *RedBlackTree[T]) GetChild(direction string) *RedBlackTree[T]

GetChild returns the child node in the direction specified

func (*RedBlackTree[T]) GetValue

func (tree *RedBlackTree[T]) GetValue() T

GetValue returns the value stored in the tree. If empty, returns the zero value of the type of tree

func (*RedBlackTree[T]) IsEmpty

func (tree *RedBlackTree[T]) IsEmpty() bool

IsEmpty checks if the tree is empty

func (*RedBlackTree[T]) String

func (tree *RedBlackTree[T]) String() string

String returns a string representation of the tree.

Jump to

Keyboard shortcuts

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