tree

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

For more details check out those links below here: Wikipedia article: https://en.wikipedia.org/wiki/Binary_search_tree authors [guuzaa](https://github.com/guuzaa)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AVL

type AVL[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

func NewAVL

func NewAVL[T constraints.Ordered]() *AVL[T]

NewAVL create a novel AVL tree

func (AVL) AccessNodesByLayer

func (t AVL) AccessNodesByLayer() [][]T

AccessNodesByLayer accesses nodes layer by layer (2-D array), instead of printing the results as 1-D array.

func (*AVL[T]) Delete

func (avl *AVL[T]) Delete(key T) bool

Delete a Node from the AVL Tree

func (AVL) Depth

func (t AVL) Depth() int

Depth returns the calculated depth of a binary search tree

func (AVL) Get

func (t AVL) Get(key T) (*Node[T], bool)

Get a Node from the binary-search Tree

func (AVL) Has

func (t AVL) Has(key T) bool

Determines the tree has the node of Key

func (AVL) InOrder

func (t AVL) InOrder() []T

Traverses the tree in the following order Left --> Root --> Right

func (AVL) LevelOrder

func (t AVL) LevelOrder() []T

LevelOrder returns the level order traversal of the tree

func (AVL) Max

func (t AVL) Max() (T, bool)

Returns the Max value of the tree

func (AVL) Min

func (t AVL) Min() (T, bool)

Return the Min value of the tree

func (AVL) PostOrder

func (t AVL) PostOrder() []T

Traverses the tree in the following order Left --> Right --> Root

func (AVL) PreOrder

func (t AVL) PreOrder() []T

Traverses the tree in the following order Root --> Left --> Right

func (AVL) Print

func (t AVL) Print()

Print the tree horizontally

func (*AVL[T]) Push

func (avl *AVL[T]) Push(keys ...T)

Push a chain of Node's into the AVL Tree

type BinarySearch

type BinarySearch[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

func NewBinarySearch

func NewBinarySearch[T constraints.Ordered]() *BinarySearch[T]

NewBinarySearch create a novel Binary-Search tree

func (BinarySearch) AccessNodesByLayer

func (t BinarySearch) AccessNodesByLayer() [][]T

AccessNodesByLayer accesses nodes layer by layer (2-D array), instead of printing the results as 1-D array.

func (*BinarySearch[T]) Delete

func (t *BinarySearch[T]) Delete(val T) bool

Delete removes the node of val

func (BinarySearch) Depth

func (t BinarySearch) Depth() int

Depth returns the calculated depth of a binary search tree

func (BinarySearch) Get

func (t BinarySearch) Get(key T) (*Node[T], bool)

Get a Node from the binary-search Tree

func (BinarySearch) Has

func (t BinarySearch) Has(key T) bool

Determines the tree has the node of Key

func (BinarySearch) InOrder

func (t BinarySearch) InOrder() []T

Traverses the tree in the following order Left --> Root --> Right

func (BinarySearch) LevelOrder

func (t BinarySearch) LevelOrder() []T

LevelOrder returns the level order traversal of the tree

func (BinarySearch) Max

func (t BinarySearch) Max() (T, bool)

Returns the Max value of the tree

func (BinarySearch) Min

func (t BinarySearch) Min() (T, bool)

Return the Min value of the tree

func (BinarySearch) PostOrder

func (t BinarySearch) PostOrder() []T

Traverses the tree in the following order Left --> Right --> Root

func (BinarySearch) PreOrder

func (t BinarySearch) PreOrder() []T

Traverses the tree in the following order Root --> Left --> Right

func (BinarySearch) Print

func (t BinarySearch) Print()

Print the tree horizontally

func (*BinarySearch[T]) Push

func (t *BinarySearch[T]) Push(keys ...T)

Push a chain of Node's into the BinarySearch

type Color

type Color byte
const (
	Red Color = iota
	Black
)

type Node

type Node[T constraints.Ordered] struct {
	Key    T
	Parent *Node[T] // for Red-Black Tree
	Left   *Node[T]
	Right  *Node[T]
	Color  Color // for Red-Black Tree
	Height int   // for AVL Tree
}

Node of a binary tree

type RB

type RB[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

func NewRB

func NewRB[T constraints.Ordered]() *RB[T]

Create a new Red-Black Tree

func (RB) AccessNodesByLayer

func (t RB) AccessNodesByLayer() [][]T

AccessNodesByLayer accesses nodes layer by layer (2-D array), instead of printing the results as 1-D array.

func (*RB[T]) Delete

func (t *RB[T]) Delete(data T) bool

Delete a node of Red-Black Tree Returns false if the node does not exist, otherwise returns true.

func (RB) Depth

func (t RB) Depth() int

Depth returns the calculated depth of a binary search tree

func (RB) Get

func (t RB) Get(key T) (*Node[T], bool)

Get a Node from the binary-search Tree

func (RB) Has

func (t RB) Has(key T) bool

Determines the tree has the node of Key

func (RB) InOrder

func (t RB) InOrder() []T

Traverses the tree in the following order Left --> Root --> Right

func (RB) LevelOrder

func (t RB) LevelOrder() []T

LevelOrder returns the level order traversal of the tree

func (RB) Max

func (t RB) Max() (T, bool)

Returns the Max value of the tree

func (RB) Min

func (t RB) Min() (T, bool)

Return the Min value of the tree

func (RB) PostOrder

func (t RB) PostOrder() []T

Traverses the tree in the following order Left --> Right --> Root

func (RB) PreOrder

func (t RB) PreOrder() []T

Traverses the tree in the following order Root --> Left --> Right

func (*RB[T]) Predecessor

func (t *RB[T]) Predecessor(key T) (T, bool)

Return the Predecessor of the node of Key if there is no predecessor, return default value of type T and false otherwise return the Key of predecessor and true

func (RB) Print

func (t RB) Print()

Print the tree horizontally

func (*RB[T]) Push

func (t *RB[T]) Push(keys ...T)

Push a chain of Node's into the Red-Black Tree

func (*RB[T]) Successor

func (t *RB[T]) Successor(key T) (T, bool)

Return the Successor of the node of Key if there is no successor, return default value of type T and false otherwise return the Key of successor and true

Jump to

Keyboard shortcuts

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