Skip to content

Commit e3d605c

Browse files
committed
Breadth FIrst Tree Traversal : Convert live test into Jest test.
1 parent 036ac90 commit e3d605c

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

Trees/BreadthFirstTreeTraversal.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class BinaryTree {
2222
for (let i = 1; i <= h; i++) {
2323
this.traverseLevel(this.root, i)
2424
}
25-
return this.traversal.toLocaleString()
25+
return this.traversal
2626
}
2727

2828
// Computing the height of the tree
@@ -48,19 +48,4 @@ class BinaryTree {
4848
}
4949
}
5050

51-
const binaryTree = new BinaryTree()
52-
const root = new Node(7)
53-
root.left = new Node(5)
54-
root.right = new Node(8)
55-
root.left.left = new Node(3)
56-
root.left.right = new Node(6)
57-
root.right.right = new Node(9)
58-
binaryTree.root = root
59-
60-
console.log(binaryTree.breadthFirst())
61-
62-
// 7
63-
// / \
64-
// 5 8
65-
// / \ \
66-
// 3 6 9
51+
export { BinaryTree, Node }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { BinaryTree, Node } from '../BreadthFirstTreeTraversal'
2+
3+
describe('Breadth First Tree Traversal', () => {
4+
const binaryTree = new BinaryTree()
5+
6+
const root = new Node(7)
7+
root.left = new Node(5)
8+
root.right = new Node(8)
9+
root.left.left = new Node(3)
10+
root.left.right = new Node(6)
11+
root.right.right = new Node(9)
12+
binaryTree.root = root
13+
14+
// Vizualization :
15+
//
16+
// 7
17+
// / \
18+
// 5 8
19+
// / \ \
20+
// 3 6 9
21+
22+
it('Binary tree - Level order traversal', () => {
23+
expect(binaryTree.traversal).toStrictEqual([])
24+
const traversal = binaryTree.breadthFirst()
25+
expect(traversal).toStrictEqual([7, 5, 8, 3, 6, 9])
26+
})
27+
})

0 commit comments

Comments
 (0)