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
54 changes: 54 additions & 0 deletions problem-1/SymbolTableWithArray.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,58 @@
class SymbolTable {
#keys = [];
#values = [];
#n = 0;

constructor(maxCount = 10) {
this.#keys = new Array(maxCount);
this.#values = new Array(maxCount);
}

get(key) {
for (let i = 0; i < this.#n; i++) {
if (this.#keys[i] === key) {
return this.#values[i];
}
}
}

put(key, value) {
for (let i = 0; i < this.#n; i++) {
if (this.#keys[i] === key) {
this.#values[i] = value;
return;
}
}

this.#keys[this.#n] = key;
this.#values[this.#n] = value;
this.#n++;
}

delete(key) {
for (let i = 0; i < this.#n; i++) {
if (this.#keys[i] === key) {
for (let j = i; j < this.#n - 1; j++) {
this.#keys[j] = this.#keys[j + 1];
this.#values[j] = this.#values[j + 1];
}
this.#n--;
return;
}
}
}

size() {
return this.#n;
}

isEmpty() {
return this.#n === 0;
}

contains(key) {
return !!this.get(key);
}
}

module.exports = {
Expand Down
42 changes: 41 additions & 1 deletion problem-2/SymbolTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class SymbolTable {
const mid = start + Math.floor((end - start) / 2);
if (key < this.#keys[mid]) {
return this.rank(key, start, mid - 1);
} if (key > this.#keys[mid]) {
}
if (key > this.#keys[mid]) {
return this.rank(key, mid + 1, end);
}
return mid;
Expand Down Expand Up @@ -106,15 +107,54 @@ class SymbolTable {
}

contains(key) {
return !!this.get(key);
}

floor(key) {
if (this.isEmpty()) {
return;
}

const i = this.rank(key);
if (i === 0) {
return this.#keys[i] === key ? key : undefined;
}

if (this.#keys[i] === key) {
return key;
}

return this.#keys[i - 1];
}

ceiling(key) {
if (this.isEmpty()) {
return;
}

const i = this.rank(key);
if (i >= this.#n) {
return;
}

return this.#keys[i];
}

keysRange(start, end) {
const startIndex = this.rank(start);
const endIndex = this.rank(end);

const arr = [];

for (let i = startIndex; i < endIndex; i++) {
arr.push(this.#keys[i]);
}

if (this.#keys[endIndex] === end) {
arr.push(end);
}

return arr;
}
}

Expand Down
2 changes: 2 additions & 0 deletions problem-3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ O: 10
N: 11
```

![이진탐색트리그림](./st-image.jpeg)

2. 트리의 높이를 구하는 height를 구현해 주세요. 이때 2가지 버전의 구현을 만들어
주세요. 첫 번째는 재귀로 실행할 때마다 새로 계산해서 높이를 구하는 버전을
만들어 주세요. 두 번째는 `size()`와 비슷하게 각 노드에 트리의 높이를 담는
Expand Down
34 changes: 30 additions & 4 deletions problem-3/SymbolTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ class Node {

n;

constructor(key, value, n) {
height;

constructor(key, value, n, height = 0) {
this.key = key;
this.value = value;
this.n = n;
this.height = height;
}
}

Expand Down Expand Up @@ -46,7 +49,8 @@ class SymbolTable {

if (key < node.key) {
return this.#get(node.left, key);
} if (key > node.key) {
}
if (key > node.key) {
return this.#get(node.right, key);
}
return node.value;
Expand All @@ -67,9 +71,13 @@ class SymbolTable {
node.right = this.#put(node.right, key, value);
} else {
node.value = value;
// 이 상황에서는 서브트리의 사이즈가 변하지 않기 때문에 return 처리를 해도 괜찮지 않을까 ?
return node;
}

node.n = this.#size(node.left) + this.#size(node.right) + 1;
node.height =
1 + Math.max(this.#height(node.left), this.#height(node.right));
return node;
}

Expand Down Expand Up @@ -143,7 +151,8 @@ class SymbolTable {
const t = this.#size(node.left);
if (t > k) {
return this.#select(node.left, k);
} if (t < k) {
}
if (t < k) {
return this.#select(node.right, k - t - 1);
}
return node;
Expand All @@ -160,7 +169,8 @@ class SymbolTable {

if (key < node.key) {
return this.#rank(node.left, key);
} if (key > node.key) {
}
if (key > node.key) {
return 1 + this.#size(node.left) + this.#rank(node.right, key);
}
return this.#size(node.left);
Expand All @@ -181,6 +191,8 @@ class SymbolTable {

node.left = this.#deleteMin(node.left);
node.n = this.#size(node.left) + this.#size(node.right) + 1;
node.height =
1 + Math.max(this.#height(node.left), this.#height(node.right));
return node;
}

Expand Down Expand Up @@ -213,6 +225,8 @@ class SymbolTable {
}

node.n = this.#size(node.left) + this.#size(node.right) + 1;
node.height =
1 + Math.max(this.#height(node.left), this.#height(node.right));
return node;
}

Expand Down Expand Up @@ -269,6 +283,18 @@ class SymbolTable {

return this.#max(node.right);
}

height() {
return this.#height(this.#root);
}

#height(node) {
if (node === undefined) {
return -1;
}

return node.height;
}
}

module.exports = {
Expand Down
5 changes: 3 additions & 2 deletions problem-3/problem-3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,12 @@ describe('height', () => {
expect(st.height()).toBe(1);

st.put('H', 4);
st.put('H', 10);

expect(st.height()).toBe(2);

st.put('Z', 5);
st.delete('Z');

expect(st.height()).toBe(3);
expect(st.height()).toBe(2);
});
});
Binary file added problem-3/st-image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions problem-4/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ E A S Y Q U T I O N

2. 이번에는 위의 키 목록을 레드 블랙 트리에 순서대로 추가했을 때 만들어지는 레드
블랙 트리를 그려주세요.

![tree](./tree.jpeg)
Binary file added problem-4/tree.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-5/LinearProbingHashTable.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 83 additions & 2 deletions problem-5/LinearProbingHashTable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,106 @@ class LinearProbingHashTable {
}

hash(key) {
return this.#hash(key)
return this.#hash(key);
}

get(key) {
for (
let i = this.#hash(key);
this.#keys[i] !== undefined;
i = (i + 1) % this.#M
) {
if (this.#keys[i] === key) {
return this.#values[i];
}
}
}

put(key, value) {
if (this.#N >= this.#M / 2) {
this.#resize(2 * this.#M);
}

let i;
for (
i = this.#hash(key);
this.#keys[i] !== undefined;
i = (i + 1) % this.#M
) {
if (this.#keys[i] === key) {
this.#values[i] = value;
return;
}
}

this.#keys[i] = key;
this.#values[i] = value;
this.#N++;
}

delete(key) {
if (!this.contains(key)) {
return;
}

let i = this.#hash(key);
while (key !== this.#keys[i]) {
i = (i + 1) % this.#M;
}

this.#keys[i] = undefined;
this.#values[i] = undefined;

i = (i + 1) % this.#M;
while (this.#keys[i] !== undefined) {
const keyToRedo = this.#keys[i];
const valueToRedo = this.#values[i];

this.#keys[i] = undefined;
this.#values[i] = undefined;

this.#N--;

this.put(keyToRedo, valueToRedo);

i = (i + 1) % this.#M;
}

this.#N--;

if (this.#N > 0 && this.#N === this.#M / 8) {
this.#resize(this.#M / 2);
}
}

contains(key) {
return this.get(key) !== undefined;
}

keys() {
const keys = [];

for (let i = 0; i < this.#M; i++) {
if (this.#keys[i] !== undefined) {
keys.push(this.#keys[i]);
}
}

return keys;
}

#resize(capacity) {
const temp = new LinearProbingHashTable(capacity);

for (let i = 0; i < this.#M; i++) {
if (this.#keys[i] !== undefined) {
temp.put(this.#keys[i], this.#values[i]);
}
}

this.#keys = temp.#keys;
this.#values = temp.#values;
this.#M = temp.#M;
}
}

Expand All @@ -53,7 +134,7 @@ const randomString = (max) => {
}

return result;
}
};

test('이미 있는 키 값에 값을 추가하면 이전 값을 덮어쓴다', () => {
const st = new LinearProbingHashTable();
Expand Down
4 changes: 4 additions & 0 deletions problem-5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
```
E A S Y Q U T I O N
```
![개별 체이닝 해시 테이블](./SeperateChainingHashTable.jpeg)


2. 선형 탐지 해싱 테이블에 위의 주어진 키를 순서대로 삽입했을 때 테이블의 내부
상태를 그림으로 그려 주세요. 초기 상태는 비어 있고 M=5라고 가정합니다. 삽입할
때 공간이 부족하다면 2배씩 테이블을 증가시킵니다. 마찬가지로 키의 알파벳
순서를 k라 할 때 해시 함수 11k % M을 이용하여 테이블의 인덱스로 변환해
주세요.

![선형 탐지 해시 테이블](./LinearProbingHashTable.jpeg)

3. 테스트 요구사항에 맞는 개별 체이닝 해시 테이블과 선형 탐지 해싱 테이블을
구현해 주세요.
Binary file added problem-5/SeperateChainingHashTable.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading