mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-08 13:21:19 +08:00
docs: add Japanese translate documents (#1812)
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
This commit is contained in:
committed by
GitHub
parent
2487a27036
commit
954c45864b
136
ja/codes/java/chapter_tree/array_binary_tree.java
Normal file
136
ja/codes/java/chapter_tree/array_binary_tree.java
Normal file
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* File: array_binary_tree.java
|
||||
* Created Time: 2023-07-19
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_tree;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
/* 配列ベースの二分木クラス */
|
||||
class ArrayBinaryTree {
|
||||
private List<Integer> tree;
|
||||
|
||||
/* コンストラクタ */
|
||||
public ArrayBinaryTree(List<Integer> arr) {
|
||||
tree = new ArrayList<>(arr);
|
||||
}
|
||||
|
||||
/* リストの容量 */
|
||||
public int size() {
|
||||
return tree.size();
|
||||
}
|
||||
|
||||
/* インデックス i のノードの値を取得 */
|
||||
public Integer val(int i) {
|
||||
// インデックスが範囲外の場合、null を返す(空の位置を表す)
|
||||
if (i < 0 || i >= size())
|
||||
return null;
|
||||
return tree.get(i);
|
||||
}
|
||||
|
||||
/* インデックス i のノードの左の子のインデックスを取得 */
|
||||
public Integer left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* インデックス i のノードの右の子のインデックスを取得 */
|
||||
public Integer right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* インデックス i のノードの親のインデックスを取得 */
|
||||
public Integer parent(int i) {
|
||||
return (i - 1) / 2;
|
||||
}
|
||||
|
||||
/* レベル順走査 */
|
||||
public List<Integer> levelOrder() {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
// 配列を走査
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (val(i) != null)
|
||||
res.add(val(i));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 深さ優先走査 */
|
||||
private void dfs(Integer i, String order, List<Integer> res) {
|
||||
// 空の位置の場合、戻る
|
||||
if (val(i) == null)
|
||||
return;
|
||||
// 前順走査
|
||||
if ("pre".equals(order))
|
||||
res.add(val(i));
|
||||
dfs(left(i), order, res);
|
||||
// 中順走査
|
||||
if ("in".equals(order))
|
||||
res.add(val(i));
|
||||
dfs(right(i), order, res);
|
||||
// 後順走査
|
||||
if ("post".equals(order))
|
||||
res.add(val(i));
|
||||
}
|
||||
|
||||
/* 前順走査 */
|
||||
public List<Integer> preOrder() {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
dfs(0, "pre", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 中順走査 */
|
||||
public List<Integer> inOrder() {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
dfs(0, "in", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 後順走査 */
|
||||
public List<Integer> postOrder() {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
dfs(0, "post", res);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class array_binary_tree {
|
||||
public static void main(String[] args) {
|
||||
// 二分木を初期化
|
||||
// 特定の関数を使用して配列を二分木に変換
|
||||
List<Integer> arr = Arrays.asList(1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15);
|
||||
|
||||
TreeNode root = TreeNode.listToTree(arr);
|
||||
System.out.println("\n二分木を初期化\n");
|
||||
System.out.println("二分木の配列表現:");
|
||||
System.out.println(arr);
|
||||
System.out.println("二分木の連結リスト表現:");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
// 配列ベースの二分木クラス
|
||||
ArrayBinaryTree abt = new ArrayBinaryTree(arr);
|
||||
|
||||
// ノードにアクセス
|
||||
int i = 1;
|
||||
Integer l = abt.left(i);
|
||||
Integer r = abt.right(i);
|
||||
Integer p = abt.parent(i);
|
||||
System.out.println("\n現在のノードのインデックスは " + i + "、値 = " + abt.val(i));
|
||||
System.out.println("その左の子のインデックスは " + l + "、値 = " + (l == null ? "null" : abt.val(l)));
|
||||
System.out.println("その右の子のインデックスは " + r + "、値 = " + (r == null ? "null" : abt.val(r)));
|
||||
System.out.println("その親のインデックスは " + p + "、値 = " + (p == null ? "null" : abt.val(p)));
|
||||
|
||||
// 木を走査
|
||||
List<Integer> res = abt.levelOrder();
|
||||
System.out.println("\nレベル順走査は:" + res);
|
||||
res = abt.preOrder();
|
||||
System.out.println("前順走査は:" + res);
|
||||
res = abt.inOrder();
|
||||
System.out.println("中順走査は:" + res);
|
||||
res = abt.postOrder();
|
||||
System.out.println("後順走査は:" + res);
|
||||
}
|
||||
}
|
||||
220
ja/codes/java/chapter_tree/avl_tree.java
Normal file
220
ja/codes/java/chapter_tree/avl_tree.java
Normal file
@@ -0,0 +1,220 @@
|
||||
/**
|
||||
* File: avl_tree.java
|
||||
* Created Time: 2022-12-10
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_tree;
|
||||
|
||||
import utils.*;
|
||||
|
||||
/* AVL木 */
|
||||
class AVLTree {
|
||||
TreeNode root; // 根ノード
|
||||
|
||||
/* ノードの高さを取得 */
|
||||
public int height(TreeNode node) {
|
||||
// 空ノードの高さは -1、葉ノードの高さは 0
|
||||
return node == null ? -1 : node.height;
|
||||
}
|
||||
|
||||
/* ノードの高さを更新 */
|
||||
private void updateHeight(TreeNode node) {
|
||||
// ノードの高さは最も高い部分木の高さ + 1
|
||||
node.height = Math.max(height(node.left), height(node.right)) + 1;
|
||||
}
|
||||
|
||||
/* 平衡因子を取得 */
|
||||
public int balanceFactor(TreeNode node) {
|
||||
// 空ノードの平衡因子は 0
|
||||
if (node == null)
|
||||
return 0;
|
||||
// ノードの平衡因子 = 左部分木の高さ - 右部分木の高さ
|
||||
return height(node.left) - height(node.right);
|
||||
}
|
||||
|
||||
/* 右回転操作 */
|
||||
private TreeNode rightRotate(TreeNode node) {
|
||||
TreeNode child = node.left;
|
||||
TreeNode grandChild = child.right;
|
||||
// child を軸として node を右に回転
|
||||
child.right = node;
|
||||
node.left = grandChild;
|
||||
// ノードの高さを更新
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 回転後の部分木の根を返す
|
||||
return child;
|
||||
}
|
||||
|
||||
/* 左回転操作 */
|
||||
private TreeNode leftRotate(TreeNode node) {
|
||||
TreeNode child = node.right;
|
||||
TreeNode grandChild = child.left;
|
||||
// child を軸として node を左に回転
|
||||
child.left = node;
|
||||
node.right = grandChild;
|
||||
// ノードの高さを更新
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 回転後の部分木の根を返す
|
||||
return child;
|
||||
}
|
||||
|
||||
/* 回転操作を実行して部分木の平衡を回復 */
|
||||
private TreeNode rotate(TreeNode node) {
|
||||
// node の平衡因子を取得
|
||||
int balanceFactor = balanceFactor(node);
|
||||
// 左傾斜の木
|
||||
if (balanceFactor > 1) {
|
||||
if (balanceFactor(node.left) >= 0) {
|
||||
// 右回転
|
||||
return rightRotate(node);
|
||||
} else {
|
||||
// 先に左回転、その後右回転
|
||||
node.left = leftRotate(node.left);
|
||||
return rightRotate(node);
|
||||
}
|
||||
}
|
||||
// 右傾斜の木
|
||||
if (balanceFactor < -1) {
|
||||
if (balanceFactor(node.right) <= 0) {
|
||||
// 左回転
|
||||
return leftRotate(node);
|
||||
} else {
|
||||
// 先に右回転、その後左回転
|
||||
node.right = rightRotate(node.right);
|
||||
return leftRotate(node);
|
||||
}
|
||||
}
|
||||
// 平衡木、回転は不要、戻る
|
||||
return node;
|
||||
}
|
||||
|
||||
/* ノードを挿入 */
|
||||
public void insert(int val) {
|
||||
root = insertHelper(root, val);
|
||||
}
|
||||
|
||||
/* 再帰的にノードを挿入(補助メソッド) */
|
||||
private TreeNode insertHelper(TreeNode node, int val) {
|
||||
if (node == null)
|
||||
return new TreeNode(val);
|
||||
/* 1. 挿入位置を見つけてノードを挿入 */
|
||||
if (val < node.val)
|
||||
node.left = insertHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
node.right = insertHelper(node.right, val);
|
||||
else
|
||||
return node; // 重複ノードは挿入しない、戻る
|
||||
updateHeight(node); // ノードの高さを更新
|
||||
/* 2. 回転操作を実行して部分木の平衡を回復 */
|
||||
node = rotate(node);
|
||||
// 部分木の根ノードを返す
|
||||
return node;
|
||||
}
|
||||
|
||||
/* ノードを削除 */
|
||||
public void remove(int val) {
|
||||
root = removeHelper(root, val);
|
||||
}
|
||||
|
||||
/* 再帰的にノードを削除(補助メソッド) */
|
||||
private TreeNode removeHelper(TreeNode node, int val) {
|
||||
if (node == null)
|
||||
return null;
|
||||
/* 1. ノードを見つけて削除 */
|
||||
if (val < node.val)
|
||||
node.left = removeHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
node.right = removeHelper(node.right, val);
|
||||
else {
|
||||
if (node.left == null || node.right == null) {
|
||||
TreeNode child = node.left != null ? node.left : node.right;
|
||||
// 子ノード数 = 0、ノードを削除して戻る
|
||||
if (child == null)
|
||||
return null;
|
||||
// 子ノード数 = 1、ノードを削除
|
||||
else
|
||||
node = child;
|
||||
} else {
|
||||
// 子ノード数 = 2、中順走査の次のノードを削除し、現在のノードをそれで置き換える
|
||||
TreeNode temp = node.right;
|
||||
while (temp.left != null) {
|
||||
temp = temp.left;
|
||||
}
|
||||
node.right = removeHelper(node.right, temp.val);
|
||||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
updateHeight(node); // ノードの高さを更新
|
||||
/* 2. 回転操作を実行して部分木の平衡を回復 */
|
||||
node = rotate(node);
|
||||
// 部分木の根ノードを返す
|
||||
return node;
|
||||
}
|
||||
|
||||
/* ノードを検索 */
|
||||
public TreeNode search(int val) {
|
||||
TreeNode cur = 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;
|
||||
}
|
||||
}
|
||||
|
||||
public class avl_tree {
|
||||
static void testInsert(AVLTree tree, int val) {
|
||||
tree.insert(val);
|
||||
System.out.println("\nノード " + val + " を挿入後、AVL木は ");
|
||||
PrintUtil.printTree(tree.root);
|
||||
}
|
||||
|
||||
static void testRemove(AVLTree tree, int val) {
|
||||
tree.remove(val);
|
||||
System.out.println("\nノード " + val + " を削除後、AVL木は ");
|
||||
PrintUtil.printTree(tree.root);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* 空のAVL木を初期化 */
|
||||
AVLTree 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 のノードを削除
|
||||
|
||||
/* ノードを検索 */
|
||||
TreeNode node = avlTree.search(7);
|
||||
System.out.println("\n見つかったノードオブジェクトは " + node + "、ノードの値 = " + node.val);
|
||||
}
|
||||
}
|
||||
158
ja/codes/java/chapter_tree/binary_search_tree.java
Normal file
158
ja/codes/java/chapter_tree/binary_search_tree.java
Normal file
@@ -0,0 +1,158 @@
|
||||
/**
|
||||
* File: binary_search_tree.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_tree;
|
||||
|
||||
import utils.*;
|
||||
|
||||
/* 二分探索木 */
|
||||
class BinarySearchTree {
|
||||
private TreeNode root;
|
||||
|
||||
/* コンストラクタ */
|
||||
public BinarySearchTree() {
|
||||
// 空の木を初期化
|
||||
root = null;
|
||||
}
|
||||
|
||||
/* 二分木の根ノードを取得 */
|
||||
public TreeNode getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
/* ノードを検索 */
|
||||
public TreeNode search(int num) {
|
||||
TreeNode cur = 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;
|
||||
}
|
||||
|
||||
/* ノードを挿入 */
|
||||
public void insert(int num) {
|
||||
// 木が空の場合、根ノードを初期化
|
||||
if (root == null) {
|
||||
root = new TreeNode(num);
|
||||
return;
|
||||
}
|
||||
TreeNode cur = 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;
|
||||
}
|
||||
// ノードを挿入
|
||||
TreeNode node = new TreeNode(num);
|
||||
if (pre.val < num)
|
||||
pre.right = node;
|
||||
else
|
||||
pre.left = node;
|
||||
}
|
||||
|
||||
/* ノードを削除 */
|
||||
public void remove(int num) {
|
||||
// 木が空の場合、戻る
|
||||
if (root == null)
|
||||
return;
|
||||
TreeNode cur = 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/その子ノード
|
||||
TreeNode child = cur.left != null ? cur.left : cur.right;
|
||||
// ノード cur を削除
|
||||
if (cur != root) {
|
||||
if (pre.left == cur)
|
||||
pre.left = child;
|
||||
else
|
||||
pre.right = child;
|
||||
} else {
|
||||
// 削除されるノードが根の場合、根を再割り当て
|
||||
root = child;
|
||||
}
|
||||
}
|
||||
// 子ノード数 = 2
|
||||
else {
|
||||
// cur の中順走査の次のノードを取得
|
||||
TreeNode tmp = cur.right;
|
||||
while (tmp.left != null) {
|
||||
tmp = tmp.left;
|
||||
}
|
||||
// 再帰的にノード tmp を削除
|
||||
remove(tmp.val);
|
||||
// cur を tmp で置き換える
|
||||
cur.val = tmp.val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class binary_search_tree {
|
||||
public static void main(String[] args) {
|
||||
/* 二分探索木を初期化 */
|
||||
BinarySearchTree bst = new BinarySearchTree();
|
||||
// 異なる挿入順序は様々な木構造を生成できることに注意。この特定の順序は完全二分木を作成する
|
||||
int[] nums = { 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };
|
||||
for (int num : nums) {
|
||||
bst.insert(num);
|
||||
}
|
||||
System.out.println("\n初期化された二分木は\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
|
||||
/* ノードを検索 */
|
||||
TreeNode node = bst.search(7);
|
||||
System.out.println("\n見つかったノードオブジェクトは " + node + "、ノードの値 = " + node.val);
|
||||
|
||||
/* ノードを挿入 */
|
||||
bst.insert(16);
|
||||
System.out.println("\nノード 16 を挿入後、二分木は\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
|
||||
/* ノードを削除 */
|
||||
bst.remove(1);
|
||||
System.out.println("\nノード 1 を削除後、二分木は\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
bst.remove(2);
|
||||
System.out.println("\nノード 2 を削除後、二分木は\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
bst.remove(4);
|
||||
System.out.println("\nノード 4 を削除後、二分木は\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
}
|
||||
}
|
||||
40
ja/codes/java/chapter_tree/binary_tree.java
Normal file
40
ja/codes/java/chapter_tree/binary_tree.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* File: binary_tree.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_tree;
|
||||
|
||||
import utils.*;
|
||||
|
||||
public class binary_tree {
|
||||
public static void main(String[] args) {
|
||||
/* 二分木を初期化 */
|
||||
// ノードを初期化
|
||||
TreeNode n1 = new TreeNode(1);
|
||||
TreeNode n2 = new TreeNode(2);
|
||||
TreeNode n3 = new TreeNode(3);
|
||||
TreeNode n4 = new TreeNode(4);
|
||||
TreeNode n5 = new TreeNode(5);
|
||||
// ノードの参照(ポインタ)を構築
|
||||
n1.left = n2;
|
||||
n1.right = n3;
|
||||
n2.left = n4;
|
||||
n2.right = n5;
|
||||
System.out.println("\n二分木を初期化\n");
|
||||
PrintUtil.printTree(n1);
|
||||
|
||||
/* ノードの挿入と削除 */
|
||||
TreeNode P = new TreeNode(0);
|
||||
// ノード P を n1 -> n2 の間に挿入
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
System.out.println("\nノード P を挿入後\n");
|
||||
PrintUtil.printTree(n1);
|
||||
// ノード P を削除
|
||||
n1.left = n2;
|
||||
System.out.println("\nノード P を削除後\n");
|
||||
PrintUtil.printTree(n1);
|
||||
}
|
||||
}
|
||||
42
ja/codes/java/chapter_tree/binary_tree_bfs.java
Normal file
42
ja/codes/java/chapter_tree/binary_tree_bfs.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* File: binary_tree_bfs.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_tree;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class binary_tree_bfs {
|
||||
/* レベル順走査 */
|
||||
static List<Integer> levelOrder(TreeNode root) {
|
||||
// キューを初期化し、根ノードを追加
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
// 走査順序を格納するリストを初期化
|
||||
List<Integer> list = new ArrayList<>();
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll(); // キューのデキュー
|
||||
list.add(node.val); // ノードの値を保存
|
||||
if (node.left != null)
|
||||
queue.offer(node.left); // 左の子ノードをエンキュー
|
||||
if (node.right != null)
|
||||
queue.offer(node.right); // 右の子ノードをエンキュー
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* 二分木を初期化 */
|
||||
// 特定の関数を使用して配列を二分木に変換
|
||||
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
|
||||
System.out.println("\n二分木を初期化\n");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
/* レベル順走査 */
|
||||
List<Integer> list = levelOrder(root);
|
||||
System.out.println("\nレベル順走査でのノードの出力順序 = " + list);
|
||||
}
|
||||
}
|
||||
68
ja/codes/java/chapter_tree/binary_tree_dfs.java
Normal file
68
ja/codes/java/chapter_tree/binary_tree_dfs.java
Normal file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* File: binary_tree_dfs.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_tree;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class binary_tree_dfs {
|
||||
// 走査順序を格納するリストを初期化
|
||||
static ArrayList<Integer> list = new ArrayList<>();
|
||||
|
||||
/* 前順走査 */
|
||||
static void preOrder(TreeNode root) {
|
||||
if (root == null)
|
||||
return;
|
||||
// 訪問優先度: 根ノード -> 左部分木 -> 右部分木
|
||||
list.add(root.val);
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
}
|
||||
|
||||
/* 中順走査 */
|
||||
static void inOrder(TreeNode root) {
|
||||
if (root == null)
|
||||
return;
|
||||
// 訪問優先度: 左部分木 -> 根ノード -> 右部分木
|
||||
inOrder(root.left);
|
||||
list.add(root.val);
|
||||
inOrder(root.right);
|
||||
}
|
||||
|
||||
/* 後順走査 */
|
||||
static void postOrder(TreeNode root) {
|
||||
if (root == null)
|
||||
return;
|
||||
// 訪問優先度: 左部分木 -> 右部分木 -> 根ノード
|
||||
postOrder(root.left);
|
||||
postOrder(root.right);
|
||||
list.add(root.val);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* 二分木を初期化 */
|
||||
// 特定の関数を使用して配列を二分木に変換
|
||||
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
|
||||
System.out.println("\n二分木を初期化\n");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
/* 前順走査 */
|
||||
list.clear();
|
||||
preOrder(root);
|
||||
System.out.println("\n前順走査でのノードの出力順序 = " + list);
|
||||
|
||||
/* 中順走査 */
|
||||
list.clear();
|
||||
inOrder(root);
|
||||
System.out.println("\n中順走査でのノードの出力順序 = " + list);
|
||||
|
||||
/* 後順走査 */
|
||||
list.clear();
|
||||
postOrder(root);
|
||||
System.out.println("\n後順走査でのノードの出力順序 = " + list);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user