structures

package
v0.0.0-...-296ad55 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrVertexNotFound      = errors.New("vertex not found")
	ErrEdgeNotFound        = errors.New("edge not found")
	ErrVertexAlreadyExists = errors.New("vertex already exists")
	ErrFindingShortestPath = errors.New("can not find shortest path")
)
View Source
var (
	ErrEmptyDoubleLinkedList             = errors.New("empty list")
	ErrIndexOutOfRangeInDoubleLinkedList = errors.New("index out of range")
)
View Source
var (
	ErrEmptySimpleLinkedList             = errors.New("empty list")
	ErrIndexOutOfRangeInSimpleLinkedList = errors.New("index out of range")
)
View Source
var (
	ErrEmptyTree        = errors.New("empty tree")
	ErrInputNodeNil     = errors.New("input node is nil")
	ErrCannotDeleteRoot = errors.New("cannot delete the root node without its descendants")
	ErrNodeNotFound     = errors.New("node not found")
)
View Source
var (
	ErrEmptyQueue = errors.New("empty queue")
)

ErrEmptyQueue tolds the queue is empty

View Source
var (
	ErrEmptyStack = errors.New("empty stack")
)

ErrEmptyStack tolds the stack is empty

View Source
var (
	ErrHashMapKeyNotFound = errors.New("hash map key not found")
)
View Source
var (
	ErrPriorityQueueEmpty = errors.New("priority queue is empty")
)
View Source
var (
	ErrorSearchBinaryTreeValueNotFound = errors.New("value not found")
)

Functions

func NumericMaxValue

func NumericMaxValue[W Numeric]() W

NumericMaxValue returns the maximum possible value for a given numeric type W.

func NumericZeroValue

func NumericZeroValue[W Numeric]() W

NumericZeroValue returns the zero value for the numeric type W.

Types

type BasicTree

type BasicTree[T comparable] interface {
	Sizer[T]
	PushAt(in *TreeNode[T], val T) (*TreeNode[T], error)
	DeleteWithDescendants(node *TreeNode[T]) error
	DeleteWithoutDescendants(node *TreeNode[T]) error
	Root() *TreeNode[T]
	Leaves() []*TreeNode[T]
	BreadFirstSearch(value T) bool
	DepthFirstSearch(value T) bool
}

BasicTree define the basic operations of a tree

func NewTree

func NewTree[T comparable](value T) BasicTree[T]

NewTree creates a new tree with a root node

type DoubleLinkedLister

type DoubleLinkedLister[T comparable] interface {
	Sizer[T]
	Finder[T]
	PushAt(index int, value T) error
	PopAt(index int) (T, error)
	First() (T, error)
	Last() (T, error)
	GetAt(index int) (T, error)
}

DoubleLinkedLister define the basic operations of a double linked list

func NewDoubleLinkedList

func NewDoubleLinkedList[T comparable]() DoubleLinkedLister[T]

NewDoubleLinkedList creates a new double linked list

type Edge

type Edge[T comparable, W any] struct {
	From   T
	To     T
	Weight W
}

Edge represents an edge in a graph with a source, destination, and weight.

type Finder

type Finder[T comparable] interface {
	Find(value T) bool
}

Finder define a way to check if an element exists or not

type Graph

type Graph[T comparable, W Numeric] interface {
	AddVertex(vertex T) error
	RemoveVertex(vertex T) error
	AddEdge(from, to T, weight W) error
	RemoveEdge(from, to T) error
	HasEdge(from, to T) bool
	Neighbors(vertex T) (map[T]W, error)
	Weight(from, to T) (W, error)
	Vertices() []T
	Edges() []Edge[T, W]
	Degree(vertex T) (int, error)
	InDegree(vertex T) (int, error)
	Transpose() Graph[T, W]
	IsDirected() bool
	ShortestPath(from, to T) ([]T, error)
}

Graph represents a generic weighted graph with advanced operations

func NewAdjacencyListGraph

func NewAdjacencyListGraph[T comparable, W Numeric](directed bool) Graph[T, W]

NewAdjacencyListGraph creates a new weighted adjacency list graph.

func NewAdjacencyMatrixGraph

func NewAdjacencyMatrixGraph[T comparable, W Numeric](directed bool) Graph[T, W]

NewAdjacencyMatrixGraph creates a new weighted adjacency matrix graph.

type HashMapper

type HashMapper[K comparable, V comparable] interface {
	Finder[V]
	Sizer[V]
	PushAt(key K, value V)
	PopAt(key K) (V, error)
	Has(key K) bool
	GetAt(key K) (V, error)
}

HashMapper define the basic operations of a hash map

func NewHashMap

func NewHashMap[K comparable, V comparable]() HashMapper[K, V]

NewHashMap creates a new emoty HashMap

type Numeric

type Numeric interface {
	constraints.Integer | constraints.Float
}

type PriorityQueuer

type PriorityQueuer[T comparable] interface {
	Sizer[T]
	Push(value T)
	Pop() (T, error)
	Top() (T, error)
}

func NewPriorityQueue

func NewPriorityQueue[T comparable](compare func(a, b T) int) PriorityQueuer[T]

NewPriorityQueue creates a new priority queue

type Queuer

type Queuer[T comparable] interface {
	Sizer[T]
	Finder[T]
	Pop() (T, error)
	Push(value T)
	Top() (T, error)
	Bottom() (T, error)
}

Queuer define the basic operations of a queue

func NewQueue

func NewQueue[T comparable]() Queuer[T]

NewQueue retrieve an empty queue

type SearchBinaryTreeNode

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

SearchBinaryTreeNode represents a node in the search binary tree

type SearchBinaryTreer

type SearchBinaryTreer[T comparable] interface {
	Finder[T]
	Sizer[T]
	Push(value T)
	Delete(value T) error
	Root() *SearchBinaryTreeNode[T]
}

func NewSearchBinaryTree

func NewSearchBinaryTree[T comparable](compare func(a, b T) int) SearchBinaryTreer[T]

NewSearchBinaryTree creates a new binary search tree

type SimpleLinkedLister

type SimpleLinkedLister[T comparable] interface {
	Sizer[T]
	Finder[T]
	PushAt(index int, value T) error
	PopAt(index int) (T, error)
	First() (T, error)
	Last() (T, error)
	GetAt(index int) (T, error)
}

SimpleLinkedLister define the basic operations of a simple linked list

func NewSimpleLinkedList

func NewSimpleLinkedList[T comparable]() SimpleLinkedLister[T]

NewSimpleLinkedList creates a new simple linked list

type Sizer

type Sizer[T comparable] interface {
	Size() int64
}

Sizer define the size of the structure

type Stacker

type Stacker[T comparable] interface {
	Sizer[T]
	Finder[T]
	Pop() (T, error)
	Push(value T)
	Top() (T, error)
	Bottom() (T, error)
}

Stacker define the basic operations of a stack

func NewStack

func NewStack[T comparable]() Stacker[T]

NewStack retrieve an empty stack

type Tree

type Tree[T comparable] struct {
	// contains filtered or unexported fields
}

func (*Tree[T]) BreadFirstSearch

func (t *Tree[T]) BreadFirstSearch(value T) bool

BreadFirstSearch find an element

func (*Tree[T]) DeleteWithDescendants

func (t *Tree[T]) DeleteWithDescendants(node *TreeNode[T]) error

DeleteWithDescendants delete a node and all its subnodes

func (*Tree[T]) DeleteWithoutDescendants

func (t *Tree[T]) DeleteWithoutDescendants(node *TreeNode[T]) error

DeleteWithoutDescendants delete a node and pass its children to its parent

func (*Tree[T]) DepthFirstSearch

func (t *Tree[T]) DepthFirstSearch(value T) bool

DepthFirstSearch find an element

func (*Tree[T]) Leaves

func (t *Tree[T]) Leaves() []*TreeNode[T]

Leaves returns all leaf nodes in the tree

func (*Tree[T]) PushAt

func (t *Tree[T]) PushAt(in *TreeNode[T], val T) (*TreeNode[T], error)

PushAt insert a new node with value val as a child of in node

func (*Tree[T]) Root

func (t *Tree[T]) Root() *TreeNode[T]

Get the root node of the tree

func (*Tree[T]) Size

func (t *Tree[T]) Size() int64

Size returns the total number of nodes in the tree

type TreeNode

type TreeNode[T comparable] struct {
	// contains filtered or unexported fields
}

TreeNode is a node in a tree that stores a value and a list of children nodes

func (*TreeNode[T]) Children

func (n *TreeNode[T]) Children() []*TreeNode[T]

Children returns a slice of children nodes

func (*TreeNode[T]) Value

func (n *TreeNode[T]) Value() T

Value returns the value of the node

Jump to

Keyboard shortcuts

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