|
| 1 | +# @datastrucures-js/binary-search-tree |
| 2 | + |
| 3 | +[](https://travis-ci.org/eyas-ranjous/datatructures-js/binary-search-tree) |
| 4 | +[](https://www.npmjs.com/package/@datastructures-js/binary-search-tree) |
| 5 | +[](https://www.npmjs.com/packages/@datastructures-js/binary-search-tree) [](https://www.npmjs.com/package/@datastructures-js/binary-search-tree) |
| 6 | + |
| 7 | +node's data type: **string**, **number**. |
| 8 | + |
| 9 | +<img width="413" alt="Binary Search Tree" src="https://user-images.githubusercontent.com/6517308/35762621-74a72626-085f-11e8-8934-ef6facdd6e10.png"> |
| 10 | + |
| 11 | +## Usage |
| 12 | +```js |
| 13 | +const binarySearchTree = require('@datastructures-js/binary-search-tree'); |
| 14 | +const bst = binarySearchTree(); |
| 15 | +``` |
| 16 | + |
| 17 | +## API |
| 18 | + |
| 19 | +**.node(value, parent, left, right)** |
| 20 | + |
| 21 | +creates a bst node. |
| 22 | + |
| 23 | +* **.setValue(value)** sets the node's value. |
| 24 | +* **.getValue(value)** gets the node's value. |
| 25 | +* **.setParent(parent)** sets the parent node. |
| 26 | +* **.getParent(parent)** gets the parent node. |
| 27 | +* **.setLeft(left)** sets the node's left child. |
| 28 | +* **.getLeft()** gets the node's left child. |
| 29 | +* **.setRight(left)** sets the node's right child. |
| 30 | +* **.getRight()** gets the node's right child. |
| 31 | + |
| 32 | +```js |
| 33 | +const n = bst.node('test', null,); |
| 34 | +console.log(n.getValue()); // test |
| 35 | +console.log(n.getParent()); // null |
| 36 | +console.log(n.getLeft()); // null |
| 37 | +console.log(n.getRight()); // null |
| 38 | +``` |
| 39 | + |
| 40 | +**.insert(value)** |
| 41 | + |
| 42 | +inserts a value into the tree. |
| 43 | +```javascript |
| 44 | +bst.insert(50); |
| 45 | +bst.insert(80); |
| 46 | +bst.insert(30); |
| 47 | +bst.insert(90); |
| 48 | +bst.insert(60); |
| 49 | +bst.insert(40); |
| 50 | +bst.insert(20); |
| 51 | +``` |
| 52 | + |
| 53 | +**.root()** |
| 54 | + |
| 55 | +gets the root node |
| 56 | +```javascript |
| 57 | +console.log(bst.root().getValue()); // 90 |
| 58 | +``` |
| 59 | + |
| 60 | +**.min()** |
| 61 | + |
| 62 | +finds the min value node (most left). |
| 63 | +```javascript |
| 64 | +console.log(bst.min().getValue()); // 20 |
| 65 | +``` |
| 66 | + |
| 67 | +**.max()** |
| 68 | + |
| 69 | +finds the min value node (most right). |
| 70 | +```javascript |
| 71 | +console.log(bst.max().getValue()); // 90 |
| 72 | +``` |
| 73 | + |
| 74 | +**.count()** |
| 75 | + |
| 76 | +gets nodes count. |
| 77 | +```javascript |
| 78 | +console.log(bst.count()); // 7 |
| 79 | +``` |
| 80 | + |
| 81 | +**.find(value)** |
| 82 | + |
| 83 | +finds the value's node or returns null if not found. |
| 84 | +```javascript |
| 85 | +let n = bst.find(30); |
| 86 | +console.log(n.getValue()); // 30 |
| 87 | +console.log(n.getRight().getValue()); // 40 |
| 88 | +console.log(n.getLeft().getValue()); // 20 |
| 89 | +``` |
| 90 | + |
| 91 | +**.traverseInOrder(cb)** |
| 92 | +```js |
| 93 | +// in-order traverse (left-parent-right) |
| 94 | +bst.traverseInOrder(node => console.log(node.getValue())); |
| 95 | + |
| 96 | +// 20 |
| 97 | +// 30 |
| 98 | +// 40 |
| 99 | +// 50 |
| 100 | +// 60 |
| 101 | +// 80 |
| 102 | +// 90 |
| 103 | +``` |
| 104 | + |
| 105 | +**.traversePreOrder(cb)** |
| 106 | + |
| 107 | +```js |
| 108 | +// pre-order traverse (parent-left-right) |
| 109 | +bst.traversePreOrder(node => console.log(node.getValue())); |
| 110 | + |
| 111 | +// 50 |
| 112 | +// 30 |
| 113 | +// 20 |
| 114 | +// 40 |
| 115 | +// 80 |
| 116 | +// 60 |
| 117 | +// 90 |
| 118 | +``` |
| 119 | + |
| 120 | +**.traversePostOrder(cb)** |
| 121 | + |
| 122 | +```js |
| 123 | +// post-order traverse (left-right-parent) |
| 124 | +bst.traverse(node => console.log(node.getValue())); |
| 125 | + |
| 126 | +// 20 |
| 127 | +// 40 |
| 128 | +// 30 |
| 129 | +// 60 |
| 130 | +// 90 |
| 131 | +// 80 |
| 132 | +// 50 |
| 133 | +``` |
| 134 | + |
| 135 | +**.traverse(cb, order)** |
| 136 | + |
| 137 | +traverse the tree in the defined order and apply a callback on each node. |
| 138 | + |
| 139 | +order values: `inOrder`, `preOrder` OR `postOrder`. default is `inOrder` |
| 140 | + |
| 141 | +```js |
| 142 | +bst.traverse(node => console.log(node.getValue())); // in-order |
| 143 | + |
| 144 | +// 20 |
| 145 | +// 30 |
| 146 | +// 40 |
| 147 | +// 50 |
| 148 | +// 60 |
| 149 | +// 80 |
| 150 | +// 90 |
| 151 | + |
| 152 | +bst.traverse(node => console.log(node.getValue()), 'preOrder'); |
| 153 | + |
| 154 | +// 50 |
| 155 | +// 30 |
| 156 | +// 20 |
| 157 | +// 40 |
| 158 | +// 80 |
| 159 | +// 60 |
| 160 | +// 90 |
| 161 | +``` |
| 162 | + |
| 163 | + |
| 164 | +**.remove(value)** |
| 165 | + |
| 166 | +removes a value's node (if exists) from the tree. |
| 167 | +```javascript |
| 168 | +bst.remove(30); |
| 169 | +let n50 = bst.find(50); |
| 170 | +let n40 = bst.find(40); |
| 171 | +console.log(n50.getLeft().getValue()); // 40 |
| 172 | +console.log(n40.getLeft().getValue()); // 20 |
| 173 | +``` |
| 174 | + |
| 175 | +**.clear()** |
| 176 | + |
| 177 | +clears the tree. |
| 178 | +```javascript |
| 179 | +bst.clear(); |
| 180 | +console.log(bst.count()); // 0 |
| 181 | +``` |
| 182 | + |
| 183 | +## Build |
| 184 | +``` |
| 185 | +grunt build |
| 186 | +``` |
| 187 | + |
| 188 | +## License |
| 189 | +The MIT License. Full License is [here](https://github.com/datastructures-js/binary-search-tree/blob/master/LICENSE) |
0 commit comments