Skip to content

Commit b18715b

Browse files
improve time complexity documentation
1 parent 312b62e commit b18715b

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

lib/SymbolTree.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class SymbolTree {
147147
/**
148148
* Find the inclusive descendant that is last in tree order of the given object.
149149
*
150-
* `O(n)` (worst case)
150+
* `O(n)` (worst case) where n is the depth of the subtree of `object`
151151
*
152152
* @method lastInclusiveDescendant
153153
* @memberOf module:symbol-tree#
@@ -169,7 +169,8 @@ class SymbolTree {
169169
* An object A is preceding an object B if A and B are in the same tree
170170
* and A comes before B in tree order.
171171
*
172-
* `O(n)` (worst case)
172+
* `O(n)` (worst case) <br>
173+
* `O(1)` (amortized when walking the entire tree)
173174
*
174175
* @method preceding
175176
* @memberOf module:symbol-tree#
@@ -202,7 +203,8 @@ class SymbolTree {
202203
* An object A is following an object B if A and B are in the same tree
203204
* and A comes after B in tree order.
204205
*
205-
* `O(n)` (worst case)
206+
* `O(n)` (worst case) where n is the amount of objects in the entire tree<br>
207+
* `O(1)` (amortized when walking the entire tree)
206208
*
207209
* @method following
208210
* @memberOf module:symbol-tree#
@@ -247,7 +249,7 @@ class SymbolTree {
247249
/**
248250
* Append all children of the given object to an array.
249251
*
250-
* `O(n)`
252+
* `O(n)` where n is the amount of children of the given `parent`
251253
*
252254
* @method childrenToArray
253255
* @memberOf module:symbol-tree#
@@ -287,7 +289,7 @@ class SymbolTree {
287289
/**
288290
* Append all inclusive ancestors of the given object to an array.
289291
*
290-
* `O(n)`
292+
* `O(n)` where n is the amount of ancestors of the given `object`
291293
*
292294
* @method ancestorsToArray
293295
* @memberOf module:symbol-tree#
@@ -320,7 +322,7 @@ class SymbolTree {
320322
/**
321323
* Append all descendants of the given object to an array (in tree order).
322324
*
323-
* `O(n)`
325+
* `O(n)` where n is the amount of objects in the sub-tree of the given `object`
324326
*
325327
* @method treeToArray
326328
* @memberOf module:symbol-tree#
@@ -434,8 +436,9 @@ class SymbolTree {
434436
/**
435437
* Iterate over all descendants of the given object (in tree order).
436438
*
437-
* `O(n)` for the entire iteration<br>
438-
* `O(n)` for a single iteration (worst case)
439+
* where n is the amount of objects in the sub-tree of the given `root`:
440+
* `O(n)` (worst case for a single iterator)
441+
* `O(n)` (amortized, when completing the iterator)<br>
439442
*
440443
* @method treeIterator
441444
* @memberOf module:symbol-tree#
@@ -458,8 +461,8 @@ class SymbolTree {
458461
/**
459462
* Find the index of the given object (the number of preceding siblings).
460463
*
461-
* `O(n)`<br>
462-
* `O(1)` (cached)
464+
* `O(n)` where n is the amount of preceding siblings<br>
465+
* `O(1)` (amortized, if the tree is not modified)
463466
*
464467
* @method index
465468
* @memberOf module:symbol-tree#
@@ -512,8 +515,8 @@ class SymbolTree {
512515
/**
513516
* Calculate the number of children.
514517
*
515-
* `O(n)`<br>
516-
* `O(1)` (cached)
518+
* `O(n)` where n is the amount of children<br>
519+
* `O(1)` (amortized, if the tree is not modified)
517520
*
518521
* @method childrenCount
519522
* @memberOf module:symbol-tree#
@@ -544,7 +547,10 @@ class SymbolTree {
544547
* The semantics are the same as compareDocumentPosition in DOM, with the exception that
545548
* DISCONNECTED never occurs with any other bit.
546549
*
547-
* `O(n)` (worst case)
550+
* where n and m are the amount of ancestors of `left` and `right`;
551+
* where o is the amount of children of the lowest common ancestor of `left` and `right`:
552+
* `O(n + m + o)` (worst case)
553+
* `O(n + m) (amortized, if the tree is not modified)
548554
*
549555
* @method compareTreePosition
550556
* @memberOf module:symbol-tree#
@@ -599,6 +605,7 @@ class SymbolTree {
599605
return TreePosition.DISCONNECTED;
600606
}
601607

608+
// find the lowest common ancestor
602609
let commonAncestorIndex = 0;
603610
const ancestorsMinLength = Math.min(leftAncestors.length, rightAncestors.length);
604611

0 commit comments

Comments
 (0)