Search tree specification for JavaScript. See docs. Parent is @aureooms/js-bst.
// eslint-disable-next-line ava/use-test
import ava from 'ava' ;
import * as spec from '@aureooms/js-search-tree-spec' ;
spec.test(
ava ,
{
name: "DummySearchTree" , // Name for the implementation
empty: compare => new spec.DummySearchTree(compare) , // Return an empty search tree using `compare` to order keys
from: (compare, iterable) => spec.DummySearchTree.from(compare, iterable) , // Return a search tree using `compare` to order keys initialized with the values in iterable
} ,
{
length : true , // Do the implementations maintain a `length` property?
lengths : [0, 1, 16, 17, 31, 32, 33, 63, 64, 65] , // Tree sizes to test.
}
) ;
This package contains a specification test suite for search tree implementations such as @aureooms/js-red-black-tree, @aureooms/js-splay-tree, and @aureooms/js-avl-tree.
We choose to parameterize trees using ternary comparator functions rather that key functions (as is done in Python for instance).
Comparator = ( x , x ) -> Number
Key = ( x ) -> String
compare( a , b ) < 0 <=> key( a ) < key( b )
compare( a , b ) = 0 <=> key( a ) = key( b )
compare( a , b ) > 0 <=> key( a ) > key( b )
The following Comparator
orders instances of String
.
const compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
No surprises here:
const { from , empty } = SearchTree ;
Create an empty search tree from a comparator function.
let tree = empty( compare ) ;
Create a search tree from a comparator function and an iterable.
let tree = from( compare , 'abc' ) ;
Returns the number of elements in the tree.
if ( tree.length > 1 ) ...
Returns true
if the tree is empty, false
otherwise.
return tree.isEmpty() ? 'empty' : 'not empty' ;
Returns true
if the tree contains given element.
if (tree.has(x)) ...
Insert given element in the tree and returns optional reference.
tree.insert('abc');
Could also be called Tree#add
instead.
Update given element in the tree and returns optional reference.
tree.insert({key: 'abc', value: 0});
tree.update({key: 'abc', value: 123});
Remove first occurrence of element. Returns optional boolean indicating if an element was removed.
tree.insert('x');
tree.insert('x');
tree.removeFirst('x');
Remove last occurrence of element. Returns optional boolean indicating if an element was removed.
tree.insert('x');
tree.insert('x');
tree.removeLast('x');
Remove all occurrences of element. Returns optional boolean indicating if an element was removed.
tree.insert('x');
tree.insert('x');
tree.removeAll('x');
Alias for Tree#removeFirst
.
Remove element given reference.
let ref = tree.insert('abc');
tree.delete(ref);
Could also be called Tree#unlink
instead. Leaving Tree#delete
as an alias
for Tree#removeFirst
or Tree#removeAll
to mimic the Set
API.
Merge two trees. Merged trees are destroyed.
let a = from(compare, 'abc');
let b = from(compare, '123');
a.meld(b);
Split a tree at x
such that
const [left, key, right] = tree.split(x);
assert(compare(key, x) === 0);
assert(isBinarySearchTree({key, left, right}));
Alias for Tree#[Symbol.iterator]()
.
Alias for Tree#rangeIE(left, right) -> Iterator
.