Skip to content

Commit 657c224

Browse files
committed
Test cases for BST traversals
1 parent e78eae5 commit 657c224

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const BinarySearchTree = require('./index');
2+
3+
describe('Binary search tree traversals', () => {
4+
let bst;
5+
let preOrderTraversal, inOrderTraversal, postOrderTraversal;
6+
7+
describe('Creates BST', () => {
8+
// Creates BST
9+
bst = new BinarySearchTree(6);
10+
bst.add(4);
11+
bst.add(9);
12+
bst.add(2);
13+
bst.add(5);
14+
bst.add(8);
15+
bst.add(12);
16+
});
17+
18+
describe('BST traversals', () => {
19+
it('should complete the Preorder traversal for the above created bst', () => {
20+
preOrderTraversal = bst.traversePreorder();
21+
expect(preOrderTraversal).toEqual([6, 4, 2, 5, 9, 8, 12]);
22+
});
23+
24+
it('should complete the Inorder traversal for the above created bst', () => {
25+
inOrderTraversal = bst.traverseInorder();
26+
expect(inOrderTraversal).toEqual([2, 4, 5, 6, 8, 9, 12]);
27+
});
28+
29+
it('should complete the Postorder traversal for the above created bst', () => {
30+
postOrderTraversal = bst.traversePostorder();
31+
expect(postOrderTraversal).toEqual([2, 5, 4, 8, 12, 9, 6]);
32+
});
33+
});
34+
});

0 commit comments

Comments
 (0)