|
| 1 | +/** |
| 2 | + * Implementation of binary search tree. |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * A node of the tree |
| 7 | + * |
| 8 | + * @public |
| 9 | + * @constructor |
| 10 | + * @param {number|string} Value of the node |
| 11 | + * @param {Node} Left subling |
| 12 | + * @param {Node} Right sibling |
| 13 | + * @param {Node} Parent of the node |
| 14 | + */ |
| 15 | +function Node(value, left, right, parent) { |
| 16 | + this.value = value; |
| 17 | + this._left = left; |
| 18 | + this._right = right; |
| 19 | + this._parent = parent; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Defines the binary tree |
| 24 | + * |
| 25 | + * @public |
| 26 | + * @constructor |
| 27 | + */ |
| 28 | +function BinaryTree() { |
| 29 | + this._root = null; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Inserts a node into the binary tree. The method's complexity is O(log n) in the average case and |
| 34 | + * O(n) in the worst case. |
| 35 | + * |
| 36 | + * @public |
| 37 | + * @param {number|string} Value |
| 38 | + * @param {[Node]} Current node |
| 39 | + */ |
| 40 | +BinaryTree.prototype.insert = function (value, current) { |
| 41 | + if (this._root === null) { |
| 42 | + this._root = new Node(value, null, null, null); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + var insertKey; |
| 47 | + current = current || this._root; |
| 48 | + if (current.value > value) |
| 49 | + insertKey = '_left'; |
| 50 | + else |
| 51 | + insertKey = '_right'; |
| 52 | + if (!current[insertKey]) |
| 53 | + current[insertKey] = new Node(value, null, null, current); |
| 54 | + else |
| 55 | + this.insert(value, current[insertKey]); |
| 56 | +}; |
| 57 | + |
| 58 | +/** |
| 59 | + * Prints the nodes of the tree in order. It starts the tree traversal from a given node. |
| 60 | + * |
| 61 | + * @private |
| 62 | + * @param {Node} Node from which to start the traversal |
| 63 | + * @param {Function} Callback which will be called for each traversed node |
| 64 | + */ |
| 65 | +BinaryTree.prototype._inorder = function (current, callback) { |
| 66 | + if (!current) |
| 67 | + return; |
| 68 | + this._inorder(current._left, callback); |
| 69 | + if (typeof callback === 'function') |
| 70 | + callback(current); |
| 71 | + this._inorder(current._right, callback); |
| 72 | +}; |
| 73 | + |
| 74 | +/** |
| 75 | + * Inorder traversal of the whole binary search tree |
| 76 | + * |
| 77 | + * @public |
| 78 | + * @param {Function} Callback which will be called for each traversed node |
| 79 | + */ |
| 80 | +BinaryTree.prototype.inorder = function (callback) { |
| 81 | + return this._inorder(this._root, callback); |
| 82 | +}; |
| 83 | + |
| 84 | +/** |
| 85 | + * Post-order traversal from given node |
| 86 | + * |
| 87 | + * @private |
| 88 | + * @param {Node} Node from which to start the traversal |
| 89 | + * @param {Function} Callback which will be called for each traversed node |
| 90 | + */ |
| 91 | +BinaryTree.prototype._postorder = function (current, callback) { |
| 92 | + if (!current) |
| 93 | + return; |
| 94 | + if (typeof callback === 'function') |
| 95 | + callback(current); |
| 96 | + this._postorder(current._left, callback); |
| 97 | + this._postorder(current._right, callback); |
| 98 | +}; |
| 99 | + |
| 100 | +/** |
| 101 | + * Post-order traversal of the whole tree |
| 102 | + * |
| 103 | + * @public |
| 104 | + * @param {Function} Callback which will be called for each traversed node |
| 105 | + */ |
| 106 | +BinaryTree.prototype.postorder = function (callback) { |
| 107 | + return this._postorder(this._root, callback); |
| 108 | +}; |
| 109 | + |
| 110 | +/** |
| 111 | + * Pre-order traversal of the tree from given node |
| 112 | + * |
| 113 | + * @private |
| 114 | + * @param {Node} Node from which to start the traversal |
| 115 | + * @param {Function} Callback which will be called for each traversed node |
| 116 | + */ |
| 117 | +BinaryTree.prototype._preorder = function (current, callback) { |
| 118 | + if (!current) |
| 119 | + return; |
| 120 | + if (typeof callback === 'function') |
| 121 | + callback(current); |
| 122 | + this._preorder(current._left, callback); |
| 123 | + this._preorder(current._right, callback); |
| 124 | +}; |
| 125 | + |
| 126 | +/** |
| 127 | + * Pre-order preorder traversal of the whole tree |
| 128 | + * |
| 129 | + * @public |
| 130 | + * @param {Function} Callback which will be called for each traversed node |
| 131 | + */ |
| 132 | +BinaryTree.prototype.preorder = function (callback) { |
| 133 | + return this._preorder(this._root, callback); |
| 134 | +}; |
| 135 | + |
| 136 | +/** |
| 137 | + * Finds a node by it's value. Average runtime complexity O(log n) |
| 138 | + * |
| 139 | + * @public |
| 140 | + * @param {number|string} Value of the node which should be found |
| 141 | + */ |
| 142 | +BinaryTree.prototype.find = function (value) { |
| 143 | + return this._find(value, this._root); |
| 144 | +}; |
| 145 | + |
| 146 | +/** |
| 147 | + * Finds a node by it's value in given sub-tree. Average runtime complexity: O(log n). |
| 148 | + * |
| 149 | + * @private |
| 150 | + * @param {number|string} Value of the node which should be found |
| 151 | + * @param {Node} Current node to be checked |
| 152 | + */ |
| 153 | +BinaryTree.prototype._find = function (value, current) { |
| 154 | + if (!current) |
| 155 | + return null; |
| 156 | + |
| 157 | + if (current.value === value) |
| 158 | + return current; |
| 159 | + |
| 160 | + if (current.value > value) |
| 161 | + return this._find(value, current._left); |
| 162 | + |
| 163 | + if (current.value < value) |
| 164 | + return this._find(value, current._right); |
| 165 | + |
| 166 | +}; |
| 167 | + |
| 168 | +/** |
| 169 | + * Replaces given child with new one, for given parent |
| 170 | + * |
| 171 | + * @private |
| 172 | + * @param {Node} Parent node |
| 173 | + * @param {Node} Child to be replaced |
| 174 | + * @param {Node} Child replacement |
| 175 | + */ |
| 176 | +BinaryTree.prototype._replaceChild = function (parent, oldChild, newChild) { |
| 177 | + if (!parent) { |
| 178 | + this._root = newChild; |
| 179 | + this._root._parent = null; |
| 180 | + } else { |
| 181 | + |
| 182 | + if (parent._left === oldChild) |
| 183 | + parent._left = newChild; |
| 184 | + else |
| 185 | + parent._right = newChild; |
| 186 | + |
| 187 | + if (newChild) { |
| 188 | + newChild._parent = parent; |
| 189 | + } |
| 190 | + } |
| 191 | +}; |
| 192 | + |
| 193 | +/** |
| 194 | + * Removes node from the tree. Average runtime complexity: O(log n). |
| 195 | + * |
| 196 | + * @public |
| 197 | + * @param {Node} Node to be removed |
| 198 | + * @returns {boolean} True/false depending on whether the given node is removed |
| 199 | + */ |
| 200 | +BinaryTree.prototype.remove = function (node) { |
| 201 | + if (!node) |
| 202 | + return false; |
| 203 | + |
| 204 | + if (node._left && node._right) { |
| 205 | + var min = this._findMin(node._right), |
| 206 | + temp = node.value; |
| 207 | + |
| 208 | + node.value = min.value; |
| 209 | + min.value = temp; |
| 210 | + return this.remove(min); |
| 211 | + } else { |
| 212 | + if (node._left) |
| 213 | + this._replaceChild(node._parent, node, node._left); |
| 214 | + else if (node._right) |
| 215 | + this._replaceChild(node._parent, node, node._right); |
| 216 | + else |
| 217 | + this._replaceChild(node._parent, node, null); |
| 218 | + return true; |
| 219 | + } |
| 220 | +}; |
| 221 | + |
| 222 | +/** |
| 223 | + * Finds the node with minimum value in given sub-tree |
| 224 | + * |
| 225 | + * @private |
| 226 | + * @param {Node} Root of the sub-tree |
| 227 | + * @param {[number|string]} Current minimum value of the sub-tree |
| 228 | + * @returns {Node} The node with minimum value in the sub-tree |
| 229 | + */ |
| 230 | +BinaryTree.prototype._findMin = function (node, current) { |
| 231 | + current = current || { value: Infinity }; |
| 232 | + if (!node) |
| 233 | + return current; |
| 234 | + if (current.value > node.value) |
| 235 | + current = node; |
| 236 | + return this._findMin(node._left, current); |
| 237 | +}; |
| 238 | + |
| 239 | +/** |
| 240 | + * Finds the node with maximum value in given sub-tree |
| 241 | + * |
| 242 | + * @private |
| 243 | + * @param {Node} Root of the sub-tree |
| 244 | + * @param {[number|string]} Current maximum value of the sub-tree |
| 245 | + * @returns {Node} The node with maximum value in the sub-tree |
| 246 | + */ |
| 247 | +BinaryTree.prototype._findMax = function (node, current) { |
| 248 | + current = current || { value: -Infinity }; |
| 249 | + if (!node) |
| 250 | + return current; |
| 251 | + if (current.value < node.value) |
| 252 | + current = node; |
| 253 | + return this._findMax(node._right, current); |
| 254 | +}; |
| 255 | + |
| 256 | +/** |
| 257 | + * Finds the node with minimum value in the whole tree |
| 258 | + * |
| 259 | + * @public |
| 260 | + * @returns {Node} The minimum node of the tree |
| 261 | + */ |
| 262 | +BinaryTree.prototype.findMin = function () { |
| 263 | + return this._findMin(this._root); |
| 264 | +}; |
| 265 | + |
| 266 | +/** |
| 267 | + * Finds the maximum node of the tree |
| 268 | + * |
| 269 | + * @public |
| 270 | + * @returns {Node} The maximum node of the tree |
| 271 | + * |
| 272 | + */ |
| 273 | +BinaryTree.prototype.findMax = function () { |
| 274 | + return this._findMax(this._root); |
| 275 | +}; |
0 commit comments