본문 바로가기

자료구조10

[Topic #자료구조 06] 기본 정렬 3가지(선택/버블/삽입) 구현 with JavaScript // Ascending Order Sort // 선택 정렬 // T.C: O(n^2) // S.C: O(n) - B/C make it to a pure function const selectionSort = (array) => { const len = array.length; const copyArr = [...array]; // array.slice() - 얕은 복사 for (let i = 0; i copyArr[j]) { minIdx = j; } } [copyArr[i], copyArr[minIdx]] = [copyArr[minIdx].. 2022. 10. 10.
[Topic #자료구조 05] Recursion 대표 4가지 유형 구현 with JavaScript // 재귀 유형 01 - Factorial // Iterative // T.C: O(n) // S.C: O(1) function factorialIterative(n) { let result = 1; for (let i = 1; i 0 ? n * factorialRecursive02(n - 1) : 1; } function factorialRecursive03(n) { return (n > 0 && n * factorialRecursive03(n - 1)) || 1; } // Simple test cases for (let i = 0; i 2022. 10. 1.
[Topic #자료구조 04] Max/Min heap 구현 with JavaScript // Max Heap class MaxHeap { constructor() { this.array = []; } push(value) { this.array.push(value); this.#upHeap(this.array.length - 1); } pop() { const result = this.array[0]; const value = this.array.pop(); if (this.array.length > 0) { this.array[0] = value; this.#downHeap(0); } return result; } isEmpty() { return this.array.length === 0; } #parentIdx(idx) { // return Math.floor((idx + 1) / 2.. 2022. 9. 26.
[Topic #자료구조 03] Binary Tree with Traverse, DFS/BFS, height 구현 with JavaScript class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } class BinaryTree { constructor() { this.root = null; } // Self, L, R #preOrder(node) { if (node) { process.stdin.write(node.value + ' > '); this.#preOrder(node.left); this.#preOrder(node.right); } } traversePreOrder() { process.stdin.write('PreOrder: '); this.#preOrder(this.root); console.log(); } // L.. 2022. 9. 12.
반응형