mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-25 05:02:24 +08:00
feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
This commit is contained in:
147
ko/codes/javascript/chapter_tree/array_binary_tree.js
Normal file
147
ko/codes/javascript/chapter_tree/array_binary_tree.js
Normal file
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
* File: array_binary_tree.js
|
||||
* Created Time: 2023-08-06
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
const { arrToTree } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* 배열로 표현한 이진 트리 클래스 */
|
||||
class ArrayBinaryTree {
|
||||
#tree;
|
||||
|
||||
/* 생성자 */
|
||||
constructor(arr) {
|
||||
this.#tree = arr;
|
||||
}
|
||||
|
||||
/* 리스트 용량 */
|
||||
size() {
|
||||
return this.#tree.length;
|
||||
}
|
||||
|
||||
/* 인덱스가 i인 노드의 값 반환 */
|
||||
val(i) {
|
||||
// 인덱스가 범위를 벗어나면 빈칸을 나타내는 null을 반환한다
|
||||
if (i < 0 || i >= this.size()) return null;
|
||||
return this.#tree[i];
|
||||
}
|
||||
|
||||
/* 인덱스가 i인 노드의 왼쪽 자식 노드 인덱스 반환 */
|
||||
left(i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* 인덱스가 i인 노드의 오른쪽 자식 노드 인덱스 반환 */
|
||||
right(i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* 인덱스가 i인 노드의 부모 노드 인덱스 반환 */
|
||||
parent(i) {
|
||||
return Math.floor((i - 1) / 2); // 나눗셈의 결과를 내림한다
|
||||
}
|
||||
|
||||
/* 레벨 순서 순회 */
|
||||
levelOrder() {
|
||||
let res = [];
|
||||
// 배열을 직접 순회한다
|
||||
for (let i = 0; i < this.size(); i++) {
|
||||
if (this.val(i) !== null) res.push(this.val(i));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 깊이 우선 순회 */
|
||||
#dfs(i, order, res) {
|
||||
// 빈칸이면 반환한다
|
||||
if (this.val(i) === null) return;
|
||||
// 전위 순회
|
||||
if (order === 'pre') res.push(this.val(i));
|
||||
this.#dfs(this.left(i), order, res);
|
||||
// 중위 순회
|
||||
if (order === 'in') res.push(this.val(i));
|
||||
this.#dfs(this.right(i), order, res);
|
||||
// 후위 순회
|
||||
if (order === 'post') res.push(this.val(i));
|
||||
}
|
||||
|
||||
/* 전위 순회 */
|
||||
preOrder() {
|
||||
const res = [];
|
||||
this.#dfs(0, 'pre', res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 중위 순회 */
|
||||
inOrder() {
|
||||
const res = [];
|
||||
this.#dfs(0, 'in', res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 후위 순회 */
|
||||
postOrder() {
|
||||
const res = [];
|
||||
this.#dfs(0, 'post', res);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
// 이진 트리 초기화
|
||||
// 여기서는 배열로부터 이진 트리를 바로 생성하는 함수를 사용한다
|
||||
const arr = Array.of(
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
null,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
null,
|
||||
null,
|
||||
12,
|
||||
null,
|
||||
null,
|
||||
15
|
||||
);
|
||||
|
||||
const root = arrToTree(arr);
|
||||
console.log('\n이진 트리 초기화\n');
|
||||
console.log('이진 트리의 배열 표현:');
|
||||
console.log(arr);
|
||||
console.log('이진 트리의 연결 리스트 표현:');
|
||||
printTree(root);
|
||||
|
||||
// 배열로 표현한 이진 트리 클래스
|
||||
const abt = new ArrayBinaryTree(arr);
|
||||
|
||||
// 노드 접근
|
||||
const i = 1;
|
||||
const l = abt.left(i);
|
||||
const r = abt.right(i);
|
||||
const p = abt.parent(i);
|
||||
console.log('\n현재 노드의 인덱스: ' + i + ' , 값: ' + abt.val(i));
|
||||
console.log(
|
||||
'왼쪽 자식 노드의 인덱스: ' + l + ' , 값: ' + (l === null ? 'null' : abt.val(l))
|
||||
);
|
||||
console.log(
|
||||
'오른쪽 자식 노드의 인덱스: ' + r + ' , 값: ' + (r === null ? 'null' : abt.val(r))
|
||||
);
|
||||
console.log(
|
||||
'부모 노드의 인덱스: ' + p + ' , 값: ' + (p === null ? 'null' : abt.val(p))
|
||||
);
|
||||
|
||||
// 트리 순회
|
||||
let res = abt.levelOrder();
|
||||
console.log('\n레벨 순서 순회:' + res);
|
||||
res = abt.preOrder();
|
||||
console.log('전위 순회:' + res);
|
||||
res = abt.inOrder();
|
||||
console.log('중위 순회:' + res);
|
||||
res = abt.postOrder();
|
||||
console.log('후위 순회:' + res);
|
||||
208
ko/codes/javascript/chapter_tree/avl_tree.js
Normal file
208
ko/codes/javascript/chapter_tree/avl_tree.js
Normal file
@@ -0,0 +1,208 @@
|
||||
/**
|
||||
* File: avl_tree.js
|
||||
* Created Time: 2023-02-05
|
||||
* Author: what-is-me (whatisme@outlook.jp)
|
||||
*/
|
||||
|
||||
const { TreeNode } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* AVL 트리*/
|
||||
class AVLTree {
|
||||
/* 생성자 */
|
||||
constructor() {
|
||||
this.root = null; //루트 노드
|
||||
}
|
||||
|
||||
/* 노드 높이 반환 */
|
||||
height(node) {
|
||||
// 빈 노드의 높이는 -1이고, 리프 노드의 높이는 0이다
|
||||
return node === null ? -1 : node.height;
|
||||
}
|
||||
|
||||
/* 노드 높이를 갱신한다 */
|
||||
#updateHeight(node) {
|
||||
// 노드 높이는 가장 높은 서브트리의 높이 + 1이다
|
||||
node.height =
|
||||
Math.max(this.height(node.left), this.height(node.right)) + 1;
|
||||
}
|
||||
|
||||
/* 균형 인수 반환 */
|
||||
balanceFactor(node) {
|
||||
// 빈 노드의 균형 인수는 0이다
|
||||
if (node === null) return 0;
|
||||
// 노드의 균형 인수 = 왼쪽 서브트리 높이 - 오른쪽 서브트리 높이
|
||||
return this.height(node.left) - this.height(node.right);
|
||||
}
|
||||
|
||||
/* 오른쪽 회전 연산 */
|
||||
#rightRotate(node) {
|
||||
const child = node.left;
|
||||
const grandChild = child.right;
|
||||
// child를 중심으로 node를 오른쪽으로 회전한다
|
||||
child.right = node;
|
||||
node.left = grandChild;
|
||||
// 노드 높이를 갱신한다
|
||||
this.#updateHeight(node);
|
||||
this.#updateHeight(child);
|
||||
// 회전한 서브트리의 루트 노드를 반환한다
|
||||
return child;
|
||||
}
|
||||
|
||||
/* 왼쪽 회전 연산 */
|
||||
#leftRotate(node) {
|
||||
const child = node.right;
|
||||
const grandChild = child.left;
|
||||
// child를 중심으로 node를 왼쪽으로 회전한다
|
||||
child.left = node;
|
||||
node.right = grandChild;
|
||||
// 노드 높이를 갱신한다
|
||||
this.#updateHeight(node);
|
||||
this.#updateHeight(child);
|
||||
// 회전한 서브트리의 루트 노드를 반환한다
|
||||
return child;
|
||||
}
|
||||
|
||||
/* 회전 연산을 수행하여 해당 서브트리의 균형 복원 */
|
||||
#rotate(node) {
|
||||
// 노드 node의 균형 인수를 구한다
|
||||
const balanceFactor = this.balanceFactor(node);
|
||||
// 왼쪽으로 치우친 트리
|
||||
if (balanceFactor > 1) {
|
||||
if (this.balanceFactor(node.left) >= 0) {
|
||||
// 오른쪽 회전
|
||||
return this.#rightRotate(node);
|
||||
} else {
|
||||
// 왼쪽으로 회전한 후 오른쪽으로 회전한다
|
||||
node.left = this.#leftRotate(node.left);
|
||||
return this.#rightRotate(node);
|
||||
}
|
||||
}
|
||||
// 오른쪽으로 치우친 트리
|
||||
if (balanceFactor < -1) {
|
||||
if (this.balanceFactor(node.right) <= 0) {
|
||||
// 왼쪽 회전
|
||||
return this.#leftRotate(node);
|
||||
} else {
|
||||
// 오른쪽으로 회전한 후 왼쪽으로 회전한다
|
||||
node.right = this.#rightRotate(node.right);
|
||||
return this.#leftRotate(node);
|
||||
}
|
||||
}
|
||||
// 균형 트리이므로 회전할 필요 없이 바로 반환한다
|
||||
return node;
|
||||
}
|
||||
|
||||
/* 노드 삽입 */
|
||||
insert(val) {
|
||||
this.root = this.#insertHelper(this.root, val);
|
||||
}
|
||||
|
||||
/* 노드의 재귀적 삽입(보조 메서드) */
|
||||
#insertHelper(node, val) {
|
||||
if (node === null) return new TreeNode(val);
|
||||
/* 1. 삽입 위치를 찾아 노드를 삽입한다 */
|
||||
if (val < node.val) node.left = this.#insertHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
node.right = this.#insertHelper(node.right, val);
|
||||
else return node; // 중복 노드는 삽입하지 않고 바로 반환한다
|
||||
this.#updateHeight(node); // 노드 높이를 갱신한다
|
||||
/* 2. 회전 연산을 수행하여 해당 서브트리의 균형을 복원한다 */
|
||||
node = this.#rotate(node);
|
||||
// 서브트리의 루트 노드를 반환한다
|
||||
return node;
|
||||
}
|
||||
|
||||
/* 노드 삭제 */
|
||||
remove(val) {
|
||||
this.root = this.#removeHelper(this.root, val);
|
||||
}
|
||||
|
||||
/* 노드의 재귀적 삭제(보조 메서드) */
|
||||
#removeHelper(node, val) {
|
||||
if (node === null) return null;
|
||||
/* 1. 노드를 찾아 삭제한다 */
|
||||
if (val < node.val) node.left = this.#removeHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
node.right = this.#removeHelper(node.right, val);
|
||||
else {
|
||||
if (node.left === null || node.right === null) {
|
||||
const child = node.left !== null ? node.left : node.right;
|
||||
// 자식 노드 수 = 0이면 node를 바로 삭제하고 반환한다
|
||||
if (child === null) return null;
|
||||
// 자식 노드 수 = 1이면 node를 바로 삭제한다
|
||||
else node = child;
|
||||
} else {
|
||||
// 자식 노드 수 = 2이면 중위 순회의 다음 노드를 삭제하고, 그 노드로 현재 노드를 대체한다
|
||||
let temp = node.right;
|
||||
while (temp.left !== null) {
|
||||
temp = temp.left;
|
||||
}
|
||||
node.right = this.#removeHelper(node.right, temp.val);
|
||||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
this.#updateHeight(node); // 노드 높이를 갱신한다
|
||||
/* 2. 회전 연산을 수행하여 해당 서브트리의 균형을 복원한다 */
|
||||
node = this.#rotate(node);
|
||||
// 서브트리의 루트 노드를 반환한다
|
||||
return node;
|
||||
}
|
||||
|
||||
/* 노드 탐색 */
|
||||
search(val) {
|
||||
let cur = this.root;
|
||||
// 반복하여 탐색하다 리프 노드를 지나면 종료한다
|
||||
while (cur !== null) {
|
||||
// 목표 노드는 cur의 오른쪽 서브트리에 있다
|
||||
if (cur.val < val) cur = cur.right;
|
||||
// 목표 노드는 cur의 왼쪽 서브트리에 있다
|
||||
else if (cur.val > val) cur = cur.left;
|
||||
// 목표 노드를 찾았으므로 반복문을 종료한다
|
||||
else break;
|
||||
}
|
||||
// 목표 노드를 반환한다
|
||||
return cur;
|
||||
}
|
||||
}
|
||||
|
||||
function testInsert(tree, val) {
|
||||
tree.insert(val);
|
||||
console.log('\n노드 삽입: ' + val + ' ; 연산 후 AVL 트리');
|
||||
printTree(tree.root);
|
||||
}
|
||||
|
||||
function testRemove(tree, val) {
|
||||
tree.remove(val);
|
||||
console.log('\n노드 삭제: ' + val + ' ; 연산 후 AVL 트리');
|
||||
printTree(tree.root);
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 빈 AVL 트리 초기화 */
|
||||
const avlTree = new AVLTree();
|
||||
/* 노드 삽입 */
|
||||
// 노드를 삽입한 후 AVL 트리가 어떻게 균형을 유지하는지 살펴본다
|
||||
testInsert(avlTree, 1);
|
||||
testInsert(avlTree, 2);
|
||||
testInsert(avlTree, 3);
|
||||
testInsert(avlTree, 4);
|
||||
testInsert(avlTree, 5);
|
||||
testInsert(avlTree, 8);
|
||||
testInsert(avlTree, 7);
|
||||
testInsert(avlTree, 9);
|
||||
testInsert(avlTree, 10);
|
||||
testInsert(avlTree, 6);
|
||||
|
||||
/* 중복 노드 삽입 */
|
||||
testInsert(avlTree, 7);
|
||||
|
||||
/* 노드 삭제 */
|
||||
// 노드를 삭제한 후 AVL 트리가 어떻게 균형을 유지하는지 살펴본다
|
||||
testRemove(avlTree, 8); // 차수가 0인 노드 삭제
|
||||
testRemove(avlTree, 5); // 차수가 1인 노드 삭제
|
||||
testRemove(avlTree, 4); // 차수가 2인 노드 삭제
|
||||
|
||||
/* 노드 조회 */
|
||||
const node = avlTree.search(7);
|
||||
console.log('\n찾은 노드 객체:', node, ', 노드 값 = ' + node.val);
|
||||
139
ko/codes/javascript/chapter_tree/binary_search_tree.js
Normal file
139
ko/codes/javascript/chapter_tree/binary_search_tree.js
Normal file
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* File: binary_search_tree.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
const { TreeNode } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* 이진 탐색 트리 */
|
||||
class BinarySearchTree {
|
||||
/* 생성자 */
|
||||
constructor() {
|
||||
// 빈 트리를 초기화한다
|
||||
this.root = null;
|
||||
}
|
||||
|
||||
/* 이진 트리의 루트 노드 반환 */
|
||||
getRoot() {
|
||||
return this.root;
|
||||
}
|
||||
|
||||
/* 노드 탐색 */
|
||||
search(num) {
|
||||
let cur = this.root;
|
||||
// 반복하여 탐색하다 리프 노드를 지나면 종료한다
|
||||
while (cur !== null) {
|
||||
// 목표 노드는 cur의 오른쪽 서브트리에 있다
|
||||
if (cur.val < num) cur = cur.right;
|
||||
// 목표 노드는 cur의 왼쪽 서브트리에 있다
|
||||
else if (cur.val > num) cur = cur.left;
|
||||
// 목표 노드를 찾았으므로 반복문을 종료한다
|
||||
else break;
|
||||
}
|
||||
// 목표 노드를 반환한다
|
||||
return cur;
|
||||
}
|
||||
|
||||
/* 노드 삽입 */
|
||||
insert(num) {
|
||||
// 트리가 비어 있으면 루트 노드를 초기화한다
|
||||
if (this.root === null) {
|
||||
this.root = new TreeNode(num);
|
||||
return;
|
||||
}
|
||||
let cur = this.root,
|
||||
pre = null;
|
||||
// 반복하여 탐색하다 리프 노드를 지나면 종료한다
|
||||
while (cur !== null) {
|
||||
// 중복 노드를 찾으면 바로 반환한다
|
||||
if (cur.val === num) return;
|
||||
pre = cur;
|
||||
// 삽입 위치는 cur의 오른쪽 서브트리에 있다
|
||||
if (cur.val < num) cur = cur.right;
|
||||
// 삽입 위치는 cur의 왼쪽 서브트리에 있다
|
||||
else cur = cur.left;
|
||||
}
|
||||
// 노드 삽입
|
||||
const node = new TreeNode(num);
|
||||
if (pre.val < num) pre.right = node;
|
||||
else pre.left = node;
|
||||
}
|
||||
|
||||
/* 노드 삭제 */
|
||||
remove(num) {
|
||||
// 트리가 비어 있으면 바로 반환하여 종료한다
|
||||
if (this.root === null) return;
|
||||
let cur = this.root,
|
||||
pre = null;
|
||||
// 반복하여 탐색하다 리프 노드를 지나면 종료한다
|
||||
while (cur !== null) {
|
||||
// 삭제할 노드를 찾았으므로 반복문을 종료한다
|
||||
if (cur.val === num) break;
|
||||
pre = cur;
|
||||
// 삭제할 노드는 cur의 오른쪽 서브트리에 있다
|
||||
if (cur.val < num) cur = cur.right;
|
||||
// 삭제할 노드는 cur의 왼쪽 서브트리에 있다
|
||||
else cur = cur.left;
|
||||
}
|
||||
// 삭제할 노드가 없으면 바로 반환한다
|
||||
if (cur === null) return;
|
||||
// 자식 노드 수 = 0 또는 1
|
||||
if (cur.left === null || cur.right === null) {
|
||||
// 자식 노드 수 = 0 / 1이면 child = null / 해당 자식 노드이다
|
||||
const child = cur.left !== null ? cur.left : cur.right;
|
||||
// 노드 cur를 삭제한다
|
||||
if (cur !== this.root) {
|
||||
if (pre.left === cur) pre.left = child;
|
||||
else pre.right = child;
|
||||
} else {
|
||||
// 삭제할 노드가 루트 노드이면 루트 노드를 다시 지정한다
|
||||
this.root = child;
|
||||
}
|
||||
}
|
||||
// 자식 노드 수 = 2
|
||||
else {
|
||||
// 중위 순회에서 cur의 다음 노드를 구한다
|
||||
let tmp = cur.right;
|
||||
while (tmp.left !== null) {
|
||||
tmp = tmp.left;
|
||||
}
|
||||
// 노드 tmp를 재귀적으로 삭제한다
|
||||
this.remove(tmp.val);
|
||||
// tmp로 cur를 덮어쓴다
|
||||
cur.val = tmp.val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 이진 탐색 트리 초기화 */
|
||||
const bst = new BinarySearchTree();
|
||||
// 삽입 순서에 따라 서로 다른 이진 트리가 생성된다. 이 순서는 포화 이진 트리를 생성할 수 있다
|
||||
const nums = [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15];
|
||||
for (const num of nums) {
|
||||
bst.insert(num);
|
||||
}
|
||||
console.log('\n초기화한 이진 트리\n');
|
||||
printTree(bst.getRoot());
|
||||
|
||||
/* 노드 탐색 */
|
||||
const node = bst.search(7);
|
||||
console.log('\n찾은 노드 객체: ' + node + ', 노드 값 = ' + node.val);
|
||||
|
||||
/* 노드 삽입 */
|
||||
bst.insert(16);
|
||||
console.log('\n노드 16을 삽입한 후의 이진 트리\n');
|
||||
printTree(bst.getRoot());
|
||||
|
||||
/* 노드 삭제 */
|
||||
bst.remove(1);
|
||||
console.log('\n노드 1을 삭제한 후의 이진 트리\n');
|
||||
printTree(bst.getRoot());
|
||||
bst.remove(2);
|
||||
console.log('\n노드 2를 삭제한 후의 이진 트리\n');
|
||||
printTree(bst.getRoot());
|
||||
bst.remove(4);
|
||||
console.log('\n노드 4를 삭제한 후의 이진 트리\n');
|
||||
printTree(bst.getRoot());
|
||||
35
ko/codes/javascript/chapter_tree/binary_tree.js
Normal file
35
ko/codes/javascript/chapter_tree/binary_tree.js
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* File: binary_tree.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
const { TreeNode } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* 이진 트리 초기화 */
|
||||
// 노드 초기화
|
||||
let n1 = new TreeNode(1),
|
||||
n2 = new TreeNode(2),
|
||||
n3 = new TreeNode(3),
|
||||
n4 = new TreeNode(4),
|
||||
n5 = new TreeNode(5);
|
||||
// 노드 사이의 참조(포인터)를 설정한다
|
||||
n1.left = n2;
|
||||
n1.right = n3;
|
||||
n2.left = n4;
|
||||
n2.right = n5;
|
||||
console.log('\n이진 트리 초기화\n');
|
||||
printTree(n1);
|
||||
|
||||
/* 노드 삽입과 삭제 */
|
||||
const P = new TreeNode(0);
|
||||
// n1 -> n2 사이에 노드 P를 삽입한다
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
console.log('\n노드 P를 삽입한 후\n');
|
||||
printTree(n1);
|
||||
// 노드 P를 삭제한다
|
||||
n1.left = n2;
|
||||
console.log('\n노드 P를 삭제한 후\n');
|
||||
printTree(n1);
|
||||
34
ko/codes/javascript/chapter_tree/binary_tree_bfs.js
Normal file
34
ko/codes/javascript/chapter_tree/binary_tree_bfs.js
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* File: binary_tree_bfs.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
const { arrToTree } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* 레벨 순서 순회 */
|
||||
function levelOrder(root) {
|
||||
// 큐를 초기화하고 루트 노드를 넣는다
|
||||
const queue = [root];
|
||||
// 순회 순서를 저장할 리스트를 초기화한다
|
||||
const list = [];
|
||||
while (queue.length) {
|
||||
let node = queue.shift(); // 큐에서 꺼낸다
|
||||
list.push(node.val); // 노드 값을 저장한다
|
||||
if (node.left) queue.push(node.left); // 왼쪽 자식 노드를 큐에 삽입한다
|
||||
if (node.right) queue.push(node.right); // 오른쪽 자식 노드를 큐에 삽입한다
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 이진 트리 초기화 */
|
||||
// 여기서는 배열로부터 이진 트리를 바로 생성하는 함수를 사용한다
|
||||
const root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
console.log('\n이진 트리 초기화\n');
|
||||
printTree(root);
|
||||
|
||||
/* 레벨 순서 순회 */
|
||||
const list = levelOrder(root);
|
||||
console.log('\n레벨 순서 순회의 노드 출력 순서 = ' + list);
|
||||
60
ko/codes/javascript/chapter_tree/binary_tree_dfs.js
Normal file
60
ko/codes/javascript/chapter_tree/binary_tree_dfs.js
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* File: binary_tree_dfs.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
const { arrToTree } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
// 순회 순서를 저장할 리스트를 초기화한다
|
||||
const list = [];
|
||||
|
||||
/* 전위 순회 */
|
||||
function preOrder(root) {
|
||||
if (root === null) return;
|
||||
// 방문 우선순위: 루트 노드 -> 왼쪽 서브트리 -> 오른쪽 서브트리
|
||||
list.push(root.val);
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
}
|
||||
|
||||
/* 중위 순회 */
|
||||
function inOrder(root) {
|
||||
if (root === null) return;
|
||||
// 방문 우선순위: 왼쪽 서브트리 -> 루트 노드 -> 오른쪽 서브트리
|
||||
inOrder(root.left);
|
||||
list.push(root.val);
|
||||
inOrder(root.right);
|
||||
}
|
||||
|
||||
/* 후위 순회 */
|
||||
function postOrder(root) {
|
||||
if (root === null) return;
|
||||
// 방문 우선순위: 왼쪽 서브트리 -> 오른쪽 서브트리 -> 루트 노드
|
||||
postOrder(root.left);
|
||||
postOrder(root.right);
|
||||
list.push(root.val);
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 이진 트리 초기화 */
|
||||
// 여기서는 배열로부터 이진 트리를 바로 생성하는 함수를 사용한다
|
||||
const root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
console.log('\n이진 트리 초기화\n');
|
||||
printTree(root);
|
||||
|
||||
/* 전위 순회 */
|
||||
list.length = 0;
|
||||
preOrder(root);
|
||||
console.log('\n전위 순회의 노드 출력 순서 = ' + list);
|
||||
|
||||
/* 중위 순회 */
|
||||
list.length = 0;
|
||||
inOrder(root);
|
||||
console.log('\n중위 순회의 노드 출력 순서 = ' + list);
|
||||
|
||||
/* 후위 순회 */
|
||||
list.length = 0;
|
||||
postOrder(root);
|
||||
console.log('\n후위 순회의 노드 출력 순서 = ' + list);
|
||||
Reference in New Issue
Block a user