์๋์ ์์๋ค์ ๋ฐํ์ผ๋ก ์์ฑ๋์์ต๋๋ค. ์ข์์์ ๊ฐ์ฌํฉ๋๋ค.
Trie(ํธ๋ผ์ด) Tree์ ๋ํด์ - https://youtu.be/TohdsR58i3Q
Trie - Insert and Search | GeeksforGeeks - https://youtu.be/dUBkaqrcYT8
0. ํธ๋ผ์ด ํธ๋ฆฌ(Trie tree)๋? |
(2023.03.16 ์์ )
๋ฌธ์์ด ๊ฒ์์ ๋น ๋ฅด๊ฒ ํด์ฃผ๋ ์ด์ง ๊ฒ์ ํธ๋ฆฌ ๊ตฌ์กฐ.
ํ ๋ ๋ฒจ๋น ํ ๋ฌธ์์ฉ ์ ์ฅ๋๋ฏ๋ก
M์ด ๋ฌธ์์ด์ ์ต๋ ๊ธธ์ด๋ผ๋ฉด ๋ฌธ์์ด ๊ฒ์์ ์๊ฐ๋ณต์ก๋๋ O(Mlogn) ๋ค.
ํธ๋ผ์ด๋ฅผ ์ด์ฉํ๋ฉด ์๊ฐ๋ณต์ก๋ O(M)์ด ๊ฑธ๋ฆฌ๋ ๋ฌธ์์ด ๊ฒ์๊ตฌ์กฐ๋ฅผ ๋ง๋ค ์ ์๋ค.
์ฐธ๊ณ -> https://ko.wikipedia.org/wiki/%ED%8A%B8%EB%9D%BC%EC%9D%B4_(%EC%BB%B4%ED%93%A8%ED%8C%85)
1. ์ฝ์ (Inserting) |
1. root๋ ๋น์๋๋ค
2. ์ฝ์ ํ๋ ค๋ ์ฒซ๊ธ์๊ฐ root์ child๋ก ์ ์ฅ๋์ด์์ผ๋ฉด ํด๋น child๋ฅผ ๋ฐ๋ผ ๋ด๋ ค๊ฐ๋ค.(์์ผ๋ฉด ์ฒซ๊ธ์๋ฅผ sibling์ผ๋ก ์ฝ์ ํ๋ค.)
3. ๋์ผํ ๋ฌธ์๊ฐ ์์๊ฒฝ์ฐ ๊ทธ๋ฅ ๋ค์๊ธ์๋ก ๋ด๋ ค๊ฐ๊ณ , ๋์ผํ ๋ฌธ์๊ฐ ์์๊ฒฝ์ฐ ์๋ก์ด ๋ฌธ์๋ฅผ child์ sibling๋ก ์ฝ์ ํ๋ค.
4. ๋งจ ๋ง์ง๋ง ๊ธ์๋ฅผ ์ฒดํฌ ๋๋ ์ฝ์ ํ ๋๊น์ง ๋ฐ๋ณตํ๋ค.
2. ํธ๋ผ์ด์์์ ๋ฌธ์์ด ํ์(Searching in a trie) |
1. root์ child๋ถํฐ ์์ํ๋ค.
2. child์ ์ฒซ๋ฒ์งธ ๋ฌธ์๊ฐ ์ ์ฅ๋์ด ์๋์ง ์ฒดํฌํ๋ค.
3. ์ ์ฅ๋์ด ์๋ค๋ฉด ํด๋น child๋ก ๋ด๋ ค๊ฐ๋ค.
4. ๋ค์ ๋ฌธ์๊ฐ child์ ์ ์ฅ๋์ด ์์๋๊น์ง ๋ฐ๋ณตํ๋ค.
5. ๊ฒ์ํ๋ ค๋ ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ leaf์ ๋ ๋ฒจ๊ณผ ๋ฑ ๋จ์ด์ง๋ฉด ๊ฒ์์ฑ๊ณต, ์๋๋ฉด ๊ฒ์ ์คํจ.
3. ํธ๋ผ์ด์ ์ฝ์ , ํ์ ๊ตฌํ(c++) |
there, their, answer, any, bye๋ฅผ ํธ๋ผ์ด์ ์ฝ์ ์ ์๋์ฒ๋ผ ๋๋๋ก ๊ตฌํํด๋ณด๊ธฐ.

