/** * File: space_complexity.js * Created Time: 2023-02-05 * Author: Justin (xiefahit@gmail.com) */ const { ListNode } = require('../modules/ListNode'); const { TreeNode } = require('../modules/TreeNode'); const { printTree } = require('../modules/PrintUtil'); /* Function */ function constFunc() { // Perform some operations return 0; } /* Constant order */ function constant(n) { // Constants, variables, objects occupy O(1) space const a = 0; const b = 0; const nums = new Array(10000); const node = new ListNode(0); // Variables in the loop occupy O(1) space for (let i = 0; i < n; i++) { const c = 0; } // Functions in the loop occupy O(1) space for (let i = 0; i < n; i++) { constFunc(); } } /* Linear order */ function linear(n) { // Array of length n uses O(n) space const nums = new Array(n); // A list of length n occupies O(n) space const nodes = []; for (let i = 0; i < n; i++) { nodes.push(new ListNode(i)); } // A hash table of length n occupies O(n) space const map = new Map(); for (let i = 0; i < n; i++) { map.set(i, i.toString()); } } /* Linear order (recursive implementation) */ function linearRecur(n) { console.log(`Recursion n = ${n}`); if (n === 1) return; linearRecur(n - 1); } /* Exponential order */ function quadratic(n) { // Matrix uses O(n^2) space const numMatrix = Array(n) .fill(null) .map(() => Array(n).fill(null)); // 2D list uses O(n^2) space const numList = []; for (let i = 0; i < n; i++) { const tmp = []; for (let j = 0; j < n; j++) { tmp.push(0); } numList.push(tmp); } } /* Quadratic order (recursive implementation) */ function quadraticRecur(n) { if (n <= 0) return 0; const nums = new Array(n); console.log(`In recursion n = ${n}, nums length = ${nums.length}`); return quadraticRecur(n - 1); } /* Driver Code */ function buildTree(n) { if (n === 0) return null; const root = new TreeNode(0); root.left = buildTree(n - 1); root.right = buildTree(n - 1); return root; } /* Driver Code */ const n = 5; // Constant order constant(n); // Linear order linear(n); linearRecur(n); // Exponential order quadratic(n); quadraticRecur(n); // Exponential order const root = buildTree(n); printTree(root);