mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 06:07:20 +08:00 
			
		
		
		
	feat(csharp): fix binary_search_tree code (#712)
* feat(csharp): fix binary_search_tree code * Update binary_search_tree.cs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
		@ -9,9 +9,9 @@ namespace hello_algo.chapter_tree;
 | 
				
			|||||||
class BinarySearchTree {
 | 
					class BinarySearchTree {
 | 
				
			||||||
    TreeNode? root;
 | 
					    TreeNode? root;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public BinarySearchTree(int[] nums) {
 | 
					    public BinarySearchTree() {
 | 
				
			||||||
        Array.Sort(nums); // 排序数组
 | 
					        // 初始化空树
 | 
				
			||||||
        root = buildTree(nums, 0, nums.Length - 1);  // 构建二叉搜索树
 | 
					        root = null;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 获取二叉树根节点 */
 | 
					    /* 获取二叉树根节点 */
 | 
				
			||||||
@ -19,20 +19,6 @@ class BinarySearchTree {
 | 
				
			|||||||
        return root;
 | 
					        return root;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 构建二叉搜索树 */
 | 
					 | 
				
			||||||
    public TreeNode? buildTree(int[] nums, int i, int j) {
 | 
					 | 
				
			||||||
        if (i > j)
 | 
					 | 
				
			||||||
            return null;
 | 
					 | 
				
			||||||
        // 将数组中间节点作为根节点
 | 
					 | 
				
			||||||
        int mid = (i + j) / 2;
 | 
					 | 
				
			||||||
        TreeNode root = new TreeNode(nums[mid]);
 | 
					 | 
				
			||||||
        // 递归建立左子树和右子树
 | 
					 | 
				
			||||||
        root.left = buildTree(nums, i, mid - 1);
 | 
					 | 
				
			||||||
        root.right = buildTree(nums, mid + 1, j);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return root;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /* 查找节点 */
 | 
					    /* 查找节点 */
 | 
				
			||||||
    public TreeNode? search(int num) {
 | 
					    public TreeNode? search(int num) {
 | 
				
			||||||
        TreeNode? cur = root;
 | 
					        TreeNode? cur = root;
 | 
				
			||||||
@ -141,8 +127,13 @@ public class binary_search_tree {
 | 
				
			|||||||
    [Test]
 | 
					    [Test]
 | 
				
			||||||
    public void Test() {
 | 
					    public void Test() {
 | 
				
			||||||
        /* 初始化二叉搜索树 */
 | 
					        /* 初始化二叉搜索树 */
 | 
				
			||||||
        int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
 | 
					        BinarySearchTree bst = new BinarySearchTree();
 | 
				
			||||||
        BinarySearchTree bst = new BinarySearchTree(nums);
 | 
					        // 请注意,不同的插入顺序会生成不同的二叉树,该序列可以生成一个完美二叉树
 | 
				
			||||||
 | 
					        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("\n初始化的二叉树为\n");
 | 
					        Console.WriteLine("\n初始化的二叉树为\n");
 | 
				
			||||||
        PrintUtil.PrintTree(bst.getRoot());
 | 
					        PrintUtil.PrintTree(bst.getRoot());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user