๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daehyunii's Dev-blog

2์ฃผ์ฐจ ๊ณผ์ œ - ํŠธ๋ผ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž๋™ ์™„์„ฑ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”. ๋ณธ๋ฌธ

๐Ÿ“„ Dev Course/Assignment

2์ฃผ์ฐจ ๊ณผ์ œ - ํŠธ๋ผ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž๋™ ์™„์„ฑ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

Daehyunii 2022. 10. 25. 01:44
ํŠธ๋ผ์ด ์ž๋ฃŒ ๊ตฌ์กฐ

์ด๋ฒˆ์— ํ•ด๊ฒฐํ•ด์•ผ ํ•˜๋Š” ๊ณผ์ œ๋Š” ํŠธ๋ผ์ด ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰์ฐฝ์˜ ์ž๋™ ์™„์„ฑ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์šฐ์„  ํŠธ๋ผ์ด ์ž๋ฃŒ ๊ตฌ์กฐ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ์•„์•ผ ํ–ˆ๋‹ค.

 

Trie ๋ž€?
๊ณต๊ฐ„ ๋ณต์žก๋„๊ฐ€ ํฐ ๋Œ€์‹ , ๋น ๋ฅธ ์‹œ๊ฐ„ ์•ˆ์— ๋‹จ์–ด๋ฅผ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋Š” ํŠธ๋ฆฌ์˜ ํ•œ ์ข…๋ฅ˜์ด๋‹ค.

ํŠธ๋ผ์ด ์ž๋ฃŒ๊ตฌ์กฐ๋ž€ ์‰ฝ๊ฒŒ ๋งํ•ด ๋ฌธ์ž์—ด์„ ์ €์žฅํ•ด์„œ ๊ฒ€์ƒ‰ํ•  ๋•Œ ํ™œ์šฉํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค. ํŠธ๋ผ์ด ์ž๋ฃŒ ๊ตฌ์กฐ์˜ ๊ฒฝ์šฐ์—๋Š” ๊ธ€๋กœ ์„ค๋ช…์„ ๋“ฃ๋Š”๊ฒƒ๋ณด๋‹ค ๊ทธ๋ฆผ์œผ๋กœ ์ดํ•ดํ•˜๋Š”๊ฒƒ์ด ํ›จ์”ฌ ์ง๊ด€์ ์œผ๋กœ ๋ฐ›์•„๋“œ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

 

Trie ์ž๋ฃŒ ๊ตฌ์กฐ

  ๊ทธ๋ฆผ์ด,, ์–‘ํ˜ธํ•˜์ง€ ๋ชปํ•œ์  ์ดํ•ดํ•˜๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค...; ๊ทธ๋ฆผ์„ ์„ค๋ช…ํ•ด ๋ณด์ž๋ฉด Trie ์ž๋ฃŒ๊ตฌ์กฐ์˜ root ๋…ธ๋“œ๋Š” value ๊ฐ’์„ ๋”ฐ๋กœ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰ ๋‹ค์‹œ ๋งํ•ด root ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ ๋นˆ ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์ด ์•„๋ฌด๋Ÿฐ ๊ฐ’๋„ ๊ฐ€์ง€์ง€ ์•Š๋Š”๊ฒƒ์ด ํŠน์ง•์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ๊ฐ„์„ ์€ ์˜๋ฏธ๋ฅผ ๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ์–ด ์ด๋ฅผ ์ž˜ ํ‘œํ˜„ํ•˜๋Š”๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. 

 

   ์œ„ ๊ทธ๋ฆผ์„ ๋‹ค์‹œ ๋ฌธ์ž๋กœ ํ’€์–ด๋ณด์ž๋ฉด root node ---(h๊ฐ„์„ )---> 'h' node ---(e๊ฐ„์„ )---> 'he' node ---(l๊ฐ„์„ )--- > 'hel' node ์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ๊ณต๊ฐ„ ๋ณต์žก๋„ ์ธก๋ฉด์—์„œ๋Š” ํšจ์œจ์„ฑ์ด ๋–จ์–ด์งˆ์ง€ ๋ชจ๋ฅด๋‚˜ ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰ํ•˜๋Š”๋ฐ ๋งค์šฐ ํšจ์œจ์ ์ด๋‹ค. ๊ฐ„์„ ์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๊ฒ ์ง€๋งŒ ๋‚˜๋Š” ๊ฐ์ฒด๋กœ ํ‘œํ˜„ํ–ˆ๋‹ค. ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ํ‚ค๋ฅผ ๊ฐ€์ค‘์น˜ ๊ฐ„์„ ์œผ๋กœ ํ™œ์šฉํ–ˆ๊ณ  ํ•ด๋‹น ํ‚ค๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์œผ๋กœ ๋…ธ๋“œ๋ฅผ ์ง‘์–ด๋„ฃ์—ˆ๋‹ค. 

 

  node๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 3๊ฐ€์ง€ ํ”„๋กœํผํ‹ฐ๋กœ ๊ตฌ์„ฑํ–ˆ๋‹ค. ๊ฐ’์„ ๋‹ด๊ณ  ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— value, ์ž์‹ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ธฐ ์œ„ํ•ด child(์—ฌ๊ธฐ์„œ ๊ฐ€์ค‘์น˜ ๊ฐ„์„ ์„ ํ‘œํ˜„), ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹จ์–ด๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ๋๊นŒ์ง€ ์˜จ์ „ํ•œ ๋‹จ์–ด๋ฅผ ์ „๋ถ€ Trie์— ๋‹ด์€ ๊ฒฝ์šฐ์— ๋‹ค ๋‹ด์•˜๋‹ค๋Š” ๊ฒƒ์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด complete ํ”„๋กœํผํ‹ฐ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. (์ด๋ฅผ ํ†ตํ•ด ํƒ์ƒ‰์„ ํ•˜๋ฉด์„œ complete๊ฐ€ true์ธ ๊ฒฝ์šฐ์—๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ์•„์„œ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ•˜์—ฌ ์ž๋™์™„์„ฑ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„)

 

