mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-15 22:57:48 +08:00
Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
This commit is contained in:
147
ja/codes/javascript/chapter_tree/array_binary_tree.js
Normal file
147
ja/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;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
// 二分木を初期化
|
||||
// ここでは、配列から直接二分木を生成する関数を利用する
|
||||
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
ja/codes/javascript/chapter_tree/avl_tree.js
Normal file
208
ja/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);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 空の 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
ja/codes/javascript/chapter_tree/binary_search_tree.js
Normal file
139
ja/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 or 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 二分探索木を初期化 */
|
||||
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
ja/codes/javascript/chapter_tree/binary_tree.js
Normal file
35
ja/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
ja/codes/javascript/chapter_tree/binary_tree_bfs.js
Normal file
34
ja/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;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 二分木を初期化 */
|
||||
// ここでは、配列から直接二分木を生成する関数を利用する
|
||||
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
ja/codes/javascript/chapter_tree/binary_tree_dfs.js
Normal file
60
ja/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);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 二分木を初期化 */
|
||||
// ここでは、配列から直接二分木を生成する関数を利用する
|
||||
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