New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(csharp): add csharp code for array binary tree #632
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/** | ||
* File: array_binary_tree.cs | ||
* Created Time: 2023-07-20 | ||
* Author: hpstory (hpstory1024@163.com) | ||
*/ | ||
|
||
namespace hello_algo.chapter_tree; | ||
|
||
/* 数组表示下的二叉树类 */ | ||
public class ArrayBinaryTree { | ||
private List<int?> tree; | ||
|
||
/* 构造方法 */ | ||
public ArrayBinaryTree(List<int?> arr) { | ||
tree = new List<int?>(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 = new List<int>(); | ||
// 直接遍历数组 | ||
for (int i = 0; i < size(); i++) { | ||
if (val(i).HasValue) | ||
res.Add(val(i).Value); | ||
} | ||
return res; | ||
} | ||
|
||
/* 深度优先遍历 */ | ||
private 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 = new List<int>(); | ||
dfs(0, "pre", res); | ||
return res; | ||
} | ||
|
||
/* 中序遍历 */ | ||
public List<int> inOrder() { | ||
List<int> res = new List<int>(); | ||
dfs(0, "in", res); | ||
return res; | ||
} | ||
|
||
/* 后序遍历 */ | ||
public List<int> postOrder() { | ||
List<int> res = new List<int>(); | ||
dfs(0, "post", res); | ||
return res; | ||
} | ||
} | ||
|
||
public class array_binary_tree { | ||
[Test] | ||
public void Test() { | ||
// 初始化二叉树 | ||
// 这里借助了一个从数组直接生成二叉树的函数 | ||
List<int?> arr = new List<int?> { 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 ArrayBinaryTree(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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part of code is different with others. Because ArrayBinaryTree class doesn't have a default constructor for test framework, resulting in a test failure, so move it outside of the class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. C#'s rule is the same with Java.