|
| 1 | +const BinarySearchTree = require('./index'); |
| 2 | + |
| 3 | +describe('Binary Search Tree', () => { |
| 4 | + let bst; |
| 5 | + let rootsLeftChild, rootsRightChild; |
| 6 | + let rootsLeftChildsLeftChild, rootsLeftChildsRightChild; |
| 7 | + let rootsRightChildsLeftChild, rootsRightChildsRightChild; |
| 8 | + |
| 9 | + describe('Creates a binary search tree', () => { |
| 10 | + it('should create a bst with root 100', () => { |
| 11 | + bst = new BinarySearchTree(100); |
| 12 | + expect(bst.root.value).toEqual(100); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should add element 20 to the left of root node', () => { |
| 16 | + bst.add(20); |
| 17 | + rootsLeftChild = bst.root.leftChild; |
| 18 | + expect(rootsLeftChild.value).toEqual(20); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should add element 500 to the right of root node', () => { |
| 22 | + bst.add(500); |
| 23 | + rootsRightChild = bst.root.rightChild; |
| 24 | + expect(rootsRightChild.value).toEqual(500); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should add element 10 to the left of root"s left child', () => { |
| 28 | + bst.add(10); |
| 29 | + rootsLeftChildsLeftChild = bst.root.leftChild.leftChild; |
| 30 | + expect(rootsLeftChildsLeftChild.value).toEqual(10); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should add element 30 to the right of root"s left child', () => { |
| 34 | + bst.add(30); |
| 35 | + rootsLeftChildsRightChild = bst.root.leftChild.rightChild; |
| 36 | + expect(rootsLeftChildsRightChild.value).toEqual(30); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should add element 400 to the left of root"s right child', () => { |
| 40 | + bst.add(400); |
| 41 | + rootsRightChildsLeftChild = bst.root.rightChild.leftChild; |
| 42 | + expect(rootsRightChildsLeftChild.value).toEqual(400); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should add element 600 to the right of root"s right child', () => { |
| 46 | + bst.add(600); |
| 47 | + rootsRightChildsRightChild = bst.root.rightChild.rightChild; |
| 48 | + expect(rootsRightChildsRightChild.value).toEqual(600); |
| 49 | + }); |
| 50 | + }); |
| 51 | +}); |
0 commit comments