A javascript implementation of red-black-tree using ES6
npm link
npm install
npm i red-black-tree-js
Thanks for using it!
usage
const rbTree = ;rbTree;rbTree;rbTree;rbTree;rbTree;rbTree;rbTree; const iterator = rbTree; let result = ;while iterator result;
Reference
- https://www.cs.usfca.edu/~galles/visualization/RedBlack.html visualization
- https://en.wikipedia.org/wiki/Red–black_tree wiki
OverView
- data structure
nodeColor | value |
---|---|
BLACK | 1 |
RED | 0 |
Node | LeafNode |
---|---|
left | left(null) |
right | right(null) |
parent | parent(null) |
key | key(null) |
value | value(null) |
color | color(black) |
API
-
create RB TREE
let rbTree = new RbTree()
create a red black tree with root = null -
find
const rbTree = ; rbTree; rbTree; rbTree; const value = rbTree; value is "bar" Look up value by it's key
- iterator() return the next smallest number
rbTree; rbTree; rbTree; rbTree; const iterator = rbTree; let result = ; while iterator result;
- findNode(key)
const rbTree = ; rbTree; rbTree; rbTree; const node = rbTree; return the node object
- update(key, value)
const rbTree = ; rbTree; rbTree; rbTree now the value is "updated"
-
insert(key, value)
rbTree.insert(1, "abc")
insert a key and a value to a node -
remove(key)
rbTree.remove(1)
remove a node by its key -
print()
rbTree; rbTree; rbTree; rbTree; rbTree; 30 color: 1 ___20 color: ___40 color: ______null color: ______null color: ______null color: ______null color:
print out the current tree in a good format
- inOrderSucc(node)
const rbTree = ; rbTree; rbTree; rbTree; let next = rbTree console key : 3 value: "bar"
- toSortedArray()
const rbTree = ; rbTree; rbTree; rbTree; const array = rbTree; array is Object Object Object return a sorted array of objects that contains key and value
- toArrayPreOrder()
const rbTree = ; rbTree; rbTree; rbTree; rbTree; rbTree; const array = rbTree; array is Object Object Object Object Object return an array of objects that contains key and value the key order is 3 1 0 2 4
- toArrayPostOrder()
const rbTree = ; rbTree; rbTree; rbTree; rbTree; rbTree; const array = rbTree; array is Object Object Object Object Object the key order is 0 2 1 4 3 return an array of objects that contains key and value
- minNode()
const rbTree = ; rbTree; rbTree; rbTree; const node = rbTree; node is Object key: 1 value: "foo" return the smallest node value in the tree
- maxNode()
const rbTree = ; rbTree; rbTree; rbTree; const node = rbTree; node is Object key: 3 value: "bar" return the largest node value in the tree
The tree structure now support inserting string as keys .
For example:
letter 'a' will be treated as 1
letter 'b' will be treated as 2
letter 'b' will be treated as 3
letter 'A' will be treated as 1 as well
letter 'B' will be treated as 2 as well
letter 'C' will be treated as 3 as well
a string like "abc" will be treated as 123
a string like "Abc" will be treated as 123
a string like "dc" will be treated as 41
rbTree rbTree; rbTree rbTree; rbTree rbTree; rbTree rbTree;
- future work
Better print format
Pass all linter
and more ...