|
| 1 | +"use strict"; |
| 2 | +var __importStar = (this && this.__importStar) || function (mod) { |
| 3 | + if (mod && mod.__esModule) return mod; |
| 4 | + var result = {}; |
| 5 | + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; |
| 6 | + result["default"] = mod; |
| 7 | + return result; |
| 8 | +}; |
| 9 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 10 | +const BinarySearchTree_1 = __importStar(require("./BinarySearchTree")); |
| 11 | +let BST = new BinarySearchTree_1.default(); |
| 12 | +beforeEach(() => BST = new BinarySearchTree_1.default()); |
| 13 | +describe('basic binary search tree functionality', () => { |
| 14 | + test('Binary search tree exists', () => { |
| 15 | + expect(BST).toBeTruthy(); |
| 16 | + }); |
| 17 | + test('Tree root initiates as null', () => { |
| 18 | + expect(BST.root).toBeNull(); |
| 19 | + }); |
| 20 | + test('Can add new root node to tree', () => { |
| 21 | + var _a; |
| 22 | + const node = new BinarySearchTree_1.BinaryTreeNode(5); |
| 23 | + BST.root = node; |
| 24 | + expect((_a = BST.root) === null || _a === void 0 ? void 0 : _a.value).toBe(5); |
| 25 | + }); |
| 26 | + test('Can add left and right nodes to tree root manually', () => { |
| 27 | + var _a, _b, _c, _d; |
| 28 | + BST.root = new BinarySearchTree_1.BinaryTreeNode(5); |
| 29 | + BST.root.left = new BinarySearchTree_1.BinaryTreeNode(1); |
| 30 | + BST.root.right = new BinarySearchTree_1.BinaryTreeNode(10); |
| 31 | + expect((_b = (_a = BST.root) === null || _a === void 0 ? void 0 : _a.left) === null || _b === void 0 ? void 0 : _b.value).toBe(1); |
| 32 | + expect((_d = (_c = BST.root) === null || _c === void 0 ? void 0 : _c.right) === null || _d === void 0 ? void 0 : _d.value).toBe(10); |
| 33 | + }); |
| 34 | + test('Insert method exists', () => { |
| 35 | + expect(typeof BST.insert).toBe('function'); |
| 36 | + }); |
| 37 | + test('insert method works by inserting in order (left to right ascending)', () => { |
| 38 | + var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s; |
| 39 | + BST.insert(5); |
| 40 | + expect((_a = BST.root) === null || _a === void 0 ? void 0 : _a.value).toBe(5); |
| 41 | + BST.insert(3); |
| 42 | + expect((_c = (_b = BST.root) === null || _b === void 0 ? void 0 : _b.left) === null || _c === void 0 ? void 0 : _c.value).toBe(3); |
| 43 | + BST.insert(7); |
| 44 | + expect((_e = (_d = BST.root) === null || _d === void 0 ? void 0 : _d.right) === null || _e === void 0 ? void 0 : _e.value).toBe(7); |
| 45 | + BST.insert(4); |
| 46 | + expect((_h = (_g = (_f = BST.root) === null || _f === void 0 ? void 0 : _f.left) === null || _g === void 0 ? void 0 : _g.right) === null || _h === void 0 ? void 0 : _h.value).toBe(4); |
| 47 | + BST.insert(6); |
| 48 | + expect((_l = (_k = (_j = BST.root) === null || _j === void 0 ? void 0 : _j.right) === null || _k === void 0 ? void 0 : _k.left) === null || _l === void 0 ? void 0 : _l.value).toBe(6); |
| 49 | + BST.insert(1); |
| 50 | + expect((_p = (_o = (_m = BST.root) === null || _m === void 0 ? void 0 : _m.left) === null || _o === void 0 ? void 0 : _o.left) === null || _p === void 0 ? void 0 : _p.value).toBe(1); |
| 51 | + BST.insert(8); |
| 52 | + expect((_s = (_r = (_q = BST.root) === null || _q === void 0 ? void 0 : _q.right) === null || _r === void 0 ? void 0 : _r.right) === null || _s === void 0 ? void 0 : _s.value).toBe(8); |
| 53 | + }); |
| 54 | +}); |
0 commit comments