Documentation ¶
Index ¶
- Variables
- type IntKey
- type Key
- type Node
- type PostOrderCommitTraverser
- type Traverser
- type Tree
- func (t *Tree[K, V]) Add(key K, value V) error
- func (t *Tree[K, V]) Clone() *Tree[K, V]
- func (t *Tree[K, V]) Commit() error
- func (t *Tree[K, V]) Delete(key K) error
- func (t *Tree[K, V]) Get(key K) (v V, err error)
- func (t *Tree[K, V]) IsClean() bool
- func (t *Tree[K, V]) Root() *Node[K, V]
- func (t *Tree[K, V]) String() string
- func (t *Tree[K, V]) Traverse(traverser Traverser[K, V]) error
- func (t *Tree[K, V]) Update(key K, value V) error
- type Value
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("not found")
Functions ¶
This section is empty.
Types ¶
type Key ¶
type Key[K any] interface { // Compare returns a negative integer, zero, or a positive integer as this Key is less than, // equal to, or greater than the specified key k. Compare(k K) int }
Key represents the type of the key and is used to insert, update, search, and delete values from the AVL Tree.
type Node ¶
type Node[K Key[K], V Value[V]] struct {
// contains filtered or unexported fields
}
Node represents a single value in the AVL Tree, with a key of type K and a value of type V. If the node is a leaf, its left and right fields are nil. If the node is an inner node, it has at least one non-nil left or right field.
The depth of a leaf node is 1. The depth of an inner node is equal to the maximum depth of its children plus 1. For example, if the left child has a depth of 3 and the right child has a depth of 2, the depth of the inner node would be max(3, 2) + 1 = 3 + 1 = 4. Depth value is used to rebalance the AVL Tree.
To enable destructive updates, a node in an AVL tree has a "clean" field. Whenever a new node is added or an existing node is changed (including rotations), a copy of the node is made with the clean field set to false (see Tree.Clone function for more information).
func NewBalancedNode ¶
type PostOrderCommitTraverser ¶
type PostOrderCommitTraverser[K Key[K], V Value[V]] struct{}
PostOrderCommitTraverser is a Traverser that visits all non-clean nodes and sets the clean value to true.
type Tree ¶
type Tree[K Key[K], V Value[V]] struct {
// contains filtered or unexported fields
}
Tree represents an AVL tree.
This implementation is not safe for concurrent use by multiple goroutines. If multiple goroutines access a tree concurrently, and at least one of them modifies the tree, it must be synchronized externally.
Clone method may be used to make a copy of the tree (lazily). The original tree and the cloned tree can be used by different goroutines.
Tree is indexed according to the function Key.Compare result. Node value V can be any struct that implements Value interface.
func New ¶
func New[K Key[K], V Value[V]]() *Tree[K, V]
New returns an empty AVL tree. - K is the type of the node's key (e.g. IntKey) - V is the type of the node's data (e.g. any kind of value that implements Value interface)
func NewWithTraverser ¶
func NewWithTraverser[K Key[K], V Value[V]](traverser Traverser[K, V]) *Tree[K, V]
NewWithTraverser creates a new AVL tree with a custom Traverser.
func NewWithTraverserAndRoot ¶
func NewWithTraverserAndRoot[K Key[K], V Value[V]](traverser Traverser[K, V], root *Node[K, V]) *Tree[K, V]
NewWithTraverserAndRoot creates a new AVL tree with a custom Traverser and a balanced root node.
func (*Tree[K, V]) Add ¶
Add inserts the given value to the tree. The value is first inserted into the tree in the appropriate position as determined by the rules of a binary search tree. If the insertion causes the tree to become unbalanced, one or more tree rotations are performed to restore balance to the tree.
If a value with given key exists, returns an error.
This method should NOT be called concurrently!
func (*Tree[K, V]) Clone ¶
Clone clones the AVL tree, lazily.
This method should NOT be called concurrently!
The original tree and the cloned tree can be used by different goroutines. Writes to both old tree and cloned tree use copy-on-write logic, creating new nodes whenever one of nodes would have been modified. Read operations should have no performance degradation. Write operations will initially experience minor slow-downs caused by additional allocs and copies due to the aforementioned copy-on-write logic, but should converge to the original performance characteristics of the original tree.
func (*Tree[K, V]) Delete ¶
Delete removes a node with the given key from the AVL Tree. If no such value exists, returns an error.
This method should NOT be called concurrently!
func (*Tree[K, V]) Get ¶
Get looks for the value with given key in the AVL tree, returning it. Returns nil if unable to find that value.
func (*Tree[K, V]) IsClean ¶
IsClean returns true if t does not contain uncommitted changes, false otherwise.
func (*Tree[K, V]) String ¶
String returns a string of the AVL Tree. Should not be used to print out large trees.