|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var mod = require('../../src/data-structures/binary-search-tree.js'); |
| 4 | +var Node = mod.Node; |
| 5 | +var BinaryTree = mod.BinaryTree; |
| 6 | + |
| 7 | +describe('Node', function () { |
| 8 | + it('should be a constructor function', function () { |
| 9 | + expect(typeof Node).toBe('function'); |
| 10 | + }); |
| 11 | +}); |
| 12 | + |
| 13 | +describe('Binary Tree', function () { |
| 14 | + it('should be a constructor function', function () { |
| 15 | + expect(typeof BinaryTree).toBe('function'); |
| 16 | + }); |
| 17 | + it('should start with null root', function () { |
| 18 | + expect(new BinaryTree()._root).toBe(null); |
| 19 | + }); |
| 20 | + it('should insert and remove single node properly', function () { |
| 21 | + var bTree = new BinaryTree(); |
| 22 | + bTree.insert(15); |
| 23 | + var node = bTree.find(15); |
| 24 | + bTree.remove(node); |
| 25 | + expect(bTree._root).toBe(null); |
| 26 | + }); |
| 27 | + it('should insert multiple nodes properly', function () { |
| 28 | + var bTree = new BinaryTree(); |
| 29 | + bTree.insert(10); |
| 30 | + bTree.insert(5); |
| 31 | + bTree.insert(15); |
| 32 | + bTree.insert(4); |
| 33 | + bTree.insert(6); |
| 34 | + bTree.insert(14); |
| 35 | + bTree.insert(16); |
| 36 | + var leftRootChild = bTree._root._left; |
| 37 | + var rightRootChild = bTree._root._right; |
| 38 | + expect(bTree._root.value).toBe(10); |
| 39 | + expect(leftRootChild.value).toBe(5); |
| 40 | + expect(rightRootChild.value).toBe(15); |
| 41 | + expect(leftRootChild._left.value).toBe(4); |
| 42 | + expect(leftRootChild._right.value).toBe(6); |
| 43 | + expect(rightRootChild._left.value).toBe(14); |
| 44 | + expect(rightRootChild._right.value).toBe(16); |
| 45 | + }); |
| 46 | + it('should remove multiple nodes properly', function () { |
| 47 | + var bTree = new BinaryTree(); |
| 48 | + bTree.insert(10); |
| 49 | + bTree.insert(5); |
| 50 | + bTree.insert(15); |
| 51 | + bTree.insert(4); |
| 52 | + bTree.insert(6); |
| 53 | + bTree.insert(7); |
| 54 | + bTree.insert(14); |
| 55 | + bTree.insert(16); |
| 56 | + var leftRootChild = bTree._root._left; |
| 57 | + var rightRootChild = bTree._root._right; |
| 58 | + var sixteen = bTree.find(16); |
| 59 | + bTree.remove(sixteen); |
| 60 | + expect(bTree._root.value).toBe(10); |
| 61 | + expect(leftRootChild.value).toBe(5); |
| 62 | + expect(rightRootChild.value).toBe(15); |
| 63 | + expect(leftRootChild._left.value).toBe(4); |
| 64 | + expect(leftRootChild._right.value).toBe(6); |
| 65 | + expect(leftRootChild._right._right.value).toBe(7); |
| 66 | + expect(rightRootChild._left.value).toBe(14); |
| 67 | + expect(rightRootChild._right).toBe(null); |
| 68 | + var fourteen = bTree.find(14); |
| 69 | + bTree.remove(fourteen); |
| 70 | + expect(rightRootChild._left).toBe(null); |
| 71 | + var five = bTree.find(5); |
| 72 | + bTree.remove(five); |
| 73 | + expect(leftRootChild.value).toBe(6); |
| 74 | + expect(leftRootChild._left.value).toBe(4); |
| 75 | + expect(leftRootChild._right.value).toBe(7); |
| 76 | + }); |
| 77 | +}); |
0 commit comments