(์ฐ์ต์ฉ์ผ๋ก ๊ตฌํํ ์ฝ๋์ ๋๋ค. ์๋ชป๋ ๋ถ๋ถ์ด ์์ผ๋ฉด ์ธ์ ๋ ์๋ ค์ฃผ์ธ์. ๊ฐ์ฌํฉ๋๋ค (--)(__))
(1) .h
#pragma once | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
class Node | |
{ | |
public: | |
char data; | |
vector<Node*> siblings; | |
}; | |
class Trie | |
{ | |
private: | |
Node* root = new Node; | |
public: | |
void push(string str) { | |
insert(str, 0, root); | |
} | |
void insert(string str, int idx, Node* node) | |
{ | |
if (idx >= str.length()) return; | |
int next = -1; | |
for (int i = 0; i < node->siblings.size(); i++) { | |
if (str[idx] == node->siblings[i]->data) {//๋์ผํ ๋ฌธ์๊ฐ ์๋ ๊ฒฝ์ฐ | |
next = i; | |
break; | |
} | |
} | |
if (next != -1) | |
insert(str, idx + 1, node->siblings[next]); | |
else//๋์ผํ ๋ฌธ์๊ฐ ์๋๊ฒฝ์ฐ, ์์์ผ๋ก ์ถ๊ฐ | |
{ | |
Node* newNode = new Node; | |
newNode->data = str[idx]; | |
node->siblings.push_back(newNode); | |
insert(str, idx + 1, newNode); | |
} | |
} | |
bool search(string str) | |
{ | |
return findWord(str, 0, root); | |
} | |
bool findWord(string str, int idx, Node* node) | |
{ | |
if (!node->siblings.empty() && idx >= str.length()) return false; | |
if (idx >= str.length()) return true; | |
int next = -1; | |
for (int i = 0; i < node->siblings.size(); i++) { | |
if (str[idx] == node->siblings[i]->data) { | |
next = i; | |
break; | |
} | |
} | |
if (next != -1)//๋์ผํ ๋ฌธ์๊ฐ ์์ | |
return findWord(str, idx + 1, node->siblings[next]); | |
else//๊ฒ์ ์คํจ | |
return false; | |
} | |
void print() { | |
order(root, 1); | |
} | |
void order(Node* node, int depth) | |
{ | |
for (int i = 0; i < node->siblings.size(); i++) | |
{ | |
cout << "(" << depth << ") " << node->siblings[i]->data << " "; | |
if (!node->siblings[i]->siblings.empty()) { | |
order(node->siblings[i], depth + 1); | |
} | |
else cout << endl; | |
} | |
} | |
}; |
(2) .cpp
#include "header.h" | |
int main(){ | |
Trie* trie = new Trie; | |
trie->push("there"); | |
trie->push("their"); | |
trie->push("answer"); | |
trie->push("any"); | |
trie->push("bye"); | |
trie->print(); | |
vector<string> v{"ans","banana","answer","bye","byeyo","a"}; | |
for (int i = 0; i < v.size(); i++) | |
{ | |
if (trie->search(v[i])) cout << "๊ฒ์ O : " << v[i] << endl; | |
else cout << "๊ฒ์ X : " << v[i] << endl; | |
} | |
return 0; | |
} |
์คํ๊ฒฐ๊ณผ

'โ๏ธ ์ด๋ก > ์๋ฃ๊ตฌ์กฐ, ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋ฃ๊ตฌ์กฐ] ํธ๋ฆฌ(Tree) - ๋ฐฐ์ด์ ์ด์ง๊ฒ์ํธ๋ฆฌ๋ก ๋ง๋ค๊ธฐ (0) | 2019.09.19 |
---|---|
[์๋ฃ๊ตฌ์กฐ] ๊ทธ๋ํ(Graph) - ๋ ์ง์ ์ ๊ฒฝ๋ก ์ฐพ๊ธฐ(dfs) (0) | 2019.09.19 |
[์๋ฃ๊ตฌ์กฐ] ๊ทธ๋ํ(Graph) - DFS, BFS ๊ตฌํ (0) | 2019.09.19 |
[์๋ฃ๊ตฌ์กฐ] ํธ๋ฆฌ(Tree) - ์ด์ง ํ(Binary Heaps) (0) | 2019.09.19 |
[์๋ฃ๊ตฌ์กฐ] ํธ๋ฆฌ(Tree) - ํธ๋ฆฌ์ ์ข ๋ฅ, 3๊ฐ์ง ์ํ๋ฐฉ๋ฒ (0) | 2019.09.19 |