2023-07-20 18:54:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: array_binary_tree.cs
|
|
|
|
|
* Created Time: 2023-07-20
|
|
|
|
|
* Author: hpstory (hpstory1024@163.com)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace hello_algo.chapter_tree;
|
|
|
|
|
|
|
|
|
|
/* 数组表示下的二叉树类 */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
public class ArrayBinaryTree(List<int?> arr) {
|
|
|
|
|
List<int?> tree = new(arr);
|
2023-07-20 18:54:40 +08:00
|
|
|
|
|
|
|
|
|
/* 节点数量 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public int Size() {
|
2023-07-20 18:54:40 +08:00
|
|
|
|
return tree.Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的值 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public int? Val(int i) {
|
2023-07-20 18:54:40 +08:00
|
|
|
|
// 若索引越界,则返回 null ,代表空位
|
2023-10-08 01:33:46 +08:00
|
|
|
|
if (i < 0 || i >= Size())
|
2023-07-20 18:54:40 +08:00
|
|
|
|
return null;
|
|
|
|
|
return tree[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的左子节点的索引 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public int Left(int i) {
|
2023-07-20 18:54:40 +08:00
|
|
|
|
return 2 * i + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的右子节点的索引 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public int Right(int i) {
|
2023-07-20 18:54:40 +08:00
|
|
|
|
return 2 * i + 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的父节点的索引 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public int Parent(int i) {
|
2023-07-20 18:54:40 +08:00
|
|
|
|
return (i - 1) / 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 层序遍历 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public List<int> LevelOrder() {
|
2023-11-26 23:18:44 +08:00
|
|
|
|
List<int> res = [];
|
2023-07-20 18:54:40 +08:00
|
|
|
|
// 直接遍历数组
|
2023-10-08 01:33:46 +08:00
|
|
|
|
for (int i = 0; i < Size(); i++) {
|
|
|
|
|
if (Val(i).HasValue)
|
2023-11-26 23:18:44 +08:00
|
|
|
|
res.Add(Val(i)!.Value);
|
2023-07-20 18:54:40 +08:00
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 深度优先遍历 */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
void DFS(int i, string order, List<int> res) {
|
2023-07-20 18:54:40 +08:00
|
|
|
|
// 若为空位,则返回
|
2023-10-08 01:33:46 +08:00
|
|
|
|
if (!Val(i).HasValue)
|
2023-07-20 18:54:40 +08:00
|
|
|
|
return;
|
|
|
|
|
// 前序遍历
|
|
|
|
|
if (order == "pre")
|
2023-11-26 23:18:44 +08:00
|
|
|
|
res.Add(Val(i)!.Value);
|
2023-10-15 21:18:09 +08:00
|
|
|
|
DFS(Left(i), order, res);
|
2023-07-20 18:54:40 +08:00
|
|
|
|
// 中序遍历
|
|
|
|
|
if (order == "in")
|
2023-11-26 23:18:44 +08:00
|
|
|
|
res.Add(Val(i)!.Value);
|
2023-10-15 21:18:09 +08:00
|
|
|
|
DFS(Right(i), order, res);
|
2023-07-20 18:54:40 +08:00
|
|
|
|
// 后序遍历
|
|
|
|
|
if (order == "post")
|
2023-11-26 23:18:44 +08:00
|
|
|
|
res.Add(Val(i)!.Value);
|
2023-07-20 18:54:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 前序遍历 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public List<int> PreOrder() {
|
2023-11-26 23:18:44 +08:00
|
|
|
|
List<int> res = [];
|
2023-10-15 21:18:09 +08:00
|
|
|
|
DFS(0, "pre", res);
|
2023-07-20 18:54:40 +08:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public List<int> InOrder() {
|
2023-11-26 23:18:44 +08:00
|
|
|
|
List<int> res = [];
|
2023-10-15 21:18:09 +08:00
|
|
|
|
DFS(0, "in", res);
|
2023-07-20 18:54:40 +08:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public List<int> PostOrder() {
|
2023-11-26 23:18:44 +08:00
|
|
|
|
List<int> res = [];
|
2023-10-15 21:18:09 +08:00
|
|
|
|
DFS(0, "post", res);
|
2023-07-20 18:54:40 +08:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class array_binary_tree {
|
|
|
|
|
[Test]
|
|
|
|
|
public void Test() {
|
|
|
|
|
// 初始化二叉树
|
|
|
|
|
// 这里借助了一个从数组直接生成二叉树的函数
|
2023-11-26 23:18:44 +08:00
|
|
|
|
List<int?> arr = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
|
2023-07-20 18:54:40 +08:00
|
|
|
|
|
2023-11-26 23:18:44 +08:00
|
|
|
|
TreeNode? root = TreeNode.ListToTree(arr);
|
2023-07-20 18:54:40 +08:00
|
|
|
|
Console.WriteLine("\n初始化二叉树\n");
|
|
|
|
|
Console.WriteLine("二叉树的数组表示:");
|
|
|
|
|
Console.WriteLine(arr.PrintList());
|
|
|
|
|
Console.WriteLine("二叉树的链表表示:");
|
|
|
|
|
PrintUtil.PrintTree(root);
|
|
|
|
|
|
|
|
|
|
// 数组表示下的二叉树类
|
2023-10-08 01:33:46 +08:00
|
|
|
|
ArrayBinaryTree abt = new(arr);
|
2023-07-20 18:54:40 +08:00
|
|
|
|
|
|
|
|
|
// 访问节点
|
|
|
|
|
int i = 1;
|
2023-10-08 01:33:46 +08:00
|
|
|
|
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"));
|
2023-07-20 18:54:40 +08:00
|
|
|
|
|
|
|
|
|
// 遍历树
|
2023-10-08 01:33:46 +08:00
|
|
|
|
List<int> res = abt.LevelOrder();
|
2023-07-20 18:54:40 +08:00
|
|
|
|
Console.WriteLine("\n层序遍历为:" + res.PrintList());
|
2023-10-08 01:33:46 +08:00
|
|
|
|
res = abt.PreOrder();
|
2023-07-20 18:54:40 +08:00
|
|
|
|
Console.WriteLine("前序遍历为:" + res.PrintList());
|
2023-10-08 01:33:46 +08:00
|
|
|
|
res = abt.InOrder();
|
2023-07-20 18:54:40 +08:00
|
|
|
|
Console.WriteLine("中序遍历为:" + res.PrintList());
|
2023-10-08 01:33:46 +08:00
|
|
|
|
res = abt.PostOrder();
|
2023-07-20 18:54:40 +08:00
|
|
|
|
Console.WriteLine("后序遍历为:" + res.PrintList());
|
|
|
|
|
}
|
|
|
|
|
}
|