
트리의 특징

트리의 구현 방법

이진 트리의 구현 방법

-> 구현할 이진 트리
이진 트리를 배열(Array)로 구현
// 0번 인덱스는 편의를 위해 비워둠
// Left = Index*2
// Right = Index*2 + 1
// Parent = floor(Index / 2)
const tree = [
undefined,
// 1
9,
// 1*2, 1*2+1
3, 8,
//2*2, 2*2+1, 3*2, 3*2+1
2, 5, undefined, 7,
//4*2, 4*2+1, 5*2, 5*2+1
undefined, undefined, undefined, 4
];
이진 트리를 연결 리스트(Linked List)로 구현
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class Tree {
constructor(node) {
this.root = node;
}
display() {
// Level Order
const queue = new Queue();
queue.enqueue(this.root);
while(queue.size) {
const currentNode = queue.dequeue();
console.log(currentNode.value);
if(currentNode.left) queue.enqueue(currentNode.left);
if(currentNode.right) queue.enqueue(currentNode.right);
}
}
}
const tree = new Tree(new Node(9));
tree.root.left = new Node(3);
tree.root.right = new Node(8);
tree.root.left.left = new Node(2);
tree.root.left.right = new Node(5);
tree.root.right.right = new Node(7);
tree.root.left.right.right = new Node(4);
우선순위 큐

힙(Heap) 요소 추가
힙 요소 추가 알고리즘



힙(Heap) 요소 제거
힙 요소 제거 알고리즘


힙 요소 추가
class MaxHeap {
constructor() {
this.heap = [null];
}
push(value) {
this.heap.push(value);
let currentIndex = this.heap.length - 1;
let parentIndex = Math.floor(currentIndex / 2);
while (parentIndex !== 0 && this.heap[parentIndex] < value) {
const temp = this.heap[parentIndex];
this.heap[parentIndex] = value;
this.heap[currentIndex] = temp;
currentIndex = parentIndex;
parentIndex = Math.floor(currentIndex / 2);
}
}
}
const heap = new MaxHeap();
heap.push(45);
heap.push(36);
heap.push(54);
heap.push(27);
heap.push(63);
console.log(heap.heap);
// Result : [ null, 63, 54, 45, 27, 36 ]
힙 요소 제거
class MaxHeap {
constructor() {
this.heap = [null];
}
push(value) {
this.heap.push(value);
let currentIndex = this.heap.length - 1;
let parentIndex = Math.floor(currentIndex / 2);
while (parentIndex !== 0 && this.heap[parentIndex] < value) {
const temp = this.heap[parentIndex];
this.heap[parentIndex] = value;
this.heap[currentIndex] = temp;
currentIndex = parentIndex;
parentIndex = Math.floor(currentIndex / 2);
}
}
pop() {
const returnValue = this.heap[1];
this.heap[1] = this.heap.pop();
let currentIndex = 1;
let leftIndex = 2;
let rightIndex = 3;
while (
this.heap[currentIndex] < this.heap[leftIndex] ||
this.heap[currentIndex] < this.heap[rightIndex]
) {
if (this.heap[leftIndex] < this.heap[rightIndex]) {
const temp = this.heap[currentIndex];
this.heap[currentIndex] = this.heap[rightIndex];
this.heap[rightIndex] = temp;
currentIndex = rightIndex;
} else {
const temp = this.heap[currentIndex];
this.heap[currentIndex] = this.heap[leftIndex];
this.heap[leftIndex] = temp;
currentIndex = leftIndex;
}
leftIndex = currentIndex * 2;
rightIndex = currentIndex * 2 + 1;
}
return returnValue;
}
}
const heap = new MaxHeap();
heap.push(45);
heap.push(36);
heap.push(54);
heap.push(27);
heap.push(63);
console.log(heap.heap);
// Result : [ null, 63, 54, 45, 27, 36 ]
const array = [];
array.push(heap.pop());
array.push(heap.pop());
array.push(heap.pop());
array.push(heap.pop());
array.push(heap.pop());
console.log(array);
//Result : [ 63, 54, 45, 36, 27 ] - Heap Sort!

