Tiny BST

Tiny Binary Search Tree

View the Project on GitHub avoidwork/tiny-bst

tiny-bst is a micro BST library for the browser or server. tiny-bst can be installed from npm & bower, and supports AMD loaders or script tags. When loaded as a script, tiny-bst is available as 'bst()'.

API

find(key)

Finds a Node in the BST

insert(key[, data])

Adds a Node to the BST

remove(key)

Removes a Node from the BST

reverse()

Returns a reverse sorted Array of the BST

show()

Returns the data property of a Node

sort()

Returns a sorted Array of the BST

Examples

Pointers

var bst   = require("tiny-bst"),
    mybst = bst();

mybst.insert(3);
mybst.insert(24);
mybst.insert(1);
mybst.sort();    // [1, 3, 24]
mybst.reverse(); // [24, 3, 1]
mybst.find(3).left.show(); // 1
mybst.remove(3);
mybst.root.show(); // 24
mybst.root.left.show(); // 1

Data

var bst   = require("tiny-bst"),
    mybst = bst();

mybst.insert(3, {abc: true});
mybst.insert(24, {abc: false});
mybst.insert(1, {abc: true});
mybst.sort();    // [1, 3, 24]
mybst.reverse(); // [24, 3, 1]
mybst.find(3).left.show(); // {abc: true}
mybst.remove(3);
mybst.root.show(); // {abc: false}
mybst.root.left.show();  // {abc: true}

License

Copyright (c) 2014 Jason Mulligan
Licensed under the BSD-3 license.