์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- Flex
- useRef
- ๋ฐ๋ธ์ฝ์ค3๊ธฐ
- Props
- float
- ์ฝ๋ฉํ ์คํธ
- position
- REACT
- history api
- ์๊ณ ๋ฆฌ์ฆ
- CSS
- ๋ฐ๋ธ์ฝ์ค
- fetch API
- ์๋ฐ์คํฌ๋ฆฝํธ
- ํ๋ก ํธ์๋
- ๋ธ๋ก๊ทธ
- ํ๋ก๊ทธ๋๋จธ์ค
- useEffect
- Gatsby
- Today
- Total
Daehyunii's Dev-blog
17 ํธ๋ฆฌ ์ํ ๋ณธ๋ฌธ
17 ํธ๋ฆฌ ์ํ
Daehyunii 2022. 8. 16. 20:1617.1 ํธ๋ฆฌ ์ํ
ํธ๋ฆฌ ์ํ๋ ๋ง ๊ทธ๋๋ก ์ด๋ค ํธ๋ฆฌ์์๋ ์ง ์ฌ์ฉํ ์ ์๋ ๊ฐ๋ ์ด๋ค. ์๋ฅผ ๋ค์ด ์ด์ง ํ์ ํธ๋ฆฌ๋ ์ง, ๊ทธ๋ฅ ์ด์ง ํธ๋ฆฌ๋ ์ง ๊ทธ ์ธ์ ์ด๋ค ํธ๋ฆฌ๋ค์๋ ๋ค ์ฌ์ฉ์ด ๊ฐ๋ฅํ ์ํ ๋ฐฉ๋ฒ์ด๋ค. ์ฆ ํธ๋ฆฌ์ ํน์ฑ์ ๊ฐ์ง๊ณ ์๋ค๋ฉด ํธ๋ฆฌ ์ํ๋ฅผ ํ ์ ์๋ค. ํธ๋ฆฌ๋ฅผ ์ํํ๋ ๋ฐฉ๋ฒ์๋ ๋ง์ ๋ฐฉ๋ฒ์ด ์กด์ฌํ๊ณ ๋ํ์ ์ผ๋ก ๋๋น ์ฐ์ ํ์(Breadth First Search)๊ณผ ๊น์ด ์ฐ์ ํ์(Deapth First Search)๊ฐ ์๋ค. ์ด๋ค ํ์ ๋ฐฉ๋ฒ์ด๋ ๋ชจ๋ ๋ ธ๋๋ค์ ์ํํ๋ ๊ฒ์ ๋์ผํ๋ค.
17.2 ๋๋น ์ฐ์ ํ์
๋๋น ์ฐ์ ํ์(Breadth First Search)์ ํธ๋ฆฌ์ ๊ฐ์ ๋ ๋ฒจ์ ์๋ ๋ชจ๋ ๋ ธ๋๋ฅผ ๊ฑฐ์ณ๊ฐ๋ฉฐ ์ํ๋ฅผ ํ๋ ๋ฐฉ์์ด๋ค.
/*
ํธ๋ฆฌ)
10
/ \
6 15
/ \ / \
3 8 12 20
๋๋น ์ฐ์ ํ์) ๊ฐ์ ๋ ๋ฒจ์ ๋
ธ๋๋ค์ ๊ฑฐ์ณ๊ฐ๋ฉด์ ํ์
------>10
--->6----------->15
3----->8----->12----->20
*/
๋๋น ์ฐ์ ํ์์ ๊ตฌํํ๋ ค๋ฉด ํ ์๋ฃ ๊ตฌ์กฐ๋ฅผ ์ด์ฉํด์ผ ํ๋ค.(์ ์ ์ ์ถ) ์ง์ ํ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ ํ์ฉํ๋ ๊ฒ๋ณด๋ค ๋ฐฐ์ด์ ํ์ฉํด์ ํ๋ฅผ ๋ง๋๋ ๊ฒ์ด ๊ตฌํํ๊ธฐ ๋ ํธ๋ฆฌํ๋ค.
โ์๋์ฝ๋
1) ๋จผ์ ๋ณ์ ๋ ๊ฐ๋ฅผ ๋ง๋ ๋ค.
2) ํ๋ฅผ ๋ง๋ค์ด์ ์์๋ค์ ์ถ์ ํ๊ณ , ๋ฐ์ดํฐ๋ฅผ ๋ด๋ ๋ฐฐ์ด์ ๋ง๋ค์ด์ ๋ง์ง๋ง์ ๋ฐํํด ์ค๋ค.
์ฆ, ๋ง์ง๋ง์ ๋ฐ์ดํฐ๋ฅผ ๋ด์ ๋ฐฐ์ด์ ๋ฐํํ๊ณ , ํ๋ ๋ง์ง๋ง์๋ ๋น์ด ์๋ ๋ฐฐ์ด ์ํ๊ฐ ๋๋ค.(ํ ๋ฐฐ์ด์ ๊ทธ๋ฅ ๋์์ฃผ๋ ์ญํ ์)
3) ์ฒ์์๋ ๋ฃจํธ ๋ ธ๋๋ฅผ ํ ๋ฐฐ์ด์ ๋ฃ๊ณ , ํ ๋ฐฐ์ด์ ๋ฐ์ดํฐ๊ฐ ์๋ค๋ฉด ๊ณ์ ๋ฃจํ๋ฅผ ๋๋ฆฐ๋ค.
4) ๊ทธ๋ฆฌ๊ณ ํ์์ ๋ํ๋ฅผ ํ๋ค. ๋ฐฐ์ด์ ์ฌ์ฉํด์ ๋ง๋๋ ๊ฒฝ์ฐ์๋ shift() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ ํ ๋ฐฐ์ด์ ๋งจ ์์ ์๋ ๋ฐ์ดํฐ๋ฅผ ์ ๊ฑฐ ํ๋ค.
5) ๊ทธ๋ฆฌ๊ณ ์ ๊ฑฐํ ๋ ธ๋์ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ด๋ ๋ฐฐ์ด์ ์ถ๊ฐํด ์ค๋ค.
6) ๊ทธ๋ฆฌ๊ณ ์ ๊ฑฐ๋ ๋ ธ๋์ ์ผ์ชฝ/์ค๋ฅธ์ชฝ์ ๋ ธ๋๊ฐ ์๋์ง ํ์ธํ๊ณ ๋ ธ๋๊ฐ ์๋ค๋ฉด ํด๋น ๋ ธ๋๋ฅผ ํ ๋ฐฐ์ด์ ๋ด๋๋ค.
7) ๋ชจ๋ ๋ฃจํ๊ฐ ๋๋ ๋ค์์๋ ๋ชจ๋ ๋ ธ๋์ ๊ฐ๋ค์ ์ ์ฅํ ๋ณ์๋ฅผ ์ถ๋ ฅํด ์ค๋ค.
class Node {
constructor(value){
this.value = value;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor(){
this.root = null;
}
insert(value){
var newNode = new Node(value);
if(this.root === null){
this.root = newNode;
return this;
}
var current = this.root;
while(true){
if(value === current.value) return undefined;
if(value < current.value){
if(current.left === null){
current.left = newNode;
return this;
}
current = current.left;
} else {
if(current.right === null){
current.right = newNode;
return this;
}
current = current.right;
}
}
}
find(value){
if(this.root === null) return false;
var current = this.root,
found = false;
while(current && !found){
if(value < current.value){
current = current.left;
} else if(value > current.value){
current = current.right;
} else {
found = true;
}
}
if(!found) return undefined;
return current;
}
BFS(){
var node = this.root,
data = [],
queue = [];
queue.push(node);
while(queue.length){ //๋น ๋ฐฐ์ด์ ๋ฌด์กฐ๊ฑด true๊ฐ ๋์์ length๋ฅผ ์ด์ฉ
node = queue.shift(); // shift ์๋ณธ ๋ฐฐ์ด ์ง์ ๋ณ๊ฒฝ(ํ ๋ฐฐ์ด์์ ์ ๊ฑฐ๋จ)
data.push(node.value); // push ์๋ณธ ๋ฐฐ์ด ์ง์ ๋ณ๊ฒฝ(๋ฐ์ดํ ๋ฐฐ์ด์ ์ถ๊ฐ๋จ)
if(node.left) queue.push(node.left);
if(node.right) queue.push(node.right);
}
return data;
}
}
17.3 ๊น์ด ์ฐ์ ํ์
๊น์ด ์ฐ์ ํ์์ ๋๋น ์ฐ์ ํ์ ์ฒ๋ผ ํ์ ๋ ธ๋๋ฅผ ๋ค ๊ฑฐ์ณ๊ฐ๋ฉด์ ํ์ํ๋ ๊ฒ์ด ์๋๋ผ, ์์ง์ผ๋ก ํธ๋ฆฌ์ ๋๊น์ง ๋ด๋ ค๊ฐ๋ฉด์ ๊ฒ์ํ๋ ๊ฒ์ด๋ค. ๊น์ด ์ฐ์ ํ์์ ์ํ์ ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๋ฐฐ์ด์ ์ธ์ ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ์ง์ ๋ฐ๋ผ ์ ์ ์ํ, ํ์ ์ํ, ์ค์ ์ํ๋ก ๋๋ ์ ์๋ค.
โ์๋์ฝ๋
1) ์ํ์ ํ์ธ์ ํ ๋ ธ๋๋ค์ ๊ฐ์ ์ ์ฅํ๋ ๋ฐฐ์ด์ ๋ง๋ ๋ค.
2) ๋งจ ๋ง์ง๋ง์๋ ๊ฐ์ ์ ์ฅํ ๋ฐฐ์ด์ ๋ฐํํ๋ค.
3) ํ์ฌ ํ์ธํ๊ณ ์๋ ๋ ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ ์ญํ ์ ํ ๋ณ์๋ฅผ ๋ง๋ค์ด ์ค๋ค. ๊ทธ๋ฆฌ๊ณ ํธ๋ฆฌ์ ๋ฃจํธ๋ ธ๋๋ฅผ ํ ๋นํ๋ค.
4) ์ด์ ํฌํผ ํจ์์ธ ์ค์ฒฉ ํจ์๋ฅผ ๋ง๋ ๋ค.
5) ์ด์ ๋ ธ๋๋ค์ ๋ฐฉ๋ฌธํด์ ๋ ธ๋๋ค์ ๊ฐ์ ์ ์ฅํ๋ ๋ฐฐ์ด์ ๊ฐ์ ์ถ๊ฐํด ์ค๋ค.
6) ๊ทธ๋ฆฌ๊ณ ํ์ธํ ๋ ธ๋๊ฐ ์ผ์ชฝ/์ค๋ฅธ์ชฝ ํ๋กํผํฐ์ ๋ ธ๋๋ฅผ ๊ฐ์ง๊ณ ์๋ค๋ฉด ์ฌ๊ท ๋ฐฉ์์ผ๋ก ํฌํผ ํจ์๋ฅผ ๋ค์ ํธ์ถํฉ๋๋ค.
class Node {
constructor(value){
this.value = value;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor(){
this.root = null;
}
insert(value){
var newNode = new Node(value);
if(this.root === null){
this.root = newNode;
return this;
}
var current = this.root;
while(true){
if(value === current.value) return undefined;
if(value < current.value){
if(current.left === null){
current.left = newNode;
return this;
}
current = current.left;
} else {
if(current.right === null){
current.right = newNode;
return this;
}
current = current.right;
}
}
}
find(value){
if(this.root === null) return false;
var current = this.root,
found = false;
while(current && !found){
if(value < current.value){
current = current.left;
} else if(value > current.value){
current = current.right;
} else {
found = true;
}
}
if(!found) return undefined;
return current;
}
DFSPreOrder(){ //์ ์
var data = [];
function traverse(node){
data.push(node.value); // <--ํด๋น ์ฝ๋์ ์์น์ ๋ฐ๋ผ dfs์ ์ ํ์ด ๋ฌ๋ผ์ง๋ค.
if(node.left) traverse(node.left);
if(node.right) traverse(node.right);
}
traverse(this.root);
return data;
}
DFSPostOrder(){ //ํ์
var data = [];
function traverse(node){
if(node.left) traverse(node.left);
if(node.right) traverse(node.right);
data.push(node.value); // <--ํด๋น ์ฝ๋์ ์์น์ ๋ฐ๋ผ dfs์ ์ ํ์ด ๋ฌ๋ผ์ง๋ค.
}
traverse(this.root);
return data;
}
DFSInOrder(){ //์ค์
var data = [];
function traverse(node){
if(node.left) traverse(node.left);
data.push(node.value); // <--ํด๋น ์ฝ๋์ ์์น์ ๋ฐ๋ผ dfs์ ์ ํ์ด ๋ฌ๋ผ์ง๋ค.
if(node.right) traverse(node.right);
}
traverse(this.root);
return data;
}
}
๊ทธ๋ ๋ค๋ฉด ๋๋น ์ฐ์ ํ์, ๊น์ด ์ฐ์ ํ์์ ์ธ์ ์ฌ์ฉํ๋ฉด ์ข์๊น? ์ฐ์ ํธ๋ฆฌ์ ๋๋น๊ฐ ๋์ ๊ฒฝ์ฐ์๋ ๊น์ด ์ฐ์ ํ์ ๋ฐฉ๋ฒ์ด ๋ ์ ๋ฆฌํ๋ค. ์๋ํ๋ฉด ๋๋น ์ฐ์ ํ์์ ํ์ ์ ์ฅํด์ ๊ทธ๊ฒ์ ํ์ฉํ๋ ๋ฐฉ์์ผ๋ก ์ด๋ค์ง๊ธฐ ๋๋ฌธ์ ๋์ด๊ฐ ๋์ด ์ง์๋ก ๋ง์ ๋ฐ์ดํฐ๋ฅผ ํ์ ์ ์ฅํด์ผ ํ๋ฏ๋ก ๊ณต๊ฐ ๋ณต์ก๋๊ฐ ์ฌ๋ผ๊ฐ๊ธฐ ๋๋ฌธ์ด๋ค. ๋ฐ๋๋ก ํธ๋ฆฌ์ ๊น์ด๊ฐ ๊น์ ๊ฒฝ์ฐ์๋ ๋๋น ์ฐ์ ํ์ ๋ฐฉ๋ฒ์ด ์ ๋ฆฌํ๋ค. ๊น์ด ์ฐ์ ํ์์ ํฌํผ ํจ์๊ฐ ์ฌ๊ท ํธ์ถ๋๋๋ฐ ๊น์ด๊ฐ ๊น์ด์ง๋ฉด ๊ทธ ๋งํผ ์ฝ ์คํ์ด ๊ณ์ํด์ ์์ด๊ธฐ ๋๋ฌธ์ด๋ค.
'๐ Language & CS knowledge > Algorithm & Data structure' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
19 ํด์ ํ ์ด๋ธ (0) | 2022.08.16 |
---|---|
18 ์ด์ง ํ (0) | 2022.08.16 |
16 ์ด์ง ๊ฒ์ ํธ๋ฆฌ (0) | 2022.08.14 |
15 ์คํ & ํ (0) | 2022.08.14 |
14 ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ (0) | 2022.08.13 |