bst

package
v0.0.0-...-f1e07a9 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2021 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BST

type BST struct {
	// contains filtered or unexported fields
}

BST stores the root Node of the tree, a key comparator, and the size of the tree. Duplicates are not allowed. Comparator format is taken from https://github.com/emirpasic/gods#comparator. Either import the package https://github.com/emirpasic/gods/utils and pass a comparator from the library, or write a custom comparator using guidelines from the gods README.

func NewWith

func NewWith(comparator utils.Comparator) *BST

NewWith returns a pointer to a BST where root is nil, size is 0, and the key comparator is set to the parameter passed in. The comparator format is taken from https://github.com/emirpasic/gods#comparator. Either import the package https://github.com/emirpasic/gods/utils and pass a comparator from the library, or write a custom comparator using guidelines from the gods README.

func NewWithIntComparator

func NewWithIntComparator() *BST

NewWithIntComparator returns a pointer to a BST where root is nil, size is 0, and the key comparator is set to the IntComparator from package https://github.com/emirpasic/gods/utils. the comparator format is taken from https://github.com/emirpasic/gods#comparator.

func NewWithStringComparator

func NewWithStringComparator() *BST

NewWithStringComparator returns a pointer to a BST where root is nil, size is 0, and the key comparator is set to the StringComparator from package https://github.com/emirpasic/gods/utils. the comparator format is taken from https://github.com/emirpasic/gods#comparator.

func (*BST) Clear

func (tree *BST) Clear()

Clear sets the root node to nil and sets the size of the tree to 0.

func (*BST) Delete

func (tree *BST) Delete(key interface{}) (interface{}, error)

Delete takes a key, removes the node from the tree, and decrements the size of the tree. The function returns the key of the deleted node and an error, if there was one.

func (*BST) Insert

func (tree *BST) Insert(key, value interface{}) (interface{}, error)

Insert takes a key and a value of type interface, and inserts a new Node with that key and value. The function inserts by key; that is, the key of the new node is compared against current nodes to find the correct insertion point. The function returns the newly inserted node's key or an error, if there was one.

func (*BST) IsEmpty

func (tree *BST) IsEmpty() bool

IsEmpty returns a boolean stating whether the tree is empty or not.

func (*BST) ReturnNodeValue

func (tree *BST) ReturnNodeValue(key interface{}) (interface{}, error)

ReturnNodeValue takes a key and returns the value associated with the key or an error, if there was one.

func (*BST) Root

func (tree *BST) Root() *Node

Root returns the root of the tree, a pointer to type Node.

func (*BST) Search

func (tree *BST) Search(key interface{}) bool

Search takes a key and searches for the key in the tree. The function returns a boolean, stating whether the key was found or not.

func (*BST) Size

func (tree *BST) Size() int

Size returns the size, or number of nodes in the tree, of the tree.

func (*BST) Update

func (tree *BST) Update(key interface{}, value interface{}) (interface{}, error)

Update takes a key and a value and updates a node with the existing key with the new value. Returns the new value of the node or an error, if there was one.

type DuplicateError

type DuplicateError struct {
	Key     interface{}
	Message string
}

func NewDuplicateError

func NewDuplicateError(k interface{}) *DuplicateError

func (*DuplicateError) Error

func (e *DuplicateError) Error() string

TODO assert type before writing out error message

type NilNodeError

type NilNodeError struct {
	Key     interface{}
	Message string
}

func NewNilNodeError

func NewNilNodeError(k interface{}) *NilNodeError

func (*NilNodeError) Error

func (e *NilNodeError) Error() string

TODO assert type before writing out error message

type Node

type Node struct {
	Data *NodeData
	// contains filtered or unexported fields
}

Node stores left, right, and parent Node pointers; and NodeData, containing the key and the value the caller wishes to store.

func NewNode

func NewNode(k, v interface{}) *Node

NewNode takes in a key and a value and returns a pointer to type Node. When creating a new node, the left and right children, as well as the parent node, are set to nil.

type NodeData

type NodeData struct {
	Key   interface{}
	Value interface{}
}

NodeData stores the key and the value of the Node.

Jump to

Keyboard shortcuts

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