mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-16 16:56:06 +00:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* File: array_binary_tree.cs
|
||||
* Created Time: 2023-07-20
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_tree;
|
||||
|
||||
/* Binary tree class represented by array */
|
||||
public class ArrayBinaryTree(List<int?> arr) {
|
||||
List<int?> tree = new(arr);
|
||||
|
||||
/* List capacity */
|
||||
public int Size() {
|
||||
return tree.Count;
|
||||
}
|
||||
|
||||
/* Get value of node at index i */
|
||||
public int? Val(int i) {
|
||||
// If index out of bounds, return null to represent empty position
|
||||
if (i < 0 || i >= Size())
|
||||
return null;
|
||||
return tree[i];
|
||||
}
|
||||
|
||||
/* Get index of left child node of node at index i */
|
||||
public int Left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* Get index of right child node of node at index i */
|
||||
public int Right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* Get index of parent node of node at index i */
|
||||
public int Parent(int i) {
|
||||
return (i - 1) / 2;
|
||||
}
|
||||
|
||||
/* Level-order traversal */
|
||||
public List<int> LevelOrder() {
|
||||
List<int> res = [];
|
||||
// Traverse array directly
|
||||
for (int i = 0; i < Size(); i++) {
|
||||
if (Val(i).HasValue)
|
||||
res.Add(Val(i)!.Value);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Depth-first traversal */
|
||||
void DFS(int i, string order, List<int> res) {
|
||||
// If empty position, return
|
||||
if (!Val(i).HasValue)
|
||||
return;
|
||||
// Preorder traversal
|
||||
if (order == "pre")
|
||||
res.Add(Val(i)!.Value);
|
||||
DFS(Left(i), order, res);
|
||||
// Inorder traversal
|
||||
if (order == "in")
|
||||
res.Add(Val(i)!.Value);
|
||||
DFS(Right(i), order, res);
|
||||
// Postorder traversal
|
||||
if (order == "post")
|
||||
res.Add(Val(i)!.Value);
|
||||
}
|
||||
|
||||
/* Preorder traversal */
|
||||
public List<int> PreOrder() {
|
||||
List<int> res = [];
|
||||
DFS(0, "pre", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Inorder traversal */
|
||||
public List<int> InOrder() {
|
||||
List<int> res = [];
|
||||
DFS(0, "in", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Postorder traversal */
|
||||
public List<int> PostOrder() {
|
||||
List<int> res = [];
|
||||
DFS(0, "post", res);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class array_binary_tree {
|
||||
[Test]
|
||||
public void Test() {
|
||||
// Initialize binary tree
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
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("\nInitialize binary tree\n");
|
||||
Console.WriteLine("Array representation of binary tree:");
|
||||
Console.WriteLine(arr.PrintList());
|
||||
Console.WriteLine("Linked list representation of binary tree:");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// Binary tree class represented by array
|
||||
ArrayBinaryTree abt = new(arr);
|
||||
|
||||
// Access node
|
||||
int i = 1;
|
||||
int l = abt.Left(i);
|
||||
int r = abt.Right(i);
|
||||
int p = abt.Parent(i);
|
||||
Console.WriteLine("\nCurrent node index is " + i + ", value is " + abt.Val(i));
|
||||
Console.WriteLine("Its left child node index is " + l + ", value is " + (abt.Val(l).HasValue ? abt.Val(l) : "null"));
|
||||
Console.WriteLine("Its right child node index is " + r + ", value is " + (abt.Val(r).HasValue ? abt.Val(r) : "null"));
|
||||
Console.WriteLine("Its parent node index is " + p + ", value is " + (abt.Val(p).HasValue ? abt.Val(p) : "null"));
|
||||
|
||||
// Traverse tree
|
||||
List<int> res = abt.LevelOrder();
|
||||
Console.WriteLine("\nLevel-order traversal is:" + res.PrintList());
|
||||
res = abt.PreOrder();
|
||||
Console.WriteLine("Preorder traversal is:" + res.PrintList());
|
||||
res = abt.InOrder();
|
||||
Console.WriteLine("Inorder traversal is:" + res.PrintList());
|
||||
res = abt.PostOrder();
|
||||
Console.WriteLine("Postorder traversal is:" + res.PrintList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
/**
|
||||
* File: avl_tree.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_tree;
|
||||
|
||||
/* AVL tree */
|
||||
class AVLTree {
|
||||
public TreeNode? root; // Root node
|
||||
|
||||
/* Get node height */
|
||||
int Height(TreeNode? node) {
|
||||
// Empty node height is -1, leaf node height is 0
|
||||
return node == null ? -1 : node.height;
|
||||
}
|
||||
|
||||
/* Update node height */
|
||||
void UpdateHeight(TreeNode node) {
|
||||
// Node height equals the height of the tallest subtree + 1
|
||||
node.height = Math.Max(Height(node.left), Height(node.right)) + 1;
|
||||
}
|
||||
|
||||
/* Get balance factor */
|
||||
public int BalanceFactor(TreeNode? node) {
|
||||
// Empty node balance factor is 0
|
||||
if (node == null) return 0;
|
||||
// Node balance factor = left subtree height - right subtree height
|
||||
return Height(node.left) - Height(node.right);
|
||||
}
|
||||
|
||||
/* Right rotation operation */
|
||||
TreeNode? RightRotate(TreeNode? node) {
|
||||
TreeNode? child = node?.left;
|
||||
TreeNode? grandChild = child?.right;
|
||||
// Using child as pivot, rotate node to the right
|
||||
child.right = node;
|
||||
node.left = grandChild;
|
||||
// Update node height
|
||||
UpdateHeight(node);
|
||||
UpdateHeight(child);
|
||||
// Return root node of subtree after rotation
|
||||
return child;
|
||||
}
|
||||
|
||||
/* Left rotation operation */
|
||||
TreeNode? LeftRotate(TreeNode? node) {
|
||||
TreeNode? child = node?.right;
|
||||
TreeNode? grandChild = child?.left;
|
||||
// Using child as pivot, rotate node to the left
|
||||
child.left = node;
|
||||
node.right = grandChild;
|
||||
// Update node height
|
||||
UpdateHeight(node);
|
||||
UpdateHeight(child);
|
||||
// Return root node of subtree after rotation
|
||||
return child;
|
||||
}
|
||||
|
||||
/* Perform rotation operation to restore balance to this subtree */
|
||||
TreeNode? Rotate(TreeNode? node) {
|
||||
// Get balance factor of node
|
||||
int balanceFactorInt = BalanceFactor(node);
|
||||
// Left-leaning tree
|
||||
if (balanceFactorInt > 1) {
|
||||
if (BalanceFactor(node?.left) >= 0) {
|
||||
// Right rotation
|
||||
return RightRotate(node);
|
||||
} else {
|
||||
// First left rotation then right rotation
|
||||
node!.left = LeftRotate(node!.left);
|
||||
return RightRotate(node);
|
||||
}
|
||||
}
|
||||
// Right-leaning tree
|
||||
if (balanceFactorInt < -1) {
|
||||
if (BalanceFactor(node?.right) <= 0) {
|
||||
// Left rotation
|
||||
return LeftRotate(node);
|
||||
} else {
|
||||
// First right rotation then left rotation
|
||||
node!.right = RightRotate(node!.right);
|
||||
return LeftRotate(node);
|
||||
}
|
||||
}
|
||||
// Balanced tree, no rotation needed, return directly
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Insert node */
|
||||
public void Insert(int val) {
|
||||
root = InsertHelper(root, val);
|
||||
}
|
||||
|
||||
/* Recursively insert node (helper method) */
|
||||
TreeNode? InsertHelper(TreeNode? node, int val) {
|
||||
if (node == null) return new TreeNode(val);
|
||||
/* 1. Find insertion position and insert node */
|
||||
if (val < node.val)
|
||||
node.left = InsertHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
node.right = InsertHelper(node.right, val);
|
||||
else
|
||||
return node; // Duplicate node not inserted, return directly
|
||||
UpdateHeight(node); // Update node height
|
||||
/* 2. Perform rotation operation to restore balance to this subtree */
|
||||
node = Rotate(node);
|
||||
// Return root node of subtree
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Remove node */
|
||||
public void Remove(int val) {
|
||||
root = RemoveHelper(root, val);
|
||||
}
|
||||
|
||||
/* Recursively delete node (helper method) */
|
||||
TreeNode? RemoveHelper(TreeNode? node, int val) {
|
||||
if (node == null) return null;
|
||||
/* 1. Find node and delete */
|
||||
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 ?? node.right;
|
||||
// Number of child nodes = 0, delete node directly and return
|
||||
if (child == null)
|
||||
return null;
|
||||
// Number of child nodes = 1, delete node directly
|
||||
else
|
||||
node = child;
|
||||
} else {
|
||||
// Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it
|
||||
TreeNode? temp = node.right;
|
||||
while (temp.left != null) {
|
||||
temp = temp.left;
|
||||
}
|
||||
node.right = RemoveHelper(node.right, temp.val!.Value);
|
||||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
UpdateHeight(node); // Update node height
|
||||
/* 2. Perform rotation operation to restore balance to this subtree */
|
||||
node = Rotate(node);
|
||||
// Return root node of subtree
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Search node */
|
||||
public TreeNode? Search(int val) {
|
||||
TreeNode? cur = root;
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur != null) {
|
||||
// Target node is in cur's right subtree
|
||||
if (cur.val < val)
|
||||
cur = cur.right;
|
||||
// Target node is in cur's left subtree
|
||||
else if (cur.val > val)
|
||||
cur = cur.left;
|
||||
// Found target node, exit loop
|
||||
else
|
||||
break;
|
||||
}
|
||||
// Return target node
|
||||
return cur;
|
||||
}
|
||||
}
|
||||
|
||||
public class avl_tree {
|
||||
static void TestInsert(AVLTree tree, int val) {
|
||||
tree.Insert(val);
|
||||
Console.WriteLine("\nInsert node " + val + ", AVL tree is");
|
||||
PrintUtil.PrintTree(tree.root);
|
||||
}
|
||||
|
||||
static void TestRemove(AVLTree tree, int val) {
|
||||
tree.Remove(val);
|
||||
Console.WriteLine("\nRemove node " + val + ", AVL tree is");
|
||||
PrintUtil.PrintTree(tree.root);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Please pay attention to how the AVL tree maintains balance after inserting nodes */
|
||||
AVLTree avlTree = new();
|
||||
|
||||
/* Insert node */
|
||||
// Delete nodes
|
||||
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);
|
||||
|
||||
/* Please pay attention to how the AVL tree maintains balance after deleting nodes */
|
||||
TestInsert(avlTree, 7);
|
||||
|
||||
/* Remove node */
|
||||
// Delete node with degree 1
|
||||
TestRemove(avlTree, 8); // Delete node with degree 2
|
||||
TestRemove(avlTree, 5); // Remove node with degree 1
|
||||
TestRemove(avlTree, 4); // Remove node with degree 2
|
||||
|
||||
/* Search node */
|
||||
TreeNode? node = avlTree.Search(7);
|
||||
Console.WriteLine("\nFound node object is " + node + ", node value = " + node?.val);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* File: binary_search_tree.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_tree;
|
||||
|
||||
class BinarySearchTree {
|
||||
TreeNode? root;
|
||||
|
||||
public BinarySearchTree() {
|
||||
// Initialize empty tree
|
||||
root = null;
|
||||
}
|
||||
|
||||
/* Get binary tree root node */
|
||||
public TreeNode? GetRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Search node */
|
||||
public TreeNode? Search(int num) {
|
||||
TreeNode? cur = root;
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur != null) {
|
||||
// Target node is in cur's right subtree
|
||||
if (cur.val < num) cur =
|
||||
cur.right;
|
||||
// Target node is in cur's left subtree
|
||||
else if (cur.val > num)
|
||||
cur = cur.left;
|
||||
// Found target node, exit loop
|
||||
else
|
||||
break;
|
||||
}
|
||||
// Return target node
|
||||
return cur;
|
||||
}
|
||||
|
||||
/* Insert node */
|
||||
public void Insert(int num) {
|
||||
// If tree is empty, initialize root node
|
||||
if (root == null) {
|
||||
root = new TreeNode(num);
|
||||
return;
|
||||
}
|
||||
TreeNode? cur = root, pre = null;
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur != null) {
|
||||
// Found duplicate node, return directly
|
||||
if (cur.val == num)
|
||||
return;
|
||||
pre = cur;
|
||||
// Insertion position is in cur's right subtree
|
||||
if (cur.val < num)
|
||||
cur = cur.right;
|
||||
// Insertion position is in cur's left subtree
|
||||
else
|
||||
cur = cur.left;
|
||||
}
|
||||
|
||||
// Insert node
|
||||
TreeNode node = new(num);
|
||||
if (pre != null) {
|
||||
if (pre.val < num)
|
||||
pre.right = node;
|
||||
else
|
||||
pre.left = node;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Remove node */
|
||||
public void Remove(int num) {
|
||||
// If tree is empty, return directly
|
||||
if (root == null)
|
||||
return;
|
||||
TreeNode? cur = root, pre = null;
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur != null) {
|
||||
// Found node to delete, exit loop
|
||||
if (cur.val == num)
|
||||
break;
|
||||
pre = cur;
|
||||
// Node to delete is in cur's right subtree
|
||||
if (cur.val < num)
|
||||
cur = cur.right;
|
||||
// Node to delete is in cur's left subtree
|
||||
else
|
||||
cur = cur.left;
|
||||
}
|
||||
// If no node to delete, return directly
|
||||
if (cur == null)
|
||||
return;
|
||||
// Number of child nodes = 0 or 1
|
||||
if (cur.left == null || cur.right == null) {
|
||||
// When number of child nodes = 0 / 1, child = null / that child node
|
||||
TreeNode? child = cur.left ?? cur.right;
|
||||
// Delete node cur
|
||||
if (cur != root) {
|
||||
if (pre!.left == cur)
|
||||
pre.left = child;
|
||||
else
|
||||
pre.right = child;
|
||||
} else {
|
||||
// If deleted node is root node, reassign root node
|
||||
root = child;
|
||||
}
|
||||
}
|
||||
// Number of child nodes = 2
|
||||
else {
|
||||
// Get next node of cur in inorder traversal
|
||||
TreeNode? tmp = cur.right;
|
||||
while (tmp.left != null) {
|
||||
tmp = tmp.left;
|
||||
}
|
||||
// Recursively delete node tmp
|
||||
Remove(tmp.val!.Value);
|
||||
// Replace cur with tmp
|
||||
cur.val = tmp.val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class binary_search_tree {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Initialize binary search tree */
|
||||
BinarySearchTree bst = new();
|
||||
// Please note that different insertion orders will generate different binary trees, this sequence can generate a perfect binary tree
|
||||
int[] nums = [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15];
|
||||
foreach (int num in nums) {
|
||||
bst.Insert(num);
|
||||
}
|
||||
|
||||
Console.WriteLine("\nInitialized binary tree is\n");
|
||||
PrintUtil.PrintTree(bst.GetRoot());
|
||||
|
||||
/* Search node */
|
||||
TreeNode? node = bst.Search(7);
|
||||
Console.WriteLine("\nFound node object is " + node + ", node value = " + node?.val);
|
||||
|
||||
/* Insert node */
|
||||
bst.Insert(16);
|
||||
Console.WriteLine("\nAfter inserting node 16, binary tree is\n");
|
||||
PrintUtil.PrintTree(bst.GetRoot());
|
||||
|
||||
/* Remove node */
|
||||
bst.Remove(1);
|
||||
Console.WriteLine("\nAfter removing node 1, binary tree is\n");
|
||||
PrintUtil.PrintTree(bst.GetRoot());
|
||||
bst.Remove(2);
|
||||
Console.WriteLine("\nAfter removing node 2, binary tree is\n");
|
||||
PrintUtil.PrintTree(bst.GetRoot());
|
||||
bst.Remove(4);
|
||||
Console.WriteLine("\nAfter removing node 4, binary tree is\n");
|
||||
PrintUtil.PrintTree(bst.GetRoot());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* File: binary_tree.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_tree;
|
||||
|
||||
public class binary_tree {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Initialize binary tree */
|
||||
// Initialize nodes
|
||||
TreeNode n1 = new(1);
|
||||
TreeNode n2 = new(2);
|
||||
TreeNode n3 = new(3);
|
||||
TreeNode n4 = new(4);
|
||||
TreeNode n5 = new(5);
|
||||
// Build references (pointers) between nodes
|
||||
n1.left = n2;
|
||||
n1.right = n3;
|
||||
n2.left = n4;
|
||||
n2.right = n5;
|
||||
Console.WriteLine("\nInitialize binary tree\n");
|
||||
PrintUtil.PrintTree(n1);
|
||||
|
||||
/* Insert node P between n1 -> n2 */
|
||||
TreeNode P = new(0);
|
||||
// Delete node
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
Console.WriteLine("\nAfter inserting node P\n");
|
||||
PrintUtil.PrintTree(n1);
|
||||
// Remove node P
|
||||
n1.left = n2;
|
||||
Console.WriteLine("\nAfter removing node P\n");
|
||||
PrintUtil.PrintTree(n1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* File: binary_tree_bfs.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_tree;
|
||||
|
||||
public class binary_tree_bfs {
|
||||
|
||||
/* Level-order traversal */
|
||||
List<int> LevelOrder(TreeNode root) {
|
||||
// Initialize queue, add root node
|
||||
Queue<TreeNode> queue = new();
|
||||
queue.Enqueue(root);
|
||||
// Initialize a list to save the traversal sequence
|
||||
List<int> list = [];
|
||||
while (queue.Count != 0) {
|
||||
TreeNode node = queue.Dequeue(); // Dequeue
|
||||
list.Add(node.val!.Value); // Save node value
|
||||
if (node.left != null)
|
||||
queue.Enqueue(node.left); // Left child node enqueue
|
||||
if (node.right != null)
|
||||
queue.Enqueue(node.right); // Right child node enqueue
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Initialize binary tree */
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
TreeNode? root = TreeNode.ListToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\nInitialize binary tree\n");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
List<int> list = LevelOrder(root!);
|
||||
Console.WriteLine("\nLevel-order traversal node print sequence = " + string.Join(",", list));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* File: binary_tree_dfs.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_tree;
|
||||
|
||||
public class binary_tree_dfs {
|
||||
List<int> list = [];
|
||||
|
||||
/* Preorder traversal */
|
||||
void PreOrder(TreeNode? root) {
|
||||
if (root == null) return;
|
||||
// Visit priority: root node -> left subtree -> right subtree
|
||||
list.Add(root.val!.Value);
|
||||
PreOrder(root.left);
|
||||
PreOrder(root.right);
|
||||
}
|
||||
|
||||
/* Inorder traversal */
|
||||
void InOrder(TreeNode? root) {
|
||||
if (root == null) return;
|
||||
// Visit priority: left subtree -> root node -> right subtree
|
||||
InOrder(root.left);
|
||||
list.Add(root.val!.Value);
|
||||
InOrder(root.right);
|
||||
}
|
||||
|
||||
/* Postorder traversal */
|
||||
void PostOrder(TreeNode? root) {
|
||||
if (root == null) return;
|
||||
// Visit priority: left subtree -> right subtree -> root node
|
||||
PostOrder(root.left);
|
||||
PostOrder(root.right);
|
||||
list.Add(root.val!.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Initialize binary tree */
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
TreeNode? root = TreeNode.ListToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\nInitialize binary tree\n");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
list.Clear();
|
||||
PreOrder(root);
|
||||
Console.WriteLine("\nPreorder traversal node print sequence = " + string.Join(",", list));
|
||||
|
||||
list.Clear();
|
||||
InOrder(root);
|
||||
Console.WriteLine("\nInorder traversal node print sequence = " + string.Join(",", list));
|
||||
|
||||
list.Clear();
|
||||
PostOrder(root);
|
||||
Console.WriteLine("\nPostorder traversal node print sequence = " + string.Join(",", list));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user