-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit 7ab9792
authored
algorithm: Iterative (and in-place) BFS for binary trees (#1102)
* Bugfix AVLTree comparator
The original insertBalance function was doing raw value comparisons as opposed to using the tree's comparator. This is clearly unintentional, and would (ultimately) cause the structure to segfault when constructed with the stringData included in the updated test.
I've added the fix, scanned the rest of the code for similar issues, and added the appropriate test case which passes successfully with the fix. The jest code coverage increases slightly as well with the changes.
* 100% jest code coverage
Added a couple of extra elements to the original test tree, and then removed elements in an order such that all previously uncovered branches of code are now covered.
Also added an emptyTree structure to test some additional (trivial) base cases.
* standard style fix
missed this from my previous commit
* Iterative & in-place BFS
An iterative analog to the traditional recursive breadth-first-search algorithm for binary trees.
This in-place solution uses the pre-existing "traversal" array for both tracking the algorithm as well as storing the result.
Also tweaked old code by resetting the traversal array each time the tree is traversed (otherwise you're only allowed to traverse a tree once which doesn't seem correct even with a single traversal function).
* Update BreadthFirstTreeTraversal.js
got rid of unnecessary currentSize
added currentNode for clarity1 parent 9bcf16b commit 7ab9792
File tree
2 files changed
+27
-3
lines changed- Trees
- test
2 files changed
+27
-3
lines changedLines changed: 20 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
17 | 17 |
| |
18 | 18 |
| |
19 | 19 |
| |
20 | - | ||
20 | + | ||
21 | + | ||
22 | + | ||
23 | + | ||
24 | + | ||
25 | + | ||
26 | + | ||
27 | + | ||
28 | + | ||
29 | + | ||
30 | + | ||
31 | + | ||
32 | + | ||
33 | + | ||
34 | + | ||
35 | + | ||
36 | + | ||
37 | + | ||
38 | + | ||
39 | + | ||
21 | 40 |
| |
22 | 41 |
| |
23 | 42 |
| |
|
Lines changed: 7 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
19 | 19 |
| |
20 | 20 |
| |
21 | 21 |
| |
22 | - | ||
22 | + | ||
23 | 23 |
| |
24 | - | ||
24 | + | ||
25 | + | ||
26 | + | ||
27 | + | ||
28 | + | ||
29 | + | ||
25 | 30 |
| |
26 | 31 |
| |
27 | 32 |
|
0 commit comments