|
30 | 30 | * @module data-structures/size-balanced-tree |
31 | 31 | */ |
32 | 32 | (function (exports) { |
33 | | - |
34 | 33 | 'use strict'; |
35 | 34 |
|
36 | | - |
37 | | - |
38 | 35 | /** |
39 | 36 | * Node of the Size-Balanced tree. |
40 | 37 | * |
|
101 | 98 |
|
102 | 99 | childNode |
103 | 100 | / \ |
104 | | - node CR |
| 101 | + node CR |
105 | 102 | / \ |
106 | 103 | NL CL |
107 | 104 | */ |
108 | 105 | node.right = childNode.left; |
109 | | - if (node.right !== Nil) node.right.parent = node; |
| 106 | + if (node.right !== Nil) { |
| 107 | + node.right.parent = node; |
| 108 | + } |
110 | 109 | childNode.left = node; |
111 | | - updateChild(node, childNode) //Access node.parent |
| 110 | + updateChild(node, childNode); //Access node.parent |
112 | 111 | node.parent = childNode; |
113 | 112 | node.updateSize(); |
114 | 113 | return childNode; |
|
126 | 125 |
|
127 | 126 | childNode |
128 | 127 | / \ |
129 | | - CL node |
| 128 | + CL node |
130 | 129 | / \ |
131 | 130 | CR NR |
132 | 131 | */ |
133 | 132 | node.left = childNode.right; |
134 | | - if (node.left !== Nil) node.left.parent = node; |
| 133 | + if (node.left !== Nil) { |
| 134 | + node.left.parent = node; |
| 135 | + } |
135 | 136 | childNode.right = node; |
136 | | - updateChild(node, childNode) //Access node.parent |
| 137 | + updateChild(node, childNode); //Access node.parent |
137 | 138 | node.parent = childNode; |
138 | 139 | node.updateSize(); |
139 | 140 | return childNode; |
|
174 | 175 | while (node.parent !== Nil) { |
175 | 176 | var childNode = node; |
176 | 177 | node = node.parent; |
177 | | - if (node.left == childNode) { |
| 178 | + if (node.left === childNode) { |
178 | 179 | node = maintain(node, true); |
179 | 180 | } else { |
180 | 181 | node = maintain(node, false); |
|
198 | 199 | } |
199 | 200 |
|
200 | 201 | function findNodeAtPos(node, pos) { |
201 | | - while (pos != node.left.size) { |
| 202 | + while (pos !== node.left.size) { |
202 | 203 | if (pos < node.left.size) { |
203 | 204 | node = node.left; |
204 | 205 | } else { |
205 | 206 | pos -= node.left.size; |
206 | | - --pos; //The node element should be decrement by 1 |
| 207 | + pos -= 1; //The node element should be decrement by 1 |
207 | 208 | node = node.right; |
208 | 209 | } |
209 | 210 | } |
|
220 | 221 | this._root = Nil; |
221 | 222 | }; |
222 | 223 |
|
223 | | - |
224 | 224 | exports.SBTree.prototype = { |
225 | 225 | get size() { |
226 | 226 | return this._root.size; |
227 | 227 | }, |
228 | | - } |
229 | | - |
| 228 | + }; |
| 229 | + |
230 | 230 | /** |
231 | 231 | * Push a value to the end of tree.<br><br> |
232 | 232 | * Complexity: O(log N). |
|
238 | 238 | exports.SBTree.prototype.push = function (value) { |
239 | 239 | var node = findRightMost(this._root); |
240 | 240 | var newNode = new Node(value, node, Nil, Nil, 1); |
241 | | - if (node !== Nil) node.right = newNode; |
| 241 | + if (node !== Nil) { |
| 242 | + node.right = newNode; |
| 243 | + } |
242 | 244 | this._root = maintainSizeBalancedTree(newNode); |
243 | 245 | return newNode; |
244 | 246 | }; |
245 | 247 |
|
246 | | - exports.SBTree.prototype.get = function(pos) { |
| 248 | + exports.SBTree.prototype.get = function (pos) { |
247 | 249 | if (pos >= this._root.size) { |
248 | 250 | return Nil; |
249 | 251 | } |
250 | 252 | return findNodeAtPos(this._root, pos); |
251 | 253 | }; |
252 | 254 |
|
253 | | - exports.SBTree.prototype.getIndex = function(node) { |
| 255 | + exports.SBTree.prototype.getIndex = function (node) { |
254 | 256 | var index = node.left.size; |
255 | | - while (node != this._root) { |
| 257 | + while (node !== this._root) { |
256 | 258 | var parent = node.parent; |
257 | 259 | if (parent.right === node) { |
258 | 260 | index += parent.left.size + 1; |
|
262 | 264 | return index; |
263 | 265 | }; |
264 | 266 |
|
265 | | - exports.SBTree.prototype.insert = function(pos, value) { |
| 267 | + exports.SBTree.prototype.insert = function (pos, value) { |
266 | 268 | if (pos >= this._root.size) { |
267 | | - return this.push(value) |
| 269 | + return this.push(value); |
268 | 270 | } |
269 | 271 | var node = findNodeAtPos(this._root, pos); |
270 | | - var newNode |
| 272 | + var newNode; |
271 | 273 | if (node.left === Nil) { |
272 | 274 | newNode = new Node(value, node, Nil, Nil, 1); |
273 | 275 | node.left = newNode; |
|
280 | 282 | return newNode; |
281 | 283 | }; |
282 | 284 |
|
283 | | - exports.SBTree.prototype.remove = function(pos) { |
| 285 | + exports.SBTree.prototype.remove = function (pos) { |
284 | 286 | if (pos >= this._root.size) { |
285 | 287 | return Nil; // There is no element to remove |
286 | 288 | } |
|
289 | 291 |
|
290 | 292 | /* |
291 | 293 | Before remove: |
292 | | - P(node's parent, be notices, N either be left child or right child of P) |
| 294 | + P (node's parent, be notices, |
| 295 | + | N either be left child or right child of P) |
293 | 296 | | |
294 | 297 | N(node) |
295 | 298 | / \ |
|
302 | 305 | After remove node N: |
303 | 306 | P(node's parent) |
304 | 307 | / |
305 | | - L |
| 308 | + L |
306 | 309 | \ |
307 | 310 | \ |
308 | 311 | LRM(Left-Rightmost) |
309 | 312 | \ |
310 | 313 | R |
311 | 314 |
|
312 | 315 | N(node) is wild node that was removed |
313 | | - |
| 316 | +
|
314 | 317 | */ |
315 | 318 | if (node.left !== Nil){ |
316 | 319 | var LRM = findRightMost(node.left); |
317 | | - updateChild(node, node.left) |
318 | | - LRM.right = node.right |
| 320 | + updateChild(node, node.left); |
| 321 | + LRM.right = node.right; |
319 | 322 | if (LRM.right === Nil) { |
320 | 323 | maintainNode = LRM; |
321 | 324 | } else { |
|
324 | 327 | } |
325 | 328 | } else if (node.right !== Nil) { |
326 | 329 | var RLM = findLeftMost(node.right); |
327 | | - updateChild(node, node.right) |
328 | | - RLM.left = node.left |
| 330 | + updateChild(node, node.right); |
| 331 | + RLM.left = node.left; |
329 | 332 | if (RLM.left === Nil) { |
330 | 333 | maintainNode = RLM; |
331 | 334 | } else { |
332 | 335 | RLM.left.parent = RLM; |
333 | 336 | maintainNode = RLM.left; |
334 | 337 | } |
335 | 338 | } else { |
336 | | - updateChild(node, Nil) |
| 339 | + updateChild(node, Nil); |
337 | 340 | maintainNode = node.parent; |
338 | 341 | } |
339 | 342 | this._root = maintainSizeBalancedTree(maintainNode); |
340 | 343 | return node; |
341 | 344 | }; |
342 | 345 |
|
343 | | - |
344 | 346 | })(typeof module === 'undefined' ? window : module.exports); |
0 commit comments