|
| 1 | +const BinarySearchTree = require('./index'); |
| 2 | +const heightOfBST = require('./height-of-bst/index'); |
| 3 | + |
| 4 | +describe('Binary search tree traversals', () => { |
| 5 | + let bst; |
| 6 | + |
| 7 | + describe('Creates BST', () => { |
| 8 | + it('should create BST', () => { |
| 9 | + // Creates BST |
| 10 | + bst = new BinarySearchTree(6); |
| 11 | + bst.add(4); |
| 12 | + bst.add(9); |
| 13 | + bst.add(2); |
| 14 | + bst.add(5); |
| 15 | + bst.add(8); |
| 16 | + bst.add(12); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('Check bst was created as expected', () => { |
| 21 | + it('Inorder traversal of the created bst should be [ 2, 4, 5, 6, 8, 9, 12 ]', () => { |
| 22 | + expect(bst.traverseInorder()).toEqual([2, 4, 5, 6, 8, 9, 12]); |
| 23 | + }); |
| 24 | + |
| 25 | + it('Preorder traversal of the created bst should be [ 6, 4, 2, 5, 9, 8, 12 ]', () => { |
| 26 | + expect(bst.traversePreorder()).toEqual([6, 4, 2, 5, 9, 8, 12]); |
| 27 | + }); |
| 28 | + |
| 29 | + it('Postorder traversal of the created bst should be [ 2, 5, 4, 8, 12, 9, 6 ]', () => { |
| 30 | + expect(bst.traversePostorder()).toEqual([2, 5, 4, 8, 12, 9, 6]); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('BST node deletions', () => { |
| 35 | + it('should check height of bst to be 3 prior deletion', () => { |
| 36 | + expect(heightOfBST(bst.root)).toEqual(3); |
| 37 | + }); |
| 38 | + |
| 39 | + it('deleting leaf element does not affect tree height if it has sibling', () => { |
| 40 | + bst.remove(2); |
| 41 | + bst.remove(8); |
| 42 | + expect(heightOfBST(bst.root)).toEqual(3); |
| 43 | + }); |
| 44 | + |
| 45 | + it('deleting leaf element does affect tree height if it has no-sibling', () => { |
| 46 | + bst.remove(5); |
| 47 | + bst.remove(12); |
| 48 | + expect(heightOfBST(bst.root)).toEqual(2); |
| 49 | + }); |
| 50 | + }); |
| 51 | +}); |
0 commit comments