본문 바로가기
728x90
반응형

JavaScript186

이진 탐색 트리(Binary Search Trees) References 아래 링크의 강의 중 Section 27. My Best Friend, Binary Search Trees의 내용을 추려 이번 글을 작성하였습니다. The Coding Interview Bootcamp: Algorithms + Data Structures on Udemy Binary Search Trees class Node { constructor(data) { this.data = data; this.left = null; this.right = null; } insert(data) { // 입력값 data가 this.data보다 작고 this.left에 값이 이미 할당되어 있다면 this.left에 입력값 data을 넣고, this.left가 없는데 입력값 data가 this.d.. 2022. 4. 6.
함수 장인이 되는 길_함수 호출에 대한 이해 from Secrets of the JavaScript Ninja 이번 글은 'Secrets of the JavaScript Ninja'의 'Chapter 4. Funtions for the journeyman: understanding function invocation'를 바탕으로 작성하였습니다. 핵심 Concepts function의 내재된 parameter 2가지: argumnets & this 함수 호출 방법 function contexts에 생긴 문제 해결 사전 지식 체크! this parameter가 function context로 알려진 이유? function과 method의 차이? constructor function이 명확하게(explicitly) object를 반환하면 생기는 일? 들어가며 이번 글에서는 내재적인(implicit) function p.. 2022. 4. 6.
트리의 너비(Tree Width) References 아래 링크의 강의 중 Section 26. Tree Width with Level Width의 내용을 추려 이번 글을 작성하였습니다. The Coding Interview Bootcamp: Algorithms + Data Structures on Udemy Tree Width function levelWidth(root) { const arr = [root, "s"]; const widths = [0]; while (arr.length > 1) { const node = arr.shift(); if (node === "s") { widths.push(0); arr.push("s"); } else { arr.push(...node.children); widths[widths.length.. 2022. 4. 5.
트리(Tree) References 아래 링크의 강의 중 Section 25. Building a Tree의 내용을 추려 이번 글을 작성하였습니다. The Coding Interview Bootcamp: Algorithms + Data Structures on Udemy Node class Node { constructor(data) { this.data = data; this.children = []; } // node의 입력값 data를 배열로 children에 push add(data) { const node = new Node(data); this.children.push(node); } // filter() method로써 특정값을 제외한 배열을 반환 remove(data) { this.children = th.. 2022. 4. 5.
끝에서부터 노드 세기(From the Tail) References 아래 링크의 강의 중 Section 24. Step Back From the Tail의 내용을 추려 이번 글을 작성하였습니다. The Coding Interview Bootcamp: Algorithms + Data Structures on Udemy Solution function fromLast(list, n) { let slow = list.getFirst(); let fast = list.getFirst(); while (n > 0) { fast = fast.next; n--; } while (fast.next) { slow = slow.next; fast = fast.next; } return slow; } 눈여겨봐야 할 부분은 두 개의 while문의 기능이다. 첫 번째 whi.. 2022. 4. 5.
순환 리스트(Circular List) References 아래 링크의 강의 중 Section 23. Circular Lists?의 내용을 추려 이번 글을 작성하였습니다. The Coding Interview Bootcamp: Algorithms + Data Structures on Udemy Solution function circular(list) { let slow = list.getFirst(); // = list.head; let fast = list.getFirst(); while (fast.next && fast.next.next) { slow = slow.next; fast = fast.next.next; if (slow === fast) { return true; } } return false; } // --- Examples.. 2022. 4. 5.
중간 노드 찾기(Find the Middle Node) References 아래 링크의 강의 중 Section 22. Find the Midpoint의 내용을 추려 이번 글을 작성하였습니다. The Coding Interview Bootcamp: Algorithms + Data Structures on Udemy Solution function midpoint(list) { let slow = list.getFirst(); let fast = list.getFirst(); while (fast.next && fast.next.next) { slow = slow.next; fast = fast.next.next; } return slow; }linked list를 한 칸씩 탐색하는 변수 slow와 두 칸씩 탐색하는 변수 fast를 각각 선언한다. fast로 탐.. 2022. 4. 5.
연결 리스트(Linked Lists) References 아래 링크의 강의 중 Section 21. Linked Lists의 내용을 추려 이번 글을 작성하였습니다. The Coding Interview Bootcamp: Algorithms + Data Structures on Udemy Solution class Node { // node에다 새로운 instance 생설할 때마다 constructor가 자동으로 실행됨. // argument next의 기본값은 null로 설정. tail node에는 next가 없기 때문. constructor(data, next = null) { this.data = data; this.next = next; } } class LinkedList { constructor() { this.head = null.. 2022. 4. 5.
스택으로 큐 만들기(Create Queue with Stacks) References 아래 링크의 강의 중 Section 20. Two Become One의 내용을 추려 이번 글을 작성하였습니다. The Coding Interview Bootcamp: Algorithms + Data Structures on Udemy Solution const Stack = require("./stack"); class Queue { // whenever you create an instance of a queue, you will automatically generate two stacks and assign it to this queue class. constructor() { this.first = new Stack(); this.second = new Stack(); } add.. 2022. 4. 5.
728x90
반응형