Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions .github/PULL_REQUEST_TEMPLATE.md

This file was deleted.

36 changes: 36 additions & 0 deletions .github/스프린트 미션 12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 미션목표

- 자바스크립트로 정렬 알고리즘 구현하기

## 요구사항

다음 정렬 알고리즘을 각각 JavaScript 함수로 구현해 주세요.

선택 정렬 (Selection sort)

- [x] 숫자형 배열을 파라미터로 받고, 해당 배열을 수정하도록 구현합니다.

삽입 정렬 (Insertion sort)

- [x] 숫자형 배열을 파라미터로 받고, 해당 배열을 수정하도록 구현합니다.

병합 정렬 (Merge sort)

- [x] 숫자형 배열을 파라미터로 받고, 정렬된 새로운 배열을 리턴하도록 구현합니다.

퀵 정렬 (Quick sort)

- [x] 숫자형 배열을 파라미터로 받고, 해당 배열을 수정하도록 구현합니다.

## 주요 변경사항

-
-

## 스크린샷

- ![alt text](image.png)

## 멘토에게

- 셀프 코드 리뷰를 통해 질문 이어가겠습니다.
60 changes: 60 additions & 0 deletions .github/스프린트 미션 13.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 미션 목표

- 자바스크립트로 자료 구조 구현하기
- 자바스크립트로 힙 정렬 구현하기

# 요구사항

- 다음 자료 구조를 구현해 algorithm 폴더에 저장해 주세요.

1. 링크드 리스트 (Linked List)
파일 이름: LinkedList.js
클래스 이름: LinkedList
메서드:
addNode(value): 리스트의 끝에 새 노드를 추가
findNode(value): 주어진 값을 가지는 노드를 찾아 리턴
insertAfter(targetValue, newValue): 특정 값을 가진 노드 뒤에 새 노드 추가
removeAfter(targetValue): 특정 값을 가진 노드 뒤의 노드를 삭제

2. 이중 링크드 리스트 (Doubly Linked List)
파일 이름: DoublyLinkedList.js
클래스 이름: DoublyLinkedList
메서드:
addToHead(value): 리스트의 앞쪽에 노드 추가
addToTail(value): 리스트의 뒤쪽에 노드 추가
insertAfter(targetValue, newValue): 특정 값을 가진 노드 뒤에 새 노드 추가
findNode(value): 값을 가진 노드를 찾아 반환합니다.
removeNode(value): 특정 값을 가진 노드 삭제

3. 큐 (Queue)
파일 이름: Queue.js
클래스 이름: Queue
메서드:
enqueue(value): 큐의 맨 뒤에 값을 추가
dequeue(): 큐의 앞에서 값을 제거하고 그 값을 리턴
peek(): 큐의 앞에 있는 값을 제거하지 않고 리턴
isEmpty(): 큐가 비어 있는지 불린형으로 리턴

4. 스택 (Stack)
파일 이름: Stack.js
클래스 이름: Stack
메서드:
push(value): 스택의 맨 위에 값을 추가
pop(): 스택의 맨 위 값을 제거하고 그 값을 리턴
peek(): 스택의 맨 위 값을 제거하지 않고 그 값을 리턴
isEmpty(): 스택이 비어 있는지 불린형으로 리턴

5. 이진 탐색 트리 (Binary Search Tree)
파일 이름: BinarySearchTree.js
클래스 이름: BinarySearchTree
메서드:
insert(value): 트리에 값 추가
find(value): 주어진 값을 찾고 해당 노드를 리턴
remove(value): 트리에서 해당 값을 삭제

- 다음 알고리즘을 JavaScript로 구현해 algorithm
sorts.js 파일에 추가로 작성해 주세요.

6. 힙 정렬
함수 이름: heapsort()
숫자형 배열을 받아서 받은 배열을 정렬된 상태로 수정
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.DS_Store
.AppleDouble
.LSOverride

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*

# Dependency directories
node_modules/

# Build output
dist/
build/

# Test output
coverage/

# Env files
.env
.env.*

# Editor / OS
.vscode/
.idea/

110 changes: 110 additions & 0 deletions BinarySearchTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
class TreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}