트라이(Trie) 생성하기
트라이 구조




트라이 구성
class Node {
constructor(value = "") {
this.value = value;
this.children = new Map();
}
}
class Trie{
constructor() {
this.root = new Node();
}
insert(string) {
let currentNode = this.root;
for(const char of string) {
if(!currentNode.children.has(char)) {
currentNode.children.set(
char,
new Node(currentNode.value + char)
);
}
currentNode = currentNode.children.get(char);
}
}
has(string) {
let currentNode = this.root;
for(const char of string) {
if(!currentNode.children.has(char)) {
return false;
}
currentNode = currentNode.children.get(char);
}
return true;
}
}
const trie = new Trie();
trie.insert("cat");
trie.insert("can");
console.log(trie.has("cat")); // true
console.log(trie.has("can")); // true
console.log(trie.has("cap")); // false

버블 정렬

선택 정렬

삽입 정렬

분할 정복

합병 정렬

큌 정렬

sort
const array = [5, 9, 10, 3, 8, 3, 2];
// 다음과 같이 그냥 정렬하면 ASCII 문자 순서대로 정렬되어
// 우리가 원하는 숫자 크기대로 정렬되지 않음
array.sort();
console.log(array); // [ 10, 2, 3, 3, 5, 8, 9 ]
// 10이 먼저 나오는 이유는 ASCII 문자 '1'이 '2'보다 작기 때문
array.sort((a, b) => a-b); // 오름차순 정렬
console.log(array); // [ 2, 3, 3, 5, 8, 9, 10 ]
array.sort((b, a) => a-b); // 내림차순 정렬
console.log(array); // [ 10, 9, 8, 5, 3, 3, 2 ]
선형 탐색

이진 탐색

배열을 이용한 구현 방법

이진 탐색 트리를 이용한 구현 방법
이진 탐색 트리

이진 탐색 트리 요소 추가


이진 탐색 트리 요소 삭제
단말 정점을 삭제하는 경우

하나의 서브 트리를 가지는 경우

두 개의 서브 트리를 가지는 경우

이진 탐색 트리의 문제점

최악의 경우 한쪽으로 편향된 트리가 될 수 있음
그런 경우 순차 탐색과 동일한 시간복잡도를 가짐
이를 해결하기 위해 다음과 같은 자료구조를 이용할 수 있음
Array
const array = [1, 1, 5, 124, 400, 599, 1004, 2876, 8712];
function binarySearch(array, findValue) {
let left = 0;
let right = array.length - 1;
let mid = Math.floor((left + right) / 2);
while (left < right) {
if(array[mid] === findValue) {
return mid;
}
if(array[mid] < findValue) {
left = mid + 1;
} else {
right = mid - 1;
}
mid = Math.floor((left + right) / 2);
}
return -1;
}
console.log(binarySearch(array, 2876)); // 7
console.log(binarySearch(array, 1)); // 1
console.log(binarySearch(array, 500)); // -1
Binary Search Tree
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinarySerachTree {
constructor() {
this.root = null;
}
insert(value) {
const newNode = new Node(value);
if(this.root === null) {
this.root = newNode;
return;
}
let currentNode = this.root;
while (currentNode !== null) {
if(currentNode.value < value) {
if(currentNode.right === null) {
currentNode.right = newNode;
break;
}
currentNode = currentNode.right;
} else {
if(currentNode.left === null) {
currentNode.left = newNode;
break;
}
currentNode = currentNode.left;
}
}
}
has(value) {
let currentNode = this.root;
while(currentNode !== null) {
if(currentNode.value === value) {
return true;
}
if(currentNode.value < value) {
currentNode = currentNode.right;
} else {
currentNode = currentNode.left;
}
}
return false;
}
}
const tree = new BinarySerachTree();
tree.insert(5);
tree.insert(4);
tree.insert(7);
tree.insert(8);
tree.insert(5);
tree.insert(6);
tree.insert(2);
console.log(tree.has(8)); // true
console.log(tree.has(1)); // false