Avlbinstree
AVL self-balancing binary search trees for ES6
Description
ES6 implementation of the AVL self-balancing binary search tree data structure with TypeScript support.
Come over to Twitter to share your thoughts on the project.
Visit the contributing guidelines to learn more on how to translate this document into more languages.
Contents
Install
Yarn
yarn add avlbinstree
NPM
npm install avlbinstree
In Depth
An AVL tree is a self-balancing binary search tree data structure, whose nodes contain a unique key
, an associated value
, and point to two distinguished left
and right
sub-trees. In the tree, the heights of the two child sub-trees of any node differ by at most one. If during a mutating operation, e.g insertion, deletion, a temporary height difference of more than one arises between two child sub-trees, the balance property of the parent sub-tree, thus of the entire tree itself, is restored through the internal usage of tree rotations. These repair tools move the tree nodes only vertically
, so that the horizontal/in-order
sequence of their keys is fully preserved. 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.
Usage
Avlbinstree exposes a chainable API, that can be utilized through a simple and minimal syntax, allowing you to combine methods effectively.
Usage examples can be also found at the test
directory.
'use strict';const Tree Node = ; const tree = ;//=> Tree { root: null } tree;// => Tree { root: Node { left: null, right: null, key: 9, value: 'A' } } treeroot;//=> Node { left: null, right: null, key: 10, value: 'A' } const node = 9 'A'; treerootkey === nodekey;//=> true treerootvalue === nodevalue;//=> true treeroot;//=> Node { left: [Node], right: [Node], key: 9, value: 'A' } treerootleft;//=> Node { left: null, right: null, key: 5, value: 'B' } treerootright;//=> Node { left: null, right: null, key: 13, value: 'C' } tree;/*=> {9} * / \ * {5} {13} * / \ * {11} {15} */ treesize;//=> 5 tree;//=> Node { key: 13, value: 'C',// left: Node { left: null, right: null, key: 11, value: 'D' },// right: Node { left: null, right: null, key: 15, value: 'E' } } tree;//=> null tree;//=> true tree;//=> false treeheight;//=> 2 tree;/*=> {13} * / \ * {9} {15} * \ * {11} */ treeroot;//=> false treeroot;//=> true tree;//=> Node { left: null, right: null, key: 15, value: 'E' } tree;//=> 15 tree;//=> 'E' tree;//=> Node { left: null, right: null, key: 9, value: 'A' } tree;//=> 9 tree;//=> 'A' tree;/*=> {11} * / \ * {9} {13} */ treeroot;//=> true tree;//=> [9, 11, 13] tree;//=> ['A', 'D', 'C']
API
root
tree.- Return Type:
Node | null
Returns the root node of the tree.
If the tree is empty null
is returned.
const Tree = ; const tree = ; tree;// => Tree { root: Node { key: 10, value: 'A', left: null, right: null } }treeroot;// => Node { key: 10, value: 'A', left: null, right: null }
clear()
tree.- Return Type:
Tree
Mutates the tree by removing all residing nodes and returns it empty.
const Tree = ; const tree = ; tree;//=> Tree { root: Node { left: [Node], right: [Node], key: 3, value: 'A' } }treesize;//=> 3treeclear;//=> Tree { root: null } }treesize;//=> 0
fullNodes()
tree.- Return Type:
Array<Node>
Applies in-order traversal to the tree and stores each traversed full node (node with two non-null children) in an array. The array is returned at the end of the traversal.
const Tree = ; const tree = ; tree;tree;//=> [ // Node { left: [Node], right: [Node], key: 10, value: 'A' } // ]
height()
tree.- Return Type:
Number
Returns the maximum distance of any leaf node from the root.
If the tree is empty -1
is returned.
const Tree = ; const tree = ; tree;treeheight;// => 0tree;treeheight;//=> 3
includes(key)
tree.- Return Type:
Boolean
Determines whether the tree includes a node with a certain key
, returning true
or false
as appropriate.
key
- Type:
Number
Node key
to search for.
const Tree = ; const tree = ; tree;tree;// => truetree;// => falsetree;// => true
inOrder(fn)
tree.- Return Type:
Tree
Applies in-order traversal (depth-first traversal - LNR) to the tree and executes the provided fn
function on each traversed node without mutating the tree itself.
fn
- Type:
Function
Function to execute on each node.
const Tree = ; const tree = ; tree;tree;// => 5// 10// 15
insert(key, value)
tree.- Return Type:
Tree
Mutates the tree by inserting a new node at the appropriate location.
key
- Type:
Number
Can be any number that will correspond to the key
of the created node.
Each node has its own unique key
.
value
- Type:
Any
Can be any value that will stored in the created node.
const Tree = ; const tree = ; tree;// => Tree { root: Node { key: 10, value: 'A', left: null, right: null } }
internalNodes()
tree.- Return Type:
Array<Node>
Applies in-order traversal to the tree and stores each traversed internal node (node with at least a single non-null child) in an array. The array is returned at the end of the traversal.
const Tree = ; const tree = ; tree;tree;//=> [ // Node { left: [Node], right: [Node], key: 10, value: 'A' },// Node { left: null, right: [Node], key: 15, value: 'C' } // ]
isComplete()
tree.- Return Type:
Boolean
The method returns true
if the tree is a complete binary search tree, which implies that every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
In any other case, the method returns false
.
const Tree = ; const tree = ; tree;tree;//=> truetree;tree;//=> truetree;tree;//=> false
isEmpty()
tree.- Return Type:
Boolean
Determines whether the tree is empty, returning true
or false
as appropriate.
const Tree = ; const tree = ; tree;tree;// => false
isFull()
tree.- Return Type:
Boolean
The method returns true
if all the nodes residing in the tree are either leaf nodes or full nodes.
In any other case (node degree equal to 1) the method returns false
.
const Tree = ; const tree = ; tree;tree;//=> truetree;tree;//=> false
isPerfect()
tree.- Return Type:
Boolean
The method returns true
if all the internal nodes residing in the tree are full nodes (node degree equal to 2) and all leaf nodes are at the same height level. In any other case (node degree equal to 1 or leaf and full nodes are found on the same height level) the method returns false
.
const Tree = ; const tree = ; tree;tree;//=> truetree;tree;//=> truetree;tree;//=> false
keys()
tree.- Return Type:
Array<Number>
Applies in-order traversal to the tree and stores the key
of each traversed node in an array.
The array is returned at the end of the traversal.
const Tree = ; const tree = ; tree;tree;//=> [ 5, 10, 15 ]
leafNodes()
tree.- Return Type:
Array<Node>
Applies in-order traversal to the tree and stores each traversed leaf node (node without children) in an array. The array is returned at the end of the traversal.
const Tree = ; const tree = ; tree;tree;//=> [ // Node { left: null, right: null, key: 5, value: 'B' },// Node { left: null, right: null, key: 15, value: 'C' } // ]
levelOrder(fn)
tree.- Return Type:
Tree
Applies level-order traversal (breadth-first traversal) to the tree and executes the provided fn
function on each traversed node without mutating the tree itself.
fn
- Type:
Function
Function to execute on each node.
const Tree = ; const tree = ; tree;tree;// => 10// 5// 15
max()
tree.- Return Type:
Node | null
Returns the right-most node in the tree, thus the node corresponding to the maximum key
.
const Tree = ; const tree = ; tree;tree;// => Node { key: 25, value: 'C', left: null, right: null }
maxKey()
tree.- Return Type:
Number | null
Returns the key
of right-most node in the tree, thus the maximum key
in the tree.
const Tree = ; const tree = ; tree;tree;// => 25
maxValue()
tree.- Return Type:
Any | null
Returns the value
of right-most node in the tree, thus the value
of the node corresponding to the maximum key
.
const Tree = ; const tree = ; tree;tree;// => 'C'
min()
tree.- Return Type:
Node | null
Returns the left-most node in the tree, thus the node corresponding to the minimum key
.
const Tree = ; const tree = ; tree;tree;// => Node { key: 0, value: 'C', left: null, right: null }
minKey()
tree.- Return Type:
Number | null
Returns the key
of the left-most node in the tree, thus the minimum key
in the tree.
const Tree = ; const tree = ; tree;tree;// => 10
minValue()
tree.- Return Type:
Any | null
Returns the value
of the left-most node in the tree, thus the value
of the node corresponding to the minimum key
.
const Tree = ; const tree = ; tree;tree;// => 'A'
outOrder(fn)
tree.- Return Type:
Tree
Applies out-order traversal (depth-first traversal - RNL) to the tree and executes the provided fn
function on each traversed node without mutating the tree itself.
fn
- Type:
Function
Function to execute on each node.
const Tree = ; const tree = ; tree;tree;// => 15// 10// 5
postOrder(fn)
tree.- Return Type:
Tree
Applies post-order traversal (depth-first traversal - LRN) to the tree and executes the provided fn
function on each traversed node without mutating the tree itself.
fn
- Type:
Function
Function to execute on each node.
const Tree = ; const tree = ; tree;tree;// => 5// 15// 10
preOrder(fn)
tree.- Return Type:
Tree
Applies pre-order traversal (depth-first traversal - NLR) to the tree and executes the provided fn
function on each traversed node without mutating the tree itself.
fn
- Type:
Function
Function to execute on each node.
const Tree = ; const tree = ; tree;tree;// => 10// 5// 15
remove(key)
tree.- Return Type:
Tree
Mutates the tree by removing the node corresponding to the key
argument.
key
- Type:
Number
Can be any number that corresponds to the key
of an existing node.
const Tree = ; const tree = ; tree;tree;//=> Tree { root: null }
search(key)
tree.- Return Type:
Node | null
Determines whether the tree includes a node with a certain key
, returning the targeted node or null
as appropriate.
key
- Type:
Number
Node key
to search for.
const Tree = ; const tree = ; tree;tree;// => Node { key: 10, value: 'A', left: [Node], right: null }tree;// => nulltree;// => Node { key: 5, value: 'B', left: null, right: null }
size()
tree.- Return Type:
Number
Returns the total number of nodes residing in the tree.
const Tree = ; const tree = ; tree;treesize;// => 3
toArray()
tree.- Return Type:
Array<Node>
Applies in-order traversal to the tree and stores each traversed node in an array. The array is returned at the end of the traversal.
const Tree = ; const tree = ; tree;tree;//=> [// Node { left: null, right: null, key: 3, value: 'D' },// Node { left: [Node], right: null, key: 5, value: 'B' },// Node { left: [Node], right: [Node], key: 10, value: 'A' },// Node { left: null, right: [Node], key: 15, value: 'C' },// Node { left: null, right: null, key: 20, value: 'F' }// ]
toPairs()
tree.- Return Type:
Array<[Number, Any]>
Applies in-order traversal to the tree and for each traversed node stores in an array of size n
, where n
the size of the tree, an ordered-pair/2-tuple, where the first element is a number
corresponding to the key
of the traversed node, and the last one is a value of type any
, corresponding to the value
stored in the traversed node.
The array is returned at the end of the traversal.
const Tree = ; const tree = ; tree;tree;//=> [ [3, 'D'], [5, 'B'], [10, 'A'], [15, 'C'], [20, 'F'] ]
values()
tree.- Return Type:
Array<Any>
Applies in-order traversal to the tree and stores the value
of each traversed node in an array.
The array is returned at the end of the traversal.
const Tree = ; const tree = ; tree;tree;//=> [ 'B', 'A', 'C' ]
Also available, along with the Tree
exposed class, is the Node
class, mainly useful for testing purposes, since it can be utilized to compare tree nodes. The class has a binary constructor method, with a key
and a value
parameter, corresponding to the key and the value stored in the created instance, respectively.
key
node.- Return Type:
Number
The key
corresponding to the node instance.
const Node = ; const node = 10 'A';// => { key:10, value: 'A', left: null, right: null }nodekey;//=> 10
value
node.- Return Type:
Any
The value that the node contains.
const Node = ; const node = 10 'A';// => { key: 10, value: 'A', left: null, right: null }nodevalue;//=> 'A'nodevalue = 'B'// => { key: 10, value: 'B', left: null, right: null }
left
node.- Return Type:
Node | null
The left sub-tree that the node points to.
const Tree = ; const tree = ; treeroot;// => { key: 10, value: 'A', left: null, right: null }treerootleft;//=> nulltreeroot;// => { key: 10, value: 'A', left: { key: 5, value: 'B', left: null, right: null } , right: null }treerootleft;//=> { key: 5, value: 'B', left: null, right: null }
right
node.- Return Type:
Node | null
The right sub-tree that the node points to.
const Tree = ; const tree = ; treeroot;// => { key: 10, value: 'A', left: null, right: null }treerootright;//=> nulltreeroot;// => { key: 10, value: 'A', left: null , right: { key: 15, value: 'B', left: null, right: null } }treerootright;//=> { key: 15, value: 'B', left: null, right: null }
balanceFactor
node.- Return Type:
Number
Returns a number corresponding to the balance factor of a node, which is defined as the height difference of its two child sub-trees.
const Tree = ; const tree = ; treerootbalanceFactor;//=> 0treerootbalanceFactor;//=> 1treerootbalanceFactor;//=> -1
children
node.- Return Type:
Array<Node>
Returns an array contacting the children of the instance, where the left child, if present, is the first element of the array, and the right child, if present, is the last element of the array.
const Tree = ; const tree = ; treerootchildren;//=> []treerootchildren;// => [// { key: 5, value: 'B', left: null , right: null }, // { key: 15, value: 'C', left: null, right: null }// ]
degree
node.- Return Type:
Number
Returns the number of sub-trees that the node points to.
const Tree = ; const tree = ; treerootdegree;//=> 0treerootdegree;//=> 1treerootdegree;//=> 2
height
node.- Return Type:
Number
Returns the maximum distance of any leaf node from the node instance.
const Tree = ; const tree = ; tree;treerootheight;//=> 2treerootrightheight;//=> 1
isBalanced()
node.- Return Type:
Boolean
Determines whether a node is a balanced (has a balance factor equal to 0), returning true
or false
as appropriate.
const Tree = ; const tree = ; treeroot;//=> truetreeroot;//=> false
isFull()
node.- Return Type:
Boolean
Determines whether a node is a full node (has two non-null children), returning true
or false
as appropriate.
const Tree = ; const tree = ; treeroot;//=> falsetreeroot;//=> true
isInternal()
node.- Return Type:
Boolean
Determines whether a node is an internal node (has at least one non-null child), returning true
or false
as appropriate.
const Tree = ; const tree = ; treeroot;//=> falsetreeroot;//=> true
isLeaf()
node.- Return Type:
Boolean
Determines whether a node is a leaf node (has no children), returning true
or false
as appropriate.
const Tree = ; const tree = ; treeroot;//=> truetreeroot;//=> false
isLeftHeavy()
node.- Return Type:
Boolean
Determines whether a node is left heavy (has a balance factor greater than zero), returning true
or false
as appropriate.
const Tree = ; const tree = ; treeroot;//=> falsetreeroot;//=> truetreeroot;//=> false
isLeftPartial()
node.- Return Type:
Boolean
Determines whether a node is a left partial node (has ony one left non-null child), returning true
or false
as appropriate.
const Tree = ; const tree = ; treeroot;//=> falsetreeroot;//=> true
isPartial()
node.- Return Type:
Boolean
Determines whether a node is a partial node (has ony one non-null child), returning true
or false
as appropriate.
const Tree = ; const tree = ; treeroot;//=> falsetreeroot;//=> true
isRightHeavy()
node.- Return Type:
Boolean
Determines whether a node is right heavy (has a balance factor less than zero), returning true
or false
as appropriate.
const Tree = ; const tree = ; treeroot;//=> falsetreeroot;//=> truetreeroot;//=> false
isRightPartial()
node.- Return Type:
Boolean
Determines whether a node is a right partial node (has ony one right non-null child), returning true
or false
as appropriate.
const Tree = ; const tree = ; treeroot;//=> falsetreeroot;//=> true
leftChildHeight()
node.- Return Type:
Number
Returns the maximum distance of any leaf node from the left child of the parent node instance. If the parent node has no left child, then -1
is returned.
const Tree = ; const tree = ; treeroot;//=> -1treeroot;//=> 0
maxChildHeight()
node.- Return Type:
Number
Returns the maximum between the heights of the two child nodes of parent instance. If the parent node has no children, then -1
is returned.
const Tree = ; const tree = ; treeroot;//=> -1treeroot;//=> 0treeroot;//=> 0treeroot;//=> 1
rightChildHeight()
node.- Return Type:
Number
Returns the maximum distance of any leaf node from the right child of the parent node instance. If the parent node has no right child, then -1
is returned.
const Tree = ; const tree = ; treeroot;//=> -1treeroot;//=> 0
toPair()
node.- Return Type:
[Number, Any]
Returns an ordered-pair/2-tuple, where the first element is a number corresponding to the key
of the node, and the last one is a value, that can be of any type, corresponding to the value
stored in the node.
const Node Tree = ; const tree = ;const node = 5 'B'; node;//=> [5, 'B']treeroot;//=> [10, 'A']
Development
For more info on how to contribute to the project, please read the contributing guidelines.
- Fork the repository and clone it to your machine
- Navigate to your local fork:
cd avlbinstree
- Install the project dependencies:
npm install
oryarn install
- Lint the code and run the tests:
npm test
oryarn test
Related
- binstree - Binary search trees for ES6
- doublie - Doubly circular & linear linked lists for ES6
- mheap - Binary min & max heaps for ES6
- prioqueue - Priority queues for ES6
- singlie - Singly circular & linear linked lists for ES6
Team
- Klaus Sinani (@klaussinani)