class BinarySearchTree {
constructor() {
this.root = null;
}

// 트리에 값 추가
insert(value) {
const newNode = new TreeNode(value);
if (!this.root) {
this.root = newNode;
return;
}
let current = this.root;
while (true) {
if (value < current.value) {
if (!current.left) {
current.left = newNode;
return;
}
current = current.left;
} else if (value > current.value) {
if (!current.right) {
current.right = newNode;
return;
}
current = current.right;
} else {
return; // 중복값 무시
}
}
}

// 주어진 값을 찾고 해당 노드를 리턴
find(value) {
let current = this.root;
while (current) {
if (value === current.value) return current;
if (value < current.value) {
current = current.left;
} else {
current = current.right;
}
}
return null;
}

// 트리에서 해당 값을 삭제
remove(value) {
this.root = this._removeNode(this.root, value);
}

_removeNode(node, value) {
if (!node) return null;

if (value < node.value) {
node.left = this._removeNode(node.left, value);
return node;
} else if (value > node.value) {
node.right = this._removeNode(node.right, value);
return node;
} else {
// 1. 자식이 없는 리프 노드인 경우
if (!node.left && !node.right) return null;

// 2. 자식이 하나인 경우
if (!node.left) return node.right;
if (!node.right) return node.left;

// 3. 자식이 둘인 경우: 우측 서브트리에서 가장 작은 값을 가져옴
let tempNode = this._getMin(node.right);
node.value = tempNode.value;
node.right = this._removeNode(node.right, tempNode.value);
return node;
}
}

_getMin(node) {
let current = node;
while (current.left) {
current = current.left;
}
return current;
}
}

module.exports = BinarySearchTree;

if (require.main === module) {
const bst = new BinarySearchTree();
console.log("find (empty):", bst.find(10));

[5, 3, 7, 2, 4, 6, 8].forEach((v) => bst.insert(v));
console.log("find 6:", bst.find(6)?.value);
console.log("find 9:", bst.find(9));

bst.remove(2); // leaf
console.log("after remove(2), find 2:", bst.find(2));

bst.remove(7); // has two children
console.log("after remove(7), find 7:", bst.find(7));
console.log("root:", bst.root?.value);
}
104 changes: 104 additions & 0 deletions DoublyLinkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
class DoublyNode {
constructor(value) {
this.value = value;
this.prev = null;
this.next = null;
}
}

class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
}

// 리스트의 앞쪽에 노드 추가
addToHead(value) {
const newNode = new DoublyNode(value);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
}
}

// 리스트의 뒤쪽에 노드 추가
addToTail(value) {
const newNode = new DoublyNode(value);
if (!this.tail) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
}
}

// 값을 가진 노드를 찾아 반환
findNode(value) {
let current = this.head;
while (current) {
if (current.value === value) return current;
current = current.next;
}
return null;
}

// 특정 값을 가진 노드 뒤에 새 노드 추가
insertAfter(targetValue, newValue) {
const targetNode = this.findNode(targetValue);
if (targetNode) {
const newNode = new DoublyNode(newValue);
newNode.next = targetNode.next;
newNode.prev = targetNode;

if (targetNode.next) {
targetNode.next.prev = newNode;
} else {
this.tail = newNode; // 마지막 노드였다면 tail 업데이트
}
targetNode.next = newNode;
}
}

// 특정 값을 가진 노드 삭제
removeNode(value) {
const targetNode = this.findNode(value);
if (!targetNode) return;

if (targetNode.prev) {
targetNode.prev.next = targetNode.next;
} else {
this.head = targetNode.next; // 삭제할 노드가 head인 경우
}

if (targetNode.next) {
targetNode.next.prev = targetNode.prev;
} else {
this.tail = targetNode.prev; // 삭제할 노드가 tail인 경우
}
}
}

module.exports = DoublyLinkedList;

if (require.main === module) {
const list = new DoublyLinkedList();
console.log("find (empty):", list.findNode(1));

list.addToHead(2);
list.addToHead(1);
list.addToTail(3);
console.log("head:", list.head?.value, "tail:", list.tail?.value);

list.insertAfter(2, 2.5);
console.log("after insertAfter(2,2.5):", list.findNode(2.5)?.value);

list.removeNode(2.5);
console.log("find 2.5 (removed):", list.findNode(2.5));
console.log("head:", list.head?.value, "tail:", list.tail?.value);
}
Loading