mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
Breadth FIrst Tree Traversal : Convert live test into Jest test.
This commit is contained in:
@ -22,7 +22,7 @@ class BinaryTree {
|
|||||||
for (let i = 1; i <= h; i++) {
|
for (let i = 1; i <= h; i++) {
|
||||||
this.traverseLevel(this.root, i)
|
this.traverseLevel(this.root, i)
|
||||||
}
|
}
|
||||||
return this.traversal.toLocaleString()
|
return this.traversal
|
||||||
}
|
}
|
||||||
|
|
||||||
// Computing the height of the tree
|
// Computing the height of the tree
|
||||||
@ -48,19 +48,4 @@ class BinaryTree {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const binaryTree = new BinaryTree()
|
export { BinaryTree, Node }
|
||||||
const root = new Node(7)
|
|
||||||
root.left = new Node(5)
|
|
||||||
root.right = new Node(8)
|
|
||||||
root.left.left = new Node(3)
|
|
||||||
root.left.right = new Node(6)
|
|
||||||
root.right.right = new Node(9)
|
|
||||||
binaryTree.root = root
|
|
||||||
|
|
||||||
console.log(binaryTree.breadthFirst())
|
|
||||||
|
|
||||||
// 7
|
|
||||||
// / \
|
|
||||||
// 5 8
|
|
||||||
// / \ \
|
|
||||||
// 3 6 9
|
|
||||||
|
27
Trees/test/BreadthFirstTreeTraversal.test.js
Normal file
27
Trees/test/BreadthFirstTreeTraversal.test.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { BinaryTree, Node } from '../BreadthFirstTreeTraversal'
|
||||||
|
|
||||||
|
describe('Breadth First Tree Traversal', () => {
|
||||||
|
const binaryTree = new BinaryTree()
|
||||||
|
|
||||||
|
const root = new Node(7)
|
||||||
|
root.left = new Node(5)
|
||||||
|
root.right = new Node(8)
|
||||||
|
root.left.left = new Node(3)
|
||||||
|
root.left.right = new Node(6)
|
||||||
|
root.right.right = new Node(9)
|
||||||
|
binaryTree.root = root
|
||||||
|
|
||||||
|
// Vizualization :
|
||||||
|
//
|
||||||
|
// 7
|
||||||
|
// / \
|
||||||
|
// 5 8
|
||||||
|
// / \ \
|
||||||
|
// 3 6 9
|
||||||
|
|
||||||
|
it('Binary tree - Level order traversal', () => {
|
||||||
|
expect(binaryTree.traversal).toStrictEqual([])
|
||||||
|
const traversal = binaryTree.breadthFirst()
|
||||||
|
expect(traversal).toStrictEqual([7, 5, 8, 3, 6, 9])
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user