Skip to content

Commit 7a1141b

Browse files
authored
refactor: BFS tree algorithms (#1108)
* 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 * refactor out traversal member var .. per earlier discussion w mods also tweaked the tests to achieve 100% coverage
1 parent 7ab9792 commit 7a1141b

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

Trees/BreadthFirstTreeTraversal.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,33 @@ class Node {
1414
class BinaryTree {
1515
constructor () {
1616
this.root = null
17-
this.traversal = []
1817
}
1918

2019
breadthFirstIterative () {
21-
this.traversal = []
20+
const traversal = []
2221
if (this.root) {
23-
this.traversal.push(this.root)
22+
traversal.push(this.root)
2423
}
25-
for (let i = 0; i < this.traversal.length; i++) {
26-
const currentNode = this.traversal[i]
24+
for (let i = 0; i < traversal.length; i++) {
25+
const currentNode = traversal[i]
2726
if (currentNode.left) {
28-
this.traversal.push(currentNode.left)
27+
traversal.push(currentNode.left)
2928
}
3029
if (currentNode.right) {
31-
this.traversal.push(currentNode.right)
30+
traversal.push(currentNode.right)
3231
}
33-
this.traversal[i] = currentNode.data
32+
traversal[i] = currentNode.data
3433
}
35-
return this.traversal
34+
return traversal
3635
}
3736

3837
breadthFirstRecursive () {
39-
this.traversal = []
38+
const traversal = []
4039
const h = this.getHeight(this.root)
4140
for (let i = 0; i !== h; i++) {
42-
this.traverseLevel(this.root, i)
41+
this.traverseLevel(this.root, i, traversal)
4342
}
44-
return this.traversal
43+
return traversal
4544
}
4645

4746
// Computing the height of the tree
@@ -54,15 +53,15 @@ class BinaryTree {
5453
return lheight > rheight ? lheight + 1 : rheight + 1
5554
}
5655

57-
traverseLevel (node, levelRemaining) {
56+
traverseLevel (node, levelRemaining, traversal) {
5857
if (node === null) {
5958
return
6059
}
6160
if (levelRemaining === 0) {
62-
this.traversal.push(node.data)
61+
traversal.push(node.data)
6362
} else {
64-
this.traverseLevel(node.left, levelRemaining - 1)
65-
this.traverseLevel(node.right, levelRemaining - 1)
63+
this.traverseLevel(node.left, levelRemaining - 1, traversal)
64+
this.traverseLevel(node.right, levelRemaining - 1, traversal)
6665
}
6766
}
6867
}

Trees/test/BreadthFirstTreeTraversal.test.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,25 @@ describe('Breadth First Tree Traversal', () => {
88
root.right = new Node(8)
99
root.left.left = new Node(3)
1010
root.left.right = new Node(6)
11-
root.right.right = new Node(9)
11+
root.left.right.right = new Node(9)
1212
binaryTree.root = root
1313

1414
// Visualization :
1515
//
1616
// 7
1717
// / \
1818
// 5 8
19-
// / \ \
20-
// 3 6 9
19+
// / \
20+
// 3 6
21+
// \
22+
// 9
23+
24+
it('Binary tree - Empty case', () => {
25+
const emptyTree = new BinaryTree()
26+
expect(emptyTree.breadthFirstIterative()).toStrictEqual([])
27+
})
2128

2229
it('Binary tree - Level order recursive traversal', () => {
23-
expect(binaryTree.traversal).toStrictEqual([])
2430
const traversal = binaryTree.breadthFirstRecursive()
2531
expect(traversal).toStrictEqual([7, 5, 8, 3, 6, 9])
2632
})

0 commit comments

Comments
 (0)