Data Structures & Algorithms with Javascript : Stacks
2024.07.18 - [๐ค data structures & algorithms] - Data Structures & Algorithms with Javascript : Lists2024.07.17 - [๐ค data structures & algorithms] - Data Structures & Algorithms with Javascript : Arrays2024.07.17 - [๐ค data structures & algorithms]
pyotato-dev.tistory.com
๐ฉ๐ป๐ป ์ ๋ฒ ํฌ์คํ ์์ ์คํ ์๋ฃ ๊ตฌ์กฐ์ ๋ํด ์ดํด๋ดค๋ค.
๋ฐ์ดํฐ๋ฅผ ์์๋๋ก ์ฝ์ ํ๋ push์ฐ์ฐ, ๊ฐ์ฅ ์ต๊ทผ์ ๋ฐ์ดํฐ๋ฅผ ๋ฆฌ์คํธ์์ ๊บผ๋ด์ ์ ๊ฑฐํ๋ pop์ฐ์ฐ,
๊ทธ๋ฆฌ๊ณ ๊ฐ์ฅ ์ต๊ทผ์ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ธฐ๋ง ํ๋ peek ์ฐ์ฐ์ class๋ก ๊ตฌํํด ๋ณด๊ณ ,
์คํ์ ํ์ฉํด ํฐ๋ฆฐ๋๋กฌ, ์ฌ๊ท ๋์ ํ๋ด๋ด๊ธฐ, ์ง์ ๋ณ๊ฒฝ ์๊ณ ๋ฆฌ์ฆ์ ๋ง๋ค์ด๋ดค๋ค.
์ด๋ฒ ์๊ฐ์๋ LIFO ๋ฐฉ์๊ณผ๋ ๋ฐ๋์ธ FIFO (first-in-first-out) ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๋ queue๊ฐ ๋ฌด์์ธ์ง, ํ๋ฅผ ํ์ฉํ ๋ฐ์ดํฐ ์ ๋ ฌ(radix sort), ์ฐ์ ์์ ํ์ ๋ํด ์์๋ณด์.
๐ซ Queue ์ฐ์ฐ
ํ ์๋ฃ ๊ตฌ์กฐ๋ ๋๊ธฐ์ค๊ณผ ๊ฐ์ ํํ๋ค. ์ค์ ์๋ ค๋ฉด ๋งจ ๋ค์ ์ ์๊ณ ,
์ ์ฅ ๋ฐ ์๋ฒ์ด ๋๋ฉด ๋งจ ์์์๋ถํฐ ์ค์ ์ฐ๋ ์ฌ๋๋ค์ด ๋น ์ง๋ ๊ฒ์ฒ๋ผํ๋ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๋์ํ๋ค.
ํ์ ์ฃผ์ ์ฐ์ฐ์๋ ์๋ก์ด ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ํ๋ "enqueue"์ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๋ "dequeue"๊ฐ ์๋ค.
"enqueue"๋ ์ ๋ฐ์ดํฐ๋ฅผ queue์ ๋งจ ๋์ ์ถ๊ฐํ๋ ์ฐ์ฐ์ด๊ณ , "dequeue"๋ ๋งจ ์์ ๋ฐ์ดํฐ๋ฅผ ์ ๊ฑฐํ๋ ๊ฒ์ด๋ค.
์ด์ stack์์ "peek" ์ฐ์ฐ์ ๋งจ ๋ง์ง๋ง ๋ฐ์ดํฐ๋ฅผ ํ์ธํ๋ ์ฐ์ฐ์ด๋ผ๊ณ ํ๋ค.
ํ์์ peek ์ฐ์ฐ์ ๋งจ ์์ ๋ฐ์ดํฐ๋ฅผ ํ์ธํ๋ ์ฐ์ฐ์ด๋ค.
๐ฉ๐ป๐ป array ๊ธฐ๋ฐ ํด๋์ค ํ ๊ตฌํ
โผ ์์ ๋ ์ฑ ์ ๊ตฌํ์ ์ฐธ๊ณ ํ์ฌ ํด๋์ค๋ก ์ฌ๊ตฌ์ฑํ์ต๋๋ค!!
class Queue{
constructor(){
this.dataStore = [];
}
// ํ์ ๋ฐ์ดํฐ ์ฝ์
enqueue(element){
this.dataStore.push(element);
}
// ํ์ ๋งจ ์ฒซ ์์ดํ
์ ๊ฑฐ
dequeue(){
return this.dataStore.shift();
}
// ๋งจ ์ ์์ ํ์ธ (peek์ฐ์ฐ)
front(){
return this.dataStore[0];
}
// ๋งจ ๋ค ์์ ํ์ธ
back(){
return this.dataStore[this.dataStore.length - 1];
}
// ํ ์์ดํ
๋ฌธ์์ด๋ก ๋ฐํ
toString(){
let stringifiedQueue = '';
for(let i=0; i< this.dataStore.length; i++){
stringifiedQueue+=this.dataStore[i] +'\n';
}
return stringifiedQueue;
}
// ํ๊ฐ ๋น์๋ ์ง ํ์ธ
empty(){
return this.dataStore.length===0
}
}
// ์ฝ์๋ก ์ถ๋ ฅํด์ฃผ๋ ์ ํธํจ์
const print = (item) => console.log(item);
const queue = new Queue();
queue.enqueue('a'); // ํ ์ํ : ['a']
queue.enqueue('b'); // ํ ์ํ : ['a','b']
queue.enqueue('c'); // ํ ์ํ : ['a','b','c']
print(queue.toString());
/**
a
b
c
*/
print(queue.dequeue()); // b ํ ์ํ : ['b','c']
print(queue.toString());
/**
b
c
*/
print(queue.front()); // b
print(queue.back()); // c
๐ฉ๐ป๐ป ํ๋ก ๋ฐ์ดํฐ ์ ๋ ฌ : radix sort
๊ณผ๊ฑฐ ์ปดํจํฐ์ ์ญ์ฌ๋ฅผ ์ดํด๋ณด์๋ฉด, ํ๋ก๊ทธ๋จ์ mainframe ํ๋ก๊ทธ๋จ์ ํ์น์นด๋๋ก ์ ๋ ฅํ์๋ค.
๊ฐ๊ฐ์ ์นด๋๊ฐ ํ๋์ ํ๋ก๊ทธ๋จ ๋ช ๋ น์ ์ง๋๊ณ ์์๋ค. ์ด ์นด๋๋ค์ bin๊ฐ์ ๊ตฌ์กฐ๋ฌผ์ด ์ ๋ ฌํด ์ฃผ๋ ๊ธฐ๊ณ๋ฅผ ํตํด ์ ๋ ฌ๋์๋ค.
์ด ์ ๋ ฌ ๋์์ ํ๋ฅผ ํ์ฉํด ๋ชจ๋ฐฉํ ์ ์๋ค. ์ด ์ ๋ ฌ ๋ฐฉ์์ radix sort๋ผ๊ณ ํ๋ค.
radix sort์ ์๋ฅผ ์ดํด๋ณด์.
๋ค์๊ณผ ๊ฐ์ด 0 ~ 99 ์ฌ์ด์ ์ซ์ ์ธํธ๊ฐ ์๋ค๊ณ ํ์.
91, 46, 85, 15, 92, 35, 31, 22
๋จผ์ 1์ ์๋ฆฌ๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ์ ํด์ฃผ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
Bin 0 :
Bin 1 : 91, 31
Bin 2 : 92, 22
Bin 3 :
Bin 4 :
Bin 5 : 85, 15, 35
Bin 6 : 46
Bin 7 :
Bin 8 :
Bin 9 :
์ฒซ๋ฒ์งธ ์ ๋ ฌ์ ํตํด ์ซ์ ์ธํธ๋ ๋ค์๊ณผ ๊ฐ์ด ์ ๋ ฌ๋๋ค.
91, 31, 92, 22, 85, 15, 35, 46
์ด์ 10์ ์๋ฆฌ๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ์ ํด์ฃผ์.
Bin 0 :
Bin 1 : 15
Bin 2 : 22
Bin 3 : 31, 35
Bin 4 : 46
Bin 5 :
Bin 6 :
Bin 7 :
Bin 8 : 85
Bin 9 : 91, 92
๋ ๋ฒ์งธ ์ ๋ ฌ์ ํตํด ์ซ์ ์ธํธ๋ ๋ค์๊ณผ ๊ฐ์ด ์ ๋ ฌ๋๋ค.
15, 22, 31, 35, 46, 85, 91, 92
์ด ์๊ณ ๋ฆฌ์ฆ์ ํ๋ก ๊ตฌํํด ๋ณด์.
๋ ผ๋ฆฌ ํ๋ฆ
1. ๊ฐ๊ฐ์ bin์ ํ๋ก ๋ํ๋ด๊ธฐ ์ํด์๋ 9๊ฐ์ ํ๊ฐ ํ์ํ๋ค.
2. modulus์ ์ ์ ๋๋์ ์ ํตํด 1๊ณผ 10์ ์๋ฆฟ์๋ฅผ ์ ํ ์ ์๋ค.
3. ์๋ฆฟ์์ ๋ฐ๋ผ์ ํด๋น ํ์ ์ซ์๋ฅผ ์ถ๊ฐํด ์ค๋ค.
4. 1๊ณผ 10์ผ๋ก ์ ๋ ฌํด ์ค ํ๋ค์ ์์๋๋ก ๋นผ์ ํ๋์ ๋ฌธ์์ด๋ก ์ด์ด์ค๋ค.'
๊ตฌํ
โผ ์์ ๋ ์ฑ ์ ๊ตฌํ์ ์ฐธ๊ณ ํ์ฌ ๋ณ๊ฒฝ์ฌํญ์ด ์์ต๋๋ค!!
const TEN = 10;
const TEST_CASE_COUNT = 3;
// digit์ 1 ๋๋ 10์ ์๋ฆฟ์๋ก ๋ถ๋ฐฐ
function distribute(nums, queues, digit){
for(let i= 0; i < TEN; ++i){
digit === 1? queues[nums[i] % TEN].enqueue(nums[i]) : queues[Math.floor(nums[i] / TEN)].enqueue(nums[i])
}
}
// 0-9๊น์ง์ ํ์์ ์ซ์ ๊ฐ์ ธ์ค๊ธฐ
function collect(queues, nums){
let i = 0;
for(let digit = 0; digit < TEN; ++digit){
while(!queues[digit].empty()){
nums[i++] = queues[digit].dequeue();
}
}
}
// ๊ณต๋ฐฑ์ผ๋ก ์ด์ด์ค ์ ๋ ฌ๋ ํ๋ค
function displayArray(arr){
return arr.join(' ');
}
// ์ฌ์ฉ
// ์๋ฆฟ์ ๋ณ 0 - 9์ ํ bin
const queues = [];
for(let i = 0; i < TEN; ++i){
queues[i] = new Queue();
}
// ์ ๋ ฌํด์ค ์ซ์ ํ
์คํธ ์ผ์ด์ค
let testCase = Array.from({length: TEST_CASE_COUNT}, ()=>{
let nums = [];
for(let i = 0; i < TEN; ++i){
nums[i] = Math.floor(Math.floor(Math.random()*101));
}
return nums;
});
for(let i = 0; i < TEST_CASE_COUNT; i++){
print(`test case ${i+1} ์ ๋ ฌ ์ : ${displayArray(testCase[i])}`);
distribute(testCase[i], queues, 1); // 1์ ์๋ฆฟ์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
collect(queues, testCase[i]);
distribute(testCase[i], queues, 10) // 10์ ์๋ฆฟ์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
collect(queues, testCase[i]);
print(`test case ${i+1} ์ ๋ ฌ ํ: ${displayArray(testCase[i])}`);
print('\n');
}
/**
VM1137:40 test case 1 ์ ๋ ฌ ์ : 20 92 26 13 3 66 24 27 81 30
VM1137:40 test case 1 ์ ๋ ฌ ํ: 3 13 20 24 26 27 30 66 81 92
VM1137:40 test case 2 ์ ๋ ฌ ์ : 28 86 46 14 52 97 98 55 72 35
VM1137:40 test case 2 ์ ๋ ฌ ํ: 14 28 35 46 52 55 72 86 97 98
VM1137:40 test case 3 ์ ๋ ฌ ์ : 16 96 44 37 5 36 32 17 62 11
VM1137:40 test case 3 ์ ๋ ฌ ํ: 5 11 16 17 32 36 37 44 62 96
*/
๐ฉ๐ป๐ป ์ฐ์ ์์ ํ (priority queue)
์ผ๋ฐ์ ์ธ ํ ์ฐ์ฐ์์๋ dequeue์ฐ์ฐ์ ๋ฌด์กฐ๊ฑด ๋งจ ์ฒ์ ํ์ ์ถ๊ฐ๋ ์๋ฆฌ๋จผํธ๊ฐ ์ ๊ฑฐ๋๋ค.
ํ์ง๋ง ๋์ ๋ฐ๋ผ FIFO๋ฐฉ์์ด ์๋ ๋ค๋ฅธ ๊ธฐ์ค์ผ๋ก ์์๋ฅผ dequeue ํ๊ณ ์ถ์ ์๊ฐ ์๋ค.
์ด๋ ์ฌ์ฉํ ์ ์๋ ์๋ฃ ๊ตฌ์กฐ๊ฐ ์ฐ์ ์์ ํ๋ค.
์ฐ์ ์์ ํ๋ ๋ง ๊ทธ๋๋ก ํน์ ์ฐ์ ์์์ ๋ฐ๋ผ ์์๋ฅผ ์ ๊ฑฐํ๋ ๊ฒ์ด๋ค.
์ฐ์ ์์ ํ์ ๋์์ ์๊ธ์ค์์ ์๊ธ์ฒ์น๊ฐ ์ด๋ฃจ์ด์ง๋ ์์์ ๋น์ทํ๋ค.
์๊ธ์ค์์ ํ์๊ฐ ์ฒ์น๋๋ ์์๋ ๋จผ์ ์ค๋ ํ์ ์์ด ์๋๋ผ, ๊ธด๊ธ๋๊ฐ ๋์ ์์ผ๋ก ์ด๋ฃจ์ด์ง๋ ๊ฒ์ฒ๋ผ ๋์ํ๋ค.
ํ์์ ์ํ์ ๋ฐ๋ผ ์ฐ์ ์์๋ฅผ ๋ถ์ฌํ๋ ์์ ๋ฅผ ๊ตฌํํด ๋ณด์.
ํ์์ ์๊ธ ์ฝ๋๋ฅผ ๋ํ๋ด๊ธฐ ์ํด ์ด์ queue์ ์ฝ๋๋ฅผ ์์ ํด ๋ณด์.
class Patient{
constructor(name, code){
this.name = name; // ํ์์ ์ด๋ฆ
this.code = code; // ํ์์ ์๊ธ ์ํ, ์ซ์๊ฐ ํด์๋ก ์ฐ์ ์์, ์๊ธ๋๊ฐ ๋๋ค.
}
}
class PriorityQueue extends Queue{ // queue๋ฅผ ์์ํ๋ priorityQueue
constructor(){
super(); // ๋ถ๋ชจ์ ์์ฑ ์์๋ฐ์
}
// ์ฐ์ ์์์ ๋ฐ๋ผ ๋ฐ์ดํฐ ์ ๊ฑฐ
dequeue(){
let priority = this.dataStore[0].code;
for(let i=1; i< this.dataStore.length; ++i){
if(this.dataStore[i].code < priority){
priority = i;
}
}
return this.dataStore.splice(priority,1);
}
toString(){
let stringifiedQueue = '';
for(let i=0; i< this.dataStore.length; i++){
stringifiedQueue+=this.dataStore[i].name +' code' + this.dataStore[i].code + "\n";
}
return stringifiedQueue;
}
}
// ํ์๋ฅผ ์ฐ์ ์์ ํ์ ์ถ๊ฐ
const patient1 = new Patient("Smith", 5);
priorityQueue.enqueue(patient1);
const patient2 = new Patient("Jones", 4);
priorityQueue.enqueue(patient2);
const patient3 = new Patient("Brown", 1);
priorityQueue.enqueue(patient3);
const patient4 = new Patient("Ingram", 1);
priorityQueue.enqueue(patient4);
print(priorityQueue.toString());
let treatedPatient = priorityQueue.dequeue();
print(`treated patient: ${treatedPatient[0].name}`);
print(`need treatment: ${priorityQueue.toString()}`);
treatedPatient = priorityQueue.dequeue();
print(`treated patient: ${treatedPatient[0].name}`);
print(`need treatment: ${priorityQueue.toString()}`);
/**
Smith code5
Jones code4
Brown code1
Ingram code1
VM1184:40 treated patient: Jones
VM1184:40 need treatment: Smith code5
Brown code1
Ingram code1
VM1184:40 treated patient: Brown
VM1184:40 need treatment: Smith code5
Ingram code1
*/
๐ ๋ง์น๋ฉด์
โบ๏ธ ์ค๋์ ํ๊ฐ ๋ฌด์์ธ์ง, ์ด๋ค ์ฃผ์ ์ฐ์ฐ์ด ์๊ณ , ํ๋ฅผ ์ฌ์ฉํด์ ์ ๋ ฌํ๋ ์์์ ์ฐ์ ์์ ํ์ ๋ํด์ ์ดํด๋ดค๋ค.
์ด ์ฑ ์ด ์ฐ์ด๋ 2014์๋ ํด๋์ค ๋ฌธ๋ฒ์ด ๊ณต์ ๊ท์ ์ ์์๋ ๋๋ผ function์ผ๋ก ์ฐ์ด๊ณ ์๋ค.๋ฌผ๋ก ๊ทธ๋ฅ function์ผ๋ก ์จ๋ ๋์ง๋ง ํด๋์ค๋ฅผ ์ธ ๊ฒฝ์ฐ๊ฐ ๋ณ๋กค ์์ด์ ํ๊ธฐ ์ฐจ์์์ ์ฐ๋๊น ๋ซ๋ฐฐ๋.
์ด์ ๋ฌตํ๋๋ ํด๋์ค ๋ฌธ๋ฒ์ ๋ค์ ์ฌ์ฉํด ๋ด์ ์ข์๋ค.
๐ Reference
- Data Structures and Algorithms Using Javaโ Script by Michael McMillian (O’Reilly). Copyright 2014 Michael McMillan, 978-1-449-36493-9
https://en.wikipedia.org/wiki/Radix_sort
Radix sort - Wikipedia
From Wikipedia, the free encyclopedia Non-comparative integer sorting algorithm In computer science, radix sort is a non-comparative sorting algorithm. It avoids comparison by creating and distributing elements into buckets according to their radix. For el
en.wikipedia.org
'๐ค data structures & algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Data Structures & Algorithms with Javascript : Dictionaries (0) | 2024.07.22 |
---|---|
Data Structures & Algorithms with Javascript : Linked Lists (0) | 2024.07.21 |
Data Structures & Algorithms with Javascript : Stacks (0) | 2024.07.19 |
Data Structures & Algorithms with Javascript : Lists (0) | 2024.07.18 |
Data Structures & Algorithms with Javascript : Arrays (3) | 2024.07.17 |