ํŠธ๋ผ์ด ์ž๋™์™„์„ฑ ๊ธฐ๋Šฅ ๊ตฌํ˜„
// ํŠธ๋ผ์ด ์ž๋ฃŒ๊ตฌ์กฐ ์ž๋™์™„์„ฑ 

class TrieNode {
    constructor(value = '') {
        this.value = value;
        this.child = {};
        this.complete = false;
    }
}

class Trie {
    constructor() {
        this.root = new TrieNode(); 
    }

    insert(string) { // ๋ฌธ์ž์—ด ์‚ฝ์ž… ๋กœ์ง
        let currentNode = this.root;
        for(let i = 0 ; i < string.length ; i++) {
            let currentChar = string[i];
            if(currentNode.child[currentChar] === undefined) {
                currentNode.child[currentChar] = new TrieNode(currentNode.value + currentChar);
                currentNode = currentNode.child[currentChar];
            } else {
                currentNode = currentNode.child[currentChar];
            }
        }
        currentNode.complete = true;
    }

    search(string) { // ์™„์ „ ํƒ์ƒ‰์„ ํ•˜๊ธฐ ์œ„ํ•ด ํƒ์ƒ‰์„ ์‹œ์ž‘ ํ•  ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š” ๋กœ์ง(autoComplete ํ•จ์ˆ˜๋ฅผ ๋ณด์™„)
        let currentNode = this.root;
        for(let i = 0 ; i < string.length ; i++) {
            let currentChar = string[i];
            if(currentNode.child[currentChar] === undefined) {
                return undefined;
            } else {
                currentNode = currentNode.child[currentChar];
            }    
        }
        return currentNode; 
    }

    autoComplete(string) { // ์™„์ „ํƒ์ƒ‰ ํ›„ ๋…ธ๋“œ์˜ complete ํ”„๋กœํผํ‹ฐ๊ฐ€ true์ธ ๋‹จ์–ด๋“ค๋งŒ ๋ฐฐ์—ด์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
        let node = this.search(string);
        if(node === undefined) return null;
        let data = [];
        let queue = [];
        queue.push(node);

        while(queue.length) {
            node = queue.shift();
            if(node.complete === true) {
                data.push(node.value)
            }
            if(Object.keys(node.child).length > 0){
                for(let item in node.child) {
                    queue.push(node.child[item]);
                }
            }    
        }
        return data;
    }
}

let trie = new Trie();

trie.insert('hell');
trie.insert('hat');
trie.insert('hot');
trie.insert('hel');
trie.insert('het')
trie.insert('htt')

console.log(trie.autoComplete('')); //  ['hel', 'het', 'hat', 'hot', 'htt', 'hell']
console.log(trie.autoComplete('he')); //  ['hel', 'het', 'hell']
console.log(trie.autoComplete('t')); // ์—†๋Š” ๋ฌธ์ž์—ด์˜ ๊ฒฝ์šฐ์—๋Š” null ๋ฐ˜ํ™˜

https://github.com/WooDaeHyun/Dev_course-first-assignment.git

 

GitHub - WooDaeHyun/Dev_course-first-assignment

Contribute to WooDaeHyun/Dev_course-first-assignment development by creating an account on GitHub.

github.com