Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
bst-js
Advanced tools
Changelog
v1.0.5
depth
Readme
Install from NPM:
npm i bst-js
const Node = require("bst-js");
// create a tree with the root node value of 5
const tree = new Node(5);
The above will assume that the value of each node is a number. If you would like to store other forms of data, you will need to use your own comparator so that the tree can properly be sorted:
const tree = new Node("f", (x, y) => {
if (x < y) return -1;
if (x > y) return 1;
return 0;
});
tree.insert("b").insert("p").insert("l").insert("z");
// f
// / \
// b p
// / \
// l z
Inserts a node with the given value.
const tree = new Node(9).insert(4);
// 9
// /
// 4
or insert many values at once.
const tree = new Node(5);
const newValues = [10, 1, 8, 7];
tree.insert(...newValues); // or tree.insert(10, 1, 8, 7)
// 5
// / \
// 1 10
// /
// 8
// /
// 7
Deletes a node with the given value.
const tree = new Node(5).insert(10).insert(1).insert(8).insert(7);
// 5
// / \
// 1 10
// /
// 8
// /
// 7
tree.delete(10);
// 5
// / \
// 1 8
// /
// 7
or insert many values at once.
const tree = new Node(5).insert(10, 1, 8, 7);
const oldValues = [10, 1];
tree.delete(...oldValues); // or tree.delete(10, 1)
// 5
// / \
// 1 8
// /
// 7
Returns true if the height of the left and right subtrees have a difference of 1 or less.
const tree = new Node(5);
tree.insert(10).insert(1).insert(0).insert(9).insert(53).insert(12);
tree.isBalanced();
// true
Returns the height of a given tree or subtree.
const tree = new Node(5).insert(10).insert(1).insert(8).insert(7);
tree.height();
// 3
const tree = new Node(5).insert(10, 1, 8, 7);
tree.depth(8);
// 2
Returns the total number of nodes in a tree or subtree.
const tree = new Node(5).insert(10).insert(1).insert(8).insert(7);
tree.size();
// 5
Returns true if a tree/subtree contains the passed value.
const tree = new Node(5).insert(10).insert(1).insert(8).insert(7);
tree.contains(10);
// true
Will execute a callback with each node's context bound to this
over a depthFirst PREORDER
traversal (by default). It also supports INORDER
and POSTORDER
traversals.
const tree = new Node(5).insert(10).insert(1).insert(8).insert(7);
tree.depthFirst(function() {
console.log(this.value);
});
// 5, 10, 1, 8, 7
tree.depthFirst(function() {
console.log(this.value);
}, "inorder");
// 1, 5, 7, 8, 10
Will execute a callback with each node's context bound to this
over a breadthFirst traversal.
const tree = new Node(5).insert(10).insert(1).insert(0).insert(8).insert(7);
// 5
// / \
// 1 10
// / /
// 0 8
// /
// 7
tree.breadthFirst(function() {
console.log(this.value);
});
// 5, 1, 10, 0, 8, 7
FAQs
Binary Search Tree in Javascript
The npm package bst-js receives a total of 0 weekly downloads. As such, bst-js popularity was classified as not popular.
We found that bst-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.