Files
JavaScript/Trees/BreadthFirstTreeTraversal.js
k ho k ho? 7a1141b637 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
2022-09-25 13:41:00 +05:30

70 lines
1.5 KiB
JavaScript

/*
Breadth First Tree Traversal or level order traversal implementation in javascript
Author: @GerardUbuntu
*/
class Node {
constructor (data) {
this.data = data
this.left = null
this.right = null
}
}
class BinaryTree {
constructor () {
this.root = null
}
breadthFirstIterative () {
const traversal = []
if (this.root) {
traversal.push(this.root)
}
for (let i = 0; i < traversal.length; i++) {
const currentNode = traversal[i]
if (currentNode.left) {
traversal.push(currentNode.left)
}
if (currentNode.right) {
traversal.push(currentNode.right)
}
traversal[i] = currentNode.data
}
return traversal
}
breadthFirstRecursive () {
const traversal = []
const h = this.getHeight(this.root)
for (let i = 0; i !== h; i++) {
this.traverseLevel(this.root, i, traversal)
}
return traversal
}
// Computing the height of the tree
getHeight (node) {
if (node === null) {
return 0
}
const lheight = this.getHeight(node.left)
const rheight = this.getHeight(node.right)
return lheight > rheight ? lheight + 1 : rheight + 1
}
traverseLevel (node, levelRemaining, traversal) {
if (node === null) {
return
}
if (levelRemaining === 0) {
traversal.push(node.data)
} else {
this.traverseLevel(node.left, levelRemaining - 1, traversal)
this.traverseLevel(node.right, levelRemaining - 1, traversal)
}
}
}
export { BinaryTree, Node }