๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿค– data structures & algorithms

Data Structures & Algorithms with Javascript : Dictionaries

[์ด์ „๊ธ€] 2024.07.21 - [๐Ÿค– data structures & algorithms] - Data Structures & Algorithms with Javascript : Linked Lists

๐Ÿ‘ซ Dictionary ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋Š” ํ‚ค-๊ฐ’ ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋‹ค.
โ˜Ž ์ „ํ™”๋ฒˆํ˜ธ๋ถ€์—์„œ ์ด๋ฆ„์„ ์ฐพ์œผ๋ฉด ํ•ด๋‹น ๋ฒˆํ˜ธ๋ฅผ ๋ฐ”๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰ ์‹œ์— ์•„์ฃผ ํŽธํ•˜๊ฒŒ ๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
๐Ÿ‘ท ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Object ํด๋ž˜์Šค๋Š” dictionary ์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๋„๋ก ๋””์ž์ธ ๋˜์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค.

๐Ÿ“š ์˜ค๋Š˜์€ Object ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•ด์„œ Dictionary ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ณ , dictionary ์‚ฌ์šฉ์˜ ํŠน์ง•๊ณผ ์žฅ์ ๋“ค์„ ์‚ดํŽด๋ณด์ž!

๐Ÿ“™ Dictionary ํด๋ž˜์Šค

Dictionary ํด๋ž˜์Šค์˜ ๊ธฐ๋ฐ˜์€ Object ํด๋ž˜์Šค๊ฐ€ ์•„๋‹ˆ๋ผ Array ํด๋ž˜์Šค๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Object์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •๋ ฌํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ dictionary์˜ ํ‚ค๋Š” ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์›์‹œํƒ€์ž…์ด ์•„๋‹Œ ๊ฐ’์€ ๋ชจ๋“ ๋‘ ๊ฐ์ฒด์ด๋ฏ€๋กœ array๋„ ๊ฐ์ฒด๋‹ค.

 

โ€ผ ์˜ˆ์ œ๋Š” ์ฑ…์˜ ๊ตฌํ˜„์„ ์ฐธ๊ณ ํ•˜์—ฌ ํด๋ž˜์Šค๋กœ ์žฌ๊ตฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค!!

class Dictionary{
	constructor(){
    	this.datastore = new Array();
    }
    
    // ์ถœ๋ ฅ ํ—ฌํผ ๋ฉ”์„œ๋“œ
    print(item){
    	console.log(item);
    }
    
    // ํ‚ค์™€ ๊ฐ’ ์ถ”๊ฐ€
    add(key,value){
    	this.datastore[key] = value;
    }
    
    // ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
    find(key){
    	return this.datastore[key]
    }
    
    // ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ํ‚ค์™€ ๊ฐ’ ์ œ๊ฑฐ
    remove(key){
    	delete this.datastore[key];
    }
        
    // ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ํ‚ค์™€ ๊ฐ’ ์ œ๊ฑฐ
    showAll(){
        Object.keys(this.datastore).forEach((key)=>this.print(key+" -> "+this.datastore[key]));
    }
    
    ////////////// ์ถ”๊ฐ€์ ์ธ ํ—ฌํผ ๋ฉ”์†Œ๋“œ๋“ค ////////////// 
    
    // dictionary ์— ํ‚ค-๊ฐ’ ๊ฐœ์ˆ˜ 
    // string ์ด ํ‚ค์ผ ๊ฒฝ์šฐ length ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—
    count(){
    	let n = 0;
        Object.keys(this.datastore).forEach((key)=> ++n);
		return n;
    }
        
    // dictionary ๋น„์šฐ๊ธฐ
    clear(){
        Object.keys(this.datastore).forEach((key)=> delete this.datastore[key]);
    }
    
    
     ////////////// ์ถ”๊ฐ€์ ์ธ ํ—ฌํผ ๋ฉ”์†Œ๋“œ๋“ค ////////////// 
}

const contacts = new Dictionary();
contacts.add("a",'010-1234-1234');
contacts.add("b",'010-1111-2222');
contacts.add("c",'010-2222-3333');
contacts.print(`b์˜ ์ „ํ™” ๋ฒˆํ˜ธ : ${contacts.find('b')}`);
contacts.remove("c");
contacts.showAll();
contacts.clear();
contacts.print(`dictionary ์•„์ดํ…œ ๊ฐœ์ˆ˜ : ${contacts.count()}`);

/**	
b์˜ ์ „ํ™” ๋ฒˆํ˜ธ : 010-1111-2222
a -> 010-1234-1234
b -> 010-1111-2222
dictionary ์•„์ดํ…œ ๊ฐœ์ˆ˜ : 0
*/

๐Ÿ“– Dictionary ์ •๋ ฌํ•˜๊ธฐ

Dictionary ์˜ ์ฃผ์š” ๋ชฉ์ ์€ ํ‚ค๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์‹ค์ œ๋กœ dictionary๊ฐ€ ์ €์žฅ๋˜๋Š” ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ •๋ ฌ๋œ ์ƒํƒœ๋กœ ์ €์žฅ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ํ•„์š”ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

array์˜ ํ‚ค๊ฐ€ ์ˆซ์ž์ธ ๊ฒฝ์šฐ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๋‹ค์Œ์€ ์ˆซ์ž ํ‚ค ๊ฐ’์„ ํ•œ array๋ฅผ ๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•œ ๊ฒƒ์ด๋‹ค.

const print = item => console.log(item);

let arr = new Array();
arr[0] = "Millie";
arr[1] = "Danielle";

print(arr); // ["Millie", "Danielle"];
arr.sort();
print(arr); // ["Danielle", "Millie"];

 

ํ•˜์ง€๋งŒ string๊ฐ’์ธ ํ‚ค๋กœ๋Š” ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•˜๋„๋ก ํ•  ์ˆ˜ ์—†๋‹ค.

๋Œ€์‹  ํ‚ค ๊ฐ’์œผ๋กœ ์ •๋ ฌํ•˜๊ณ ์ž ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

class Dictionary{
	constructor(){
    	this.datastore = new Array();
    }
    
    print(item){
    	console.log(item);
    }
    
    add(key,value){
    	this.datastore[key] = value;
    }
    
    find(key){
    	return this.datastore[key]
    }
    
    remove(key){
    	delete this.datastore[key];
    }

    showAll(){
    // ๋ณ€๊ฒฝ
        Object.keys(this.datastore).sort().forEach((key)=>this.print(key+" -> "+this.datastore[key]));
    }


    count(){
    	let n = 0;
        Object.keys(this.datastore).forEach((key)=> ++n);
		return n;
    }

    clear(){
        Object.keys(this.datastore).forEach((key)=> delete this.datastore[key]);
    }

}

const contacts = new Dictionary();
contacts.add("b",'010-2342-3333');
contacts.add("a",'010-1234-1234');
contacts.add("d",'010-1111-2321');
contacts.add("f",'010-2424-3333');
contacts.add("e",'010-333-2222');
contacts.add("c",'010-1023-3333');
contacts.showAll();


/**	
a -> 010-1234-1234
b -> 010-2342-3333
c -> 010-1023-3333
d -> 010-1111-2321
e -> 010-333-2222
f -> 010-2424-3333

*/

 

 

 


๐Ÿ“š Reference

- Data Structures and Algorithms Using Javaโ€ Script by Michael McMillian (O’Reilly). Copyright 2014 Michael McMillan, 978-1-449-36493-9