Documentation
¶
Overview ¶
Package rbtree provides methods to work with generic red-black tree.
Index ¶
- type RBNode
- type RBTree
- func (rbt *RBTree[T]) Clone() *RBTree[T]
- func (rbt *RBTree[T]) Delete(val T) (T, bool)
- func (rbt *RBTree[T]) EqualTo(anotherRBT *RBTree[T]) bool
- func (rbt *RBTree[T]) Find(val T) (*RBNode[T], bool)
- func (rbt *RBTree[T]) Insert(val T) (*RBNode[T], bool)
- func (rbt *RBTree[T]) IsValid() bool
- func (rbt *RBTree[T]) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RBNode ¶
type RBNode[T any] struct { Val T // contains filtered or unexported fields }
RBNode is a node of a red-black tree.
type RBTree ¶
type RBTree[T any] struct { // Min is a pointer to the node with the smallest value of the tree. Min *RBNode[T] // Left is a pointer to the node with the biggest value of the tree. Max *RBNode[T] // Count is an amount of nodes in the tree. Count int // contains filtered or unexported fields }
RBTree is a red-black tree. It contains the size and pointers to the first and the last nodes. RBTree consists of red and black nodes.
func New ¶
New returns an empty red-black tree. cmp is a pointer to the function to compare user-defined types.
cmp returns the result of comparison:
- result < 0, if first value is smaller;
- result > 0, if first value is bigger;
- result == 0, if both values are equal.
For ordered primitive types, use NewOrdered.
func NewOrdered ¶
NewOrdered returns an empty red-black tree for primitive types (cmp.Ordered).
func (*RBTree[T]) Clone ¶
Clone copies the red-black tree to a new red-black tree with the same values and structure. Clone returns a new red-black tree.
func (*RBTree[T]) Delete ¶
Delete deletes a node with particular value from the red-black tree and fixes the tree if necessary. Delete returns the deleted value and true if deletion was successful. It returns an empty value and false otherwise.
func (*RBTree[T]) Find ¶
Find returns the node pointer and true if a node with particular value was found in the red-black tree.
func (*RBTree[T]) Insert ¶
Insert adds a new value to the red-black tree and fixes the tree afterwards if necessary. If the insertion was successful, the newly inserted node and true are returned. Otherwise the existent node and false are returned.