Skip to content

Commit 7ab9792

Browse files
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 clarity
1 parent 9bcf16b commit 7ab9792

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

Trees/BreadthFirstTreeTraversal.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,26 @@ class BinaryTree {
1717
this.traversal = []
1818
}
1919

20-
breadthFirst () {
20+
breadthFirstIterative () {
21+
this.traversal = []
22+
if (this.root) {
23+
this.traversal.push(this.root)
24+
}
25+
for (let i = 0; i < this.traversal.length; i++) {
26+
const currentNode = this.traversal[i]
27+
if (currentNode.left) {
28+
this.traversal.push(currentNode.left)
29+
}
30+
if (currentNode.right) {
31+
this.traversal.push(currentNode.right)
32+
}
33+
this.traversal[i] = currentNode.data
34+
}
35+
return this.traversal
36+
}
37+
38+
breadthFirstRecursive () {
39+
this.traversal = []
2140
const h = this.getHeight(this.root)
2241
for (let i = 0; i !== h; i++) {
2342
this.traverseLevel(this.root, i)

Trees/test/BreadthFirstTreeTraversal.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ describe('Breadth First Tree Traversal', () => {
1919
// / \ \
2020
// 3 6 9
2121

22-
it('Binary tree - Level order traversal', () => {
22+
it('Binary tree - Level order recursive traversal', () => {
2323
expect(binaryTree.traversal).toStrictEqual([])
24-
const traversal = binaryTree.breadthFirst()
24+
const traversal = binaryTree.breadthFirstRecursive()
25+
expect(traversal).toStrictEqual([7, 5, 8, 3, 6, 9])
26+
})
27+
28+
it('Binary tree - Level order iterative traversal', () => {
29+
const traversal = binaryTree.breadthFirstIterative()
2530
expect(traversal).toStrictEqual([7, 5, 8, 3, 6, 9])
2631
})
2732
})

0 commit comments

Comments
 (0)