Skip to content

Latest commit

 

History

History
390 lines (216 loc) · 7.76 KB

avltree.md

File metadata and controls

390 lines (216 loc) · 7.76 KB

dastal - v5.0.0 / AVLTree

Class: AVLTree<T>

An AVL tree is a self-balancing binary search tree (source).

It is named after inventors Georgy Adelson-Velsky and Evgenii Landis and was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property.

Lookup, insertion, and deletion all take O(log(n)) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations.

AVL trees are often compared with red–black trees as both take O(log(n)) time for the basic operations. For lookup-intensive applications, AVL trees are faster than red–black trees because they are more strictly balanced. Similar to red–black trees, AVL trees are height-balanced.

Type parameters

Name
T

Implements

Table of contents

Constructors

Accessors

Methods

Constructors

constructor

new AVLTree<T>(compareFn, elements?)

Instantiate a tree.

Type parameters

Name
T

Parameters

Name Type Description
compareFn CompareFn<T> The function to determine the order of elements.
elements? Iterable<T> A set of elements to initialize the tree with.

Defined in

src/tree/avlTree.ts:56

new AVLTree<T>(compareFn, allowDuplicates, elements?)

Instantiate a tree.

Type parameters

Name
T

Parameters

Name Type Description
compareFn CompareFn<T> The function to determine the order of elements.
allowDuplicates boolean Whether to allow duplicates
elements? Iterable<T> A set of elements to initialize the tree with.

Defined in

src/tree/avlTree.ts:63

Accessors

size

get size(): number

The number of elements in the collection.

Returns

number

Implementation of

SortedTree.size

Defined in

src/tree/avlTree.ts:180

Methods

[iterator]

[iterator](): Iterator<T, any, undefined>

Receive an iterator through the list.

Note: Unexpected behavior can occur if the collection is modified during iteration.

Returns

Iterator<T, any, undefined>

An iterator through the list

Implementation of

SortedTree.[iterator]

Defined in

src/tree/avlTree.ts:196


add

add(element): AVLTree<T>

Inserts an element into the tree.

Parameters

Name Type
element T

Returns

AVLTree<T>

Implementation of

SortedTree.add

Defined in

src/tree/avlTree.ts:88


clear

clear(): void

Removes all elements.

Returns

void

Implementation of

SortedTree.clear

Defined in

src/tree/avlTree.ts:120


comparator

comparator(): CompareFn<T>

Returns

CompareFn<T>

Implementation of

SortedTree.comparator

Defined in

src/tree/avlTree.ts:125


delete

delete(element): boolean

Delete an element from the tree.

Parameters

Name Type
element T

Returns

boolean

Implementation of

SortedTree.delete

Defined in

src/tree/avlTree.ts:129


has

has(element): boolean

Check if an element is in the tree.

Parameters

Name Type
element T

Returns

boolean

Implementation of

SortedTree.has

Defined in

src/tree/avlTree.ts:140


max

max(): undefined | T

Get the maximum element.

Returns

undefined | T

Implementation of

SortedTree.max

Defined in

src/tree/avlTree.ts:144


min

min(): undefined | T

Get the minimum element.

Returns

undefined | T

Implementation of

SortedTree.min

Defined in

src/tree/avlTree.ts:148


pop

pop(): undefined | T

Remove the maximum element.

Returns

undefined | T

Implementation of

SortedTree.pop

Defined in

src/tree/avlTree.ts:152


shift

shift(): undefined | T

Remove the minimum element.

Returns

undefined | T

Implementation of

SortedTree.shift

Defined in

src/tree/avlTree.ts:166


sorted

sorted(): Iterable<T>

Iterate through the tree in sorted order (i.e in-order traversal).

Note: Unexpected behavior can occur if the collection is modified during iteration.

Returns

Iterable<T>

Implementation of

SortedTree.sorted

Defined in

src/tree/avlTree.ts:184


update

update(curElement, newElement): boolean

Update a specific element.

Parameters

Name Type
curElement T
newElement T

Returns

boolean

Implementation of

SortedTree.update

Defined in

src/tree/avlTree.ts:202