|
| 1 | +const BinarySearchTree = require('./index'); |
| 2 | + |
| 3 | +describe('Binary Search Tree', () => { |
| 4 | + let bst; |
| 5 | + let rootsLeftChild; |
| 6 | + let rootsRightChild; |
| 7 | + let rootsLeftChildsLeftChild; |
| 8 | + let rootsLeftChildsRightChild; |
| 9 | + let rootsRightChildsLeftChild; |
| 10 | + let rootsRightChildsRightChild; |
| 11 | + |
| 12 | + describe('Creates a binary search tree', () => { |
| 13 | + it('should create a bst with root 100', () => { |
| 14 | + bst = new BinarySearchTree(100); |
| 15 | + expect(bst.root.value).toEqual(100); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should add element 20 to the left of root node', () => { |
| 19 | + bst.add(20); |
| 20 | + rootsLeftChild = bst.root.leftChild; |
| 21 | + expect(rootsLeftChild.value).toEqual(20); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should add element 500 to the right of root node', () => { |
| 25 | + bst.add(500); |
| 26 | + rootsRightChild = bst.root.rightChild; |
| 27 | + expect(rootsRightChild.value).toEqual(500); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should add element 10 to the left of root"s left child', () => { |
| 31 | + bst.add(10); |
| 32 | + rootsLeftChildsLeftChild = bst.root.leftChild.leftChild; |
| 33 | + expect(rootsLeftChildsLeftChild.value).toEqual(10); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should add element 30 to the right of root"s left child', () => { |
| 37 | + bst.add(30); |
| 38 | + rootsLeftChildsRightChild = bst.root.leftChild.rightChild; |
| 39 | + expect(rootsLeftChildsRightChild.value).toEqual(30); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should add element 400 to the left of root"s right child', () => { |
| 43 | + bst.add(400); |
| 44 | + rootsRightChildsLeftChild = bst.root.rightChild.leftChild; |
| 45 | + expect(rootsRightChildsLeftChild.value).toEqual(400); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should add element 600 to the right of root"s right child', () => { |
| 49 | + bst.add(600); |
| 50 | + rootsRightChildsRightChild = bst.root.rightChild.rightChild; |
| 51 | + expect(rootsRightChildsRightChild.value).toEqual(600); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('Check insertion was as expected', () => { |
| 56 | + it('Inorder traversal of the created bst should be [ 10, 20, 30, 100, 400, 500, 600 ]', () => { |
| 57 | + expect(bst.traverseInorder()).toEqual([10, 20, 30, 100, 400, 500, 600]); |
| 58 | + }); |
| 59 | + |
| 60 | + it('Preorder traversal of the created bst should be [ 100, 20, 10, 30, 500, 400, 600 ]', () => { |
| 61 | + expect(bst.traversePreorder()).toEqual([100, 20, 10, 30, 500, 400, 600]); |
| 62 | + }); |
| 63 | + |
| 64 | + it('Postorder traversal of the created bst should be [ 10, 30, 20, 400, 600, 500, 100 ]', () => { |
| 65 | + expect(bst.traversePostorder()).toEqual([10, 30, 20, 400, 600, 500, 100]); |
| 66 | + }); |
| 67 | + }); |
| 68 | +}); |
0 commit comments