What
Brief
This is a standalone BST (Binary Search Tree) data structure from the data-structure-typed collection. If you wish to
access more data structures or advanced features, you can transition to directly installing the
complete data-structure-typed package
How
install
npm
npm i bst-typed --save
yarn
yarn add bst-typed
methods
snippet
TS
import {BST, BSTNode} from 'data-structure-typed';
const bst = new BST();
bst instanceof BST;
bst.add(11);
bst.add(3);
const idsAndValues = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
bst.addMany(idsAndValues);
bst.root instanceof BSTNode;
if (bst.root) bst.root.id;
bst.size;
bst.has(6);
const node6 = bst.get(6);
node6 && bst.getHeight(6);
node6 && bst.getDepth(6);
const nodeId10 = bst.get(10);
nodeId10?.id;
const nodeVal9 = bst.get(9, 'val');
nodeVal9?.id;
const leftMost = bst.getLeftMost();
leftMost?.id;
const node15 = bst.get(15);
const minNodeBySpecificNode = node15 && bst.getLeftMost(node15);
minNodeBySpecificNode?.id;
const subTreeSum = node15 && bst.subTreeSum(15);
subTreeSum;
const lesserSum = bst.lesserSum(10);
lesserSum;
node15 instanceof BSTNode;
const node11 = bst.get(11);
node11 instanceof BSTNode;
const dfsInorderNodes = bst.DFS('in', 'node');
dfsInorderNodes[0].id;
dfsInorderNodes[dfsInorderNodes.length - 1].id;
bst.perfectlyBalance();
bst.isPerfectlyBalanced();
const bfsNodesAfterBalanced = bst.BFS('node');
bfsNodesAfterBalanced[0].id;
bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id;
const removed11 = bst.remove(11, true);
removed11 instanceof Array;
if (removed11[0].deleted) removed11[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight(15);
const removed1 = bst.remove(1, true);
removed1 instanceof Array;
if (removed1[0].deleted) removed1[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed4 = bst.remove(4, true);
removed4 instanceof Array;
if (removed4[0].deleted) removed4[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed10 = bst.remove(10, true);
if (removed10[0].deleted) removed10[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed15 = bst.remove(15, true);
if (removed15[0].deleted) removed15[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed5 = bst.remove(5, true);
if (removed5[0].deleted) removed5[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed13 = bst.remove(13, true);
if (removed13[0].deleted) removed13[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed3 = bst.remove(3, true);
if (removed3[0].deleted) removed3[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed8 = bst.remove(8, true);
if (removed8[0].deleted) removed8[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed6 = bst.remove(6, true);
if (removed6[0].deleted) removed6[0].deleted.id;
bst.remove(6, true).length;
bst.isAVLBalanced();
bst.getHeight();
const removed7 = bst.remove(7, true);
if (removed7[0].deleted) removed7[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed9 = bst.remove(9, true);
if (removed9[0].deleted) removed9[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed14 = bst.remove(14, true);
if (removed14[0].deleted) removed14[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
bst.isAVLBalanced();
const bfsIDs = bst.BFS();
bfsIDs[0];
bfsIDs[1];
bfsIDs[2];
const bfsNodes = bst.BFS('node');
bfsNodes[0].id;
bfsNodes[1].id;
bfsNodes[2].id;
JS
const {BST, BSTNode} = require('data-structure-typed');
const bst = new BST();
bst instanceof BST;
bst.add(11);
bst.add(3);
const idsAndValues = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
bst.addMany(idsAndValues);
bst.root instanceof BSTNode;
if (bst.root) bst.root.id;
bst.size;
bst.has(6);
const node6 = bst.get(6);
node6 && bst.getHeight(6);
node6 && bst.getDepth(6);
const nodeId10 = bst.get(10);
nodeId10?.id;
const nodeVal9 = bst.get(9, 'val');
nodeVal9?.id;
const leftMost = bst.getLeftMost();
leftMost?.id;
const node15 = bst.get(15);
const minNodeBySpecificNode = node15 && bst.getLeftMost(node15);
minNodeBySpecificNode?.id;
const subTreeSum = node15 && bst.subTreeSum(15);
subTreeSum;
const lesserSum = bst.lesserSum(10);
lesserSum;
node15 instanceof BSTNode;
const node11 = bst.get(11);
node11 instanceof BSTNode;
const dfsInorderNodes = bst.DFS('in', 'node');
dfsInorderNodes[0].id;
dfsInorderNodes[dfsInorderNodes.length - 1].id;
bst.perfectlyBalance();
bst.isPerfectlyBalanced();
const bfsNodesAfterBalanced = bst.BFS('node');
bfsNodesAfterBalanced[0].id;
bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id;
const removed11 = bst.remove(11, true);
removed11 instanceof Array;
if (removed11[0].deleted) removed11[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight(15);
const removed1 = bst.remove(1, true);
removed1 instanceof Array;
if (removed1[0].deleted) removed1[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed4 = bst.remove(4, true);
removed4 instanceof Array;
if (removed4[0].deleted) removed4[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed10 = bst.remove(10, true);
if (removed10[0].deleted) removed10[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed15 = bst.remove(15, true);
if (removed15[0].deleted) removed15[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed5 = bst.remove(5, true);
if (removed5[0].deleted) removed5[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed13 = bst.remove(13, true);
if (removed13[0].deleted) removed13[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed3 = bst.remove(3, true);
if (removed3[0].deleted) removed3[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed8 = bst.remove(8, true);
if (removed8[0].deleted) removed8[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed6 = bst.remove(6, true);
if (removed6[0].deleted) removed6[0].deleted.id;
bst.remove(6, true).length;
bst.isAVLBalanced();
bst.getHeight();
const removed7 = bst.remove(7, true);
if (removed7[0].deleted) removed7[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed9 = bst.remove(9, true);
if (removed9[0].deleted) removed9[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
const removed14 = bst.remove(14, true);
if (removed14[0].deleted) removed14[0].deleted.id;
bst.isAVLBalanced();
bst.getHeight();
bst.isAVLBalanced();
const bfsIDs = bst.BFS();
bfsIDs[0];
bfsIDs[1];
bfsIDs[2];
const bfsNodes = bst.BFS('node');
bfsNodes[0].id;
bfsNodes[1].id;
bfsNodes[2].id;
API docs & Examples
API Docs
Live Examples
Examples Repository
Data Structures
Standard library data structure comparison
Data Structure Typed | C++ STL | java.util | Python collections |
---|
Heap<E> | priority_queue<T> | PriorityQueue<E> | heapq |
Deque<E> | deque<T> | ArrayDeque<E> | deque |
Queue<E> | queue<T> | Queue<E> | - |
HashMap<K, V> | unordered_map<K, V> | HashMap<K, V> | defaultdict |
DoublyLinkedList<E> | list<T> | LinkedList<E> | - |
SinglyLinkedList<E> | - | - | - |
BinaryTree<K, V> | - | - | - |
BST<K, V> | - | - | - |
RedBlackTree<E> | set<T> | TreeSet<E> | - |
RedBlackTree<K, V> | map<K, V> | TreeMap<K, V> | - |
TreeMultimap<K, V> | multimap<K, V> | - | - |
- | multiset<T> | - | - |
Trie | - | - | - |
DirectedGraph<V, E> | - | - | - |
UndirectedGraph<V, E> | - | - | - |
PriorityQueue<E> | priority_queue<T> | PriorityQueue<E> | - |
Array<E> | vector<T> | ArrayList<E> | list |
Stack<E> | stack<T> | Stack<E> | - |
Set<E> | - | HashSet<E> | set |
Map<K, V> | - | HashMap<K, V> | dict |
- | unordered_set<T> | HashSet<E> | - |
Map<K, V> | - | - | OrderedDict |
- | unordered_multiset | - | Counter |
- | - | LinkedHashSet<E> | - |
HashMap<K, V> | - | LinkedHashMap<K, V> | - |
- | unordered_multimap<K, V> | - | - |
- | bitset<N> | - | - |
Benchmark
avl-tree
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add randomly | 31.32 | 31.93 | 3.67e-4 |
10,000 add & delete randomly | 70.90 | 14.10 | 0.00 |
10,000 addMany | 40.58 | 24.64 | 4.87e-4 |
10,000 get | 27.31 | 36.62 | 2.00e-4 |
binary-tree
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000 add randomly | 12.35 | 80.99 | 7.17e-5 |
1,000 add & delete randomly | 15.98 | 62.58 | 7.98e-4 |
1,000 addMany | 10.96 | 91.27 | 0.00 |
1,000 get | 18.61 | 53.73 | 0.00 |
1,000 dfs | 164.20 | 6.09 | 0.04 |
1,000 bfs | 58.84 | 17.00 | 0.01 |
1,000 morris | 256.66 | 3.90 | 7.70e-4 |
bst
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add randomly | 31.59 | 31.66 | 2.74e-4 |
10,000 add & delete randomly | 74.56 | 13.41 | 8.32e-4 |
10,000 addMany | 29.16 | 34.30 | 0.00 |
10,000 get | 29.24 | 34.21 | 0.00 |
rb-tree
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add | 85.85 | 11.65 | 0.00 |
100,000 add & delete randomly | 211.54 | 4.73 | 0.00 |
100,000 getNode | 37.92 | 26.37 | 1.65e-4 |
comparison
test name | time taken (ms) | executions per sec | sample deviation |
---|
SRC PQ 10,000 add | 0.57 | 1748.73 | 4.96e-6 |
CJS PQ 10,000 add | 0.57 | 1746.69 | 4.91e-6 |
MJS PQ 10,000 add | 0.57 | 1749.68 | 4.43e-6 |
SRC PQ 10,000 add & pop | 3.47 | 288.14 | 6.38e-4 |
CJS PQ 10,000 add & pop | 3.39 | 295.36 | 3.90e-5 |
MJS PQ 10,000 add & pop | 3.37 | 297.17 | 3.03e-5 |
directed-graph
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000 addVertex | 0.10 | 9534.93 | 8.72e-7 |
1,000 addEdge | 6.30 | 158.67 | 0.00 |
1,000 getVertex | 0.05 | 2.16e+4 | 3.03e-7 |
1,000 getEdge | 22.31 | 44.82 | 0.00 |
tarjan | 210.90 | 4.74 | 0.01 |
tarjan all | 214.72 | 4.66 | 0.01 |
topologicalSort | 172.52 | 5.80 | 0.00 |
hash-map
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 set | 275.88 | 3.62 | 0.12 |
1,000,000 Map set | 211.66 | 4.72 | 0.01 |
1,000,000 Set add | 177.72 | 5.63 | 0.02 |
1,000,000 set & get | 317.60 | 3.15 | 0.02 |
1,000,000 Map set & get | 274.99 | 3.64 | 0.03 |
1,000,000 Set add & has | 172.23 | 5.81 | 0.02 |
1,000,000 ObjKey set & get | 929.40 | 1.08 | 0.07 |
1,000,000 Map ObjKey set & get | 310.02 | 3.23 | 0.05 |
1,000,000 Set ObjKey add & has | 283.28 | 3.53 | 0.04 |
heap
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add & pop | 5.80 | 172.35 | 8.78e-5 |
10,000 fib add & pop | 357.92 | 2.79 | 0.00 |
doubly-linked-list
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 221.57 | 4.51 | 0.03 |
1,000,000 unshift | 229.02 | 4.37 | 0.07 |
1,000,000 unshift & shift | 169.21 | 5.91 | 0.02 |
1,000,000 insertBefore | 314.48 | 3.18 | 0.07 |
singly-linked-list
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 push & pop | 212.98 | 4.70 | 0.01 |
10,000 insertBefore | 250.68 | 3.99 | 0.01 |
max-priority-queue
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 refill & poll | 8.91 | 112.29 | 2.26e-4 |
priority-queue
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add & pop | 103.59 | 9.65 | 0.00 |
deque
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 14.55 | 68.72 | 6.91e-4 |
1,000,000 push & pop | 23.40 | 42.73 | 5.94e-4 |
1,000,000 push & shift | 24.41 | 40.97 | 1.45e-4 |
1,000,000 unshift & shift | 22.56 | 44.32 | 1.30e-4 |
queue
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 39.90 | 25.07 | 0.01 |
1,000,000 push & shift | 81.79 | 12.23 | 0.00 |
stack
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 37.60 | 26.60 | 0.00 |
1,000,000 push & pop | 47.01 | 21.27 | 0.00 |
trie
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 push | 45.97 | 21.76 | 0.00 |
100,000 getWords | 66.20 | 15.11 | 0.00 |
Built-in classic algorithms
Algorithm | Function Description | Iteration Type |
---|
Binary Tree DFS | Traverse a binary tree in a depth-first manner, starting from the root node, first visiting the left subtree,
and then the right subtree, using recursion.
| Recursion + Iteration |
Binary Tree BFS | Traverse a binary tree in a breadth-first manner, starting from the root node, visiting nodes level by level
from left to right.
| Iteration |
Graph DFS | Traverse a graph in a depth-first manner, starting from a given node, exploring along one path as deeply as
possible, and backtracking to explore other paths. Used for finding connected components, paths, etc.
| Recursion + Iteration |
Binary Tree Morris | Morris traversal is an in-order traversal algorithm for binary trees with O(1) space complexity. It allows tree
traversal without additional stack or recursion.
| Iteration |
Graph BFS | Traverse a graph in a breadth-first manner, starting from a given node, first visiting nodes directly connected
to the starting node, and then expanding level by level. Used for finding shortest paths, etc.
| Recursion + Iteration |
Graph Tarjan's Algorithm | Find strongly connected components in a graph, typically implemented using depth-first search. | Recursion |
Graph Bellman-Ford Algorithm | Finding the shortest paths from a single source, can handle negative weight edges | Iteration |
Graph Dijkstra's Algorithm | Finding the shortest paths from a single source, cannot handle negative weight edges | Iteration |
Graph Floyd-Warshall Algorithm | Finding the shortest paths between all pairs of nodes | Iteration |
Graph getCycles | Find all cycles in a graph or detect the presence of cycles. | Recursion |
Graph getCutVertexes | Find cut vertices in a graph, which are nodes that, when removed, increase the number of connected components in
the graph.
| Recursion |
Graph getSCCs | Find strongly connected components in a graph, which are subgraphs where any two nodes can reach each other.
| Recursion |
Graph getBridges | Find bridges in a graph, which are edges that, when removed, increase the number of connected components in the
graph.
| Recursion |
Graph topologicalSort | Perform topological sorting on a directed acyclic graph (DAG) to find a linear order of nodes such that all
directed edges go from earlier nodes to later nodes.
| Recursion |
Software Engineering Design Standards
Principle | Description |
---|
Practicality | Follows ES6 and ESNext standards, offering unified and considerate optional parameters, and simplifies method names. |
Extensibility | Adheres to OOP (Object-Oriented Programming) principles, allowing inheritance for all data structures. |
Modularization | Includes data structure modularization and independent NPM packages. |
Efficiency | All methods provide time and space complexity, comparable to native JS performance. |
Maintainability | Follows open-source community development standards, complete documentation, continuous integration, and adheres to TDD (Test-Driven Development) patterns. |
Testability | Automated and customized unit testing, performance testing, and integration testing. |
Portability | Plans for porting to Java, Python, and C++, currently achieved to 80%. |
Reusability | Fully decoupled, minimized side effects, and adheres to OOP. |
Security | Carefully designed security for member variables and methods. Read-write separation. Data structure software does not need to consider other security aspects. |
Scalability | Data structure software does not involve load issues. |