bstreelib

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package bstreelib: Basic Binary Search Tree Stuff

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BinarySearchTree

type BinarySearchTree[T BinarySearchTreeElement] struct {
	// contains filtered or unexported fields
}

BinarySearchTree struct will hold the core functionality of this library

func ConstructBalancedTree

func ConstructBalancedTree[T BinarySearchTreeElement](values ...T) (*BinarySearchTree[T], error)

ConstructBalancedTree is a helper function to insert all the given values into a binary search tree, in a manner which creates a balanced tree

func ConstructFromValues

func ConstructFromValues[T BinarySearchTreeElement](values ...T) (*BinarySearchTree[T], error)

ConstructFromValues is a helper function to insert all the given values (in the order that they are provided) into a binary search tree

func (*BinarySearchTree[T]) BalanceTree

func (bst *BinarySearchTree[T]) BalanceTree() error

BalanceTree will re-arrange the binary tree into its most balanced form

func (*BinarySearchTree[T]) ConstructOrderedSlice

func (bst *BinarySearchTree[T]) ConstructOrderedSlice() ([]T, error)

ConstructOrderedSlice collects all the elements in the binary search tree in an ordered manner, and returns them in a slice

func (*BinarySearchTree[T]) Count

func (bst *BinarySearchTree[T]) Count() (int, error)

Count returns the number of elements in a binary search tree

func (*BinarySearchTree[T]) Insert

func (bst *BinarySearchTree[T]) Insert(value T) error

Insert will add a new value to the binary search tree at the correct position

func (*BinarySearchTree[T]) IsEmpty

func (bst *BinarySearchTree[T]) IsEmpty() bool

IsEmpty tells you if the binary search tree is empty

func (*BinarySearchTree[T]) IsNil

func (bst *BinarySearchTree[T]) IsNil() bool

IsNil tells you if the pointer to the binary search tree is nil

func (*BinarySearchTree[T]) Root

func (bst *BinarySearchTree[T]) Root() *Node[T]

Root returns a pointer to the root of a binary search tree

func (*BinarySearchTree[T]) Search

func (bst *BinarySearchTree[T]) Search(val T) (bool, error)

Search looks for a given value the binary search tree, and tell you whether that value is present in the tree or not

func (*BinarySearchTree[T]) TraverseBFS

func (bst *BinarySearchTree[T]) TraverseBFS() (string, error)

TraverseBFS returns a string that represents the traversal order of nodes using Breadth First Search

func (*BinarySearchTree[T]) TraverseDFSInOrder

func (bst *BinarySearchTree[T]) TraverseDFSInOrder() (string, error)

TraverseDFSInOrder returns a string that represents the traversal order of nodes using Depth First Search In an in-order manner (visit a node's left subtree, then the node itself, followed by its right subtree) This method uses recursion

func (*BinarySearchTree[T]) TraverseDFSPostOrder

func (bst *BinarySearchTree[T]) TraverseDFSPostOrder() (string, error)

func (*BinarySearchTree[T]) TraverseDFSPreOrder

func (bst *BinarySearchTree[T]) TraverseDFSPreOrder() (string, error)

type BinarySearchTreeElement

type BinarySearchTreeElement interface {
	cmp.Ordered
	fmt.Stringer
}

BinarySearchTreeElement is a custom interface that combines the constraints of the Ordered and Stringer interfaces

type Node

type Node[T BinarySearchTreeElement] struct {
	// contains filtered or unexported fields
}

Node is the basic unit of the binary search tree, and contains data which can be anything that implements the BinarySearchTreeElement interface

func (*Node[T]) LeftChild

func (node *Node[T]) LeftChild() (*Node[T], error)

LeftChild is used to get a pointer to the left child of a given node

func (*Node[T]) Parent

func (node *Node[T]) Parent() (*Node[T], error)

Parent is used to get a pointer to the parent node of a given node

func (*Node[T]) RightChild

func (node *Node[T]) RightChild() (*Node[T], error)

RightChild is used to get a pointer to the right child of a given node

func (*Node[T]) String

func (node *Node[T]) String() string

Node's implementation of the fmt.Stringer interface

Jump to

Keyboard shortcuts

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