mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00:36 +08:00
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
40 lines
1.0 KiB
C#
40 lines
1.0 KiB
C#
/**
|
|
* 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 the binary tree */
|
|
// Initialize the nodes
|
|
TreeNode n1 = new(1);
|
|
TreeNode n2 = new(2);
|
|
TreeNode n3 = new(3);
|
|
TreeNode n4 = new(4);
|
|
TreeNode n5 = new(5);
|
|
// Set up references (pointers) between the nodes
|
|
n1.left = n2;
|
|
n1.right = n3;
|
|
n2.left = n4;
|
|
n2.right = n5;
|
|
Console.WriteLine("\nInitialize the binary tree\n");
|
|
PrintUtil.PrintTree(n1);
|
|
|
|
/* Insert and remove nodes */
|
|
TreeNode P = new(0);
|
|
// Insert node P between n1 and n2 in n1 -> n2
|
|
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);
|
|
}
|
|
}
|