mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-02 03:05:24 +08:00
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
129 lines
4.1 KiB
C#
129 lines
4.1 KiB
C#
/**
|
|
* File: array_binary_tree.cs
|
|
* Created Time: 2023-07-20
|
|
* Author: hpstory ([email protected])
|
|
*/
|
|
|
|
namespace hello_algo.chapter_tree;
|
|
|
|
/* 配列で表した二分木クラス */
|
|
public class ArrayBinaryTree(List<int?> arr) {
|
|
List<int?> tree = new(arr);
|
|
|
|
/* リストの容量 */
|
|
public int Size() {
|
|
return tree.Count;
|
|
}
|
|
|
|
/* インデックス i のノードの値を取得する */
|
|
public int? Val(int i) {
|
|
// インデックスが範囲外の場合、空きを表す null を返す
|
|
if (i < 0 || i >= Size())
|
|
return null;
|
|
return tree[i];
|
|
}
|
|
|
|
/* インデックス i のノードの左の子ノードのインデックスを取得する */
|
|
public int Left(int i) {
|
|
return 2 * i + 1;
|
|
}
|
|
|
|
/* インデックス i のノードの右の子ノードのインデックスを取得する */
|
|
public int Right(int i) {
|
|
return 2 * i + 2;
|
|
}
|
|
|
|
/* インデックス i のノードの親ノードのインデックスを取得する */
|
|
public int Parent(int i) {
|
|
return (i - 1) / 2;
|
|
}
|
|
|
|
/* レベル順走査 */
|
|
public List<int> LevelOrder() {
|
|
List<int> res = [];
|
|
// 配列をそのまま走査する
|
|
for (int i = 0; i < Size(); i++) {
|
|
if (Val(i).HasValue)
|
|
res.Add(Val(i)!.Value);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
/* 深さ優先走査 */
|
|
void DFS(int i, string order, List<int> res) {
|
|
// 空きの場合、戻る
|
|
if (!Val(i).HasValue)
|
|
return;
|
|
// 行きがけ順走査
|
|
if (order == "pre")
|
|
res.Add(Val(i)!.Value);
|
|
DFS(Left(i), order, res);
|
|
// 通りがけ順走査
|
|
if (order == "in")
|
|
res.Add(Val(i)!.Value);
|
|
DFS(Right(i), order, res);
|
|
// 帰りがけ順走査
|
|
if (order == "post")
|
|
res.Add(Val(i)!.Value);
|
|
}
|
|
|
|
/* 行きがけ順走査 */
|
|
public List<int> PreOrder() {
|
|
List<int> res = [];
|
|
DFS(0, "pre", res);
|
|
return res;
|
|
}
|
|
|
|
/* 通りがけ順走査 */
|
|
public List<int> InOrder() {
|
|
List<int> res = [];
|
|
DFS(0, "in", res);
|
|
return res;
|
|
}
|
|
|
|
/* 帰りがけ順走査 */
|
|
public List<int> PostOrder() {
|
|
List<int> res = [];
|
|
DFS(0, "post", res);
|
|
return res;
|
|
}
|
|
}
|
|
|
|
public class array_binary_tree {
|
|
[Test]
|
|
public void Test() {
|
|
// 二分木を初期化する
|
|
// ここでは配列から二分木を直接生成する関数を使う
|
|
List<int?> arr = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
|
|
|
|
TreeNode? root = TreeNode.ListToTree(arr);
|
|
Console.WriteLine("\n二分木を初期化する\n");
|
|
Console.WriteLine("二分木の配列表現:");
|
|
Console.WriteLine(arr.PrintList());
|
|
Console.WriteLine("二分木の連結リスト表現:");
|
|
PrintUtil.PrintTree(root);
|
|
|
|
// 配列で表した二分木クラス
|
|
ArrayBinaryTree abt = new(arr);
|
|
|
|
// ノードにアクセスする
|
|
int i = 1;
|
|
int l = abt.Left(i);
|
|
int r = abt.Right(i);
|
|
int p = abt.Parent(i);
|
|
Console.WriteLine("\n現在のノードのインデックスは " + i + " 、値は " + abt.Val(i));
|
|
Console.WriteLine("左の子ノードのインデックスは " + l + " 、値は " + (abt.Val(l).HasValue ? abt.Val(l) : "null"));
|
|
Console.WriteLine("右の子ノードのインデックスは " + r + " 、値は " + (abt.Val(r).HasValue ? abt.Val(r) : "null"));
|
|
Console.WriteLine("親ノードのインデックスは " + p + " 、値は " + (abt.Val(p).HasValue ? abt.Val(p) : "null"));
|
|
|
|
// 木を走査する
|
|
List<int> res = abt.LevelOrder();
|
|
Console.WriteLine("\nレベル順走査:" + res.PrintList());
|
|
res = abt.PreOrder();
|
|
Console.WriteLine("行きがけ順走査:" + res.PrintList());
|
|
res = abt.InOrder();
|
|
Console.WriteLine("通りがけ順走査:" + res.PrintList());
|
|
res = abt.PostOrder();
|
|
Console.WriteLine("帰りがけ順走査:" + res.PrintList());
|
|
}
|
|
} |