Package Exports
- bst-lib
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (bst-lib) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Binary search tree
In computer science, binary search trees (BST), sometimes called ordered or sorted binary trees, are a particular type of container: data structures that store "items" (such as numbers, names etc.) in memory. By https://en.wikipedia.org/wiki/Binary_search_tree
The library was written in Typescript provides a binary tree data structure for storing or searching data.
NPM: https://www.npmjs.com/package/bst-lib
GIT: https://github.com/edwardconnect/binary-search-tree
Install
npm i bst-libExample
Define and instantiate some mock objects.
class Mock {
constructor(
public id: number,
public value: string
) { }
}
var mockA = new Mock(1, 'MockA');
var mockB = new Mock(2, 'MockB');
var mockC = new Mock(3, 'MockC');
var mockD = new Mock(4, 'MockD');
var mockE = new Mock(5, 'MockE');
Instantiate a Bst with type 'Mock'
var bst = new Bst<Mock>()
Insert mock object into bst
bst.insert(mockA.id, mockA);
bst.insert(mockC.id, mockC);
bst.insert(mockB.id, mockB);
bst.insert(mockE.id, mockE);
bst.insert(mockD.id, mockD);Print the bst
bst.print();
/* Print result
5
4
3
2
1
*/Properties
BstNode
| Name | Description |
|---|---|
| `id: string | number` |
data: T |
Store the data with generic type |
left: Bst<T>() |
The left child node of this node |
right: Bst<T>() |
The right child node of this node |
Bst
| Name | Description |
|---|---|
private root: BstNode<T> = null |
Point to a BstNode if not null |
Methods
| Name | Description |
|---|---|
isEmpty(): boolean |
Whether the node is Bst point to null |
| `isContains(id: string | number): boolean` |
| `getMaxId(): number | string |
| `getMinId(): number | string |
getMaxData() :T |
Find the data with largest id |
getMinData() :T |
Find the data with smallest id |
| `searchDataById(id: number | string): T |
print(depth: number = 0) |
Print the tree in horizontally. (root on left and leafs on right) |
| `insert(id: string | number, data: T)` |
| `remove(id: string | number)` |