c# 二叉搜索树 (Binary Search Tree,BST)

本文介绍了在C#中如何实现和应用二叉搜索树,包括节点类和树类的定义,插入操作的实现,以及二叉搜索树的性能优化,重点提及了平衡二叉搜索树的重要性。
摘要由CSDN通过智能技术生成

        在 C# 中,二叉搜索树(Binary Search Tree,BST)是一种常见的数据结构,其特点是每个节点最多有两个子节点,且满足以下性质:

  1. 对于任意节点 n,其左子树中所有节点的值小于节点 n 的值。
  2. 对于任意节点 n,其右子树中所有节点的值大于节点 n 的值。
  3. 左子树和右子树本身也是二叉搜索树。

        在 C# 中实现二叉搜索树通常需要定义一个节点类(Node)和一个树类(BinarySearchTree)。节点类用来表示二叉搜索树中的节点,包含节点的值、左子节点和右子节点。树类则包含对树进行操作的方法,比如插入节点、删除节点和查找节点等。

        二叉搜索树的优点是可以快速进行查找、插入和删除操作,时间复杂度为 O(log n),其中 n 表示树中节点的数量。然而,如果树的形状不平衡,可能会导致性能下降,因此在实际应用中可能需要考虑平衡二叉搜索树(如 AVL 树、红黑树等)来确保性能。

        二叉搜索树在实际应用中非常有用,比如在数据库索引、编译器符号表等领域经常被使用。通过了解和掌握二叉搜索树的基本原理和实现方式,可以更好地理解和应用这种数据结构。

示例一:

// C# function to search a given key in a given BST
 
using System;
 
public class Node {
    public int key;
    public Node left, right;
}
 
public class BinaryTree {
    // A utility function to create a new BST node
    public Node NewNode(int item)
    {
        Node temp = new Node();
        temp.key = item;
        temp.left = temp.right = null;
        return temp;
    }
 
    // A utility function to insert
    // a new node with given key in BST
    public Node Insert(Node node, int key)
    {
        // If the tree is empty, return a new node
        if (node == null)
            return NewNode(key);
 
        // Otherwise, recur down the tree
        if (key < node.key)
            node.left = Insert(node.left, key);
        else if (key > node.key)
            node.right = Insert(node.right, key);
 
        // Return the (unchanged) node pointer
        return node;
    }
 
    // Utility function to search a key in a BST
    public Node Search(Node root, int key)
    {
        // Base Cases: root is null or key is present at root
        if (root == null || root.key == key)
            return root;
 
        // Key is greater than root's key
        if (root.key < key)
            return Search(root.right, key);
 
        // Key is smaller than root's key
        return Search(root.left, key);
    }
 
    // Driver Code
    public static void Main()
    {
        Node root = null;
        BinaryTree bt = new BinaryTree();
        root = bt.Insert(root, 50);
        bt.Insert(root, 30);
        bt.Insert(root, 20);
        bt.Insert(root, 40);
        bt.Insert(root, 70);
        bt.Insert(root, 60);
        bt.Insert(root, 80);
 
        // Key to be found
        int key = 6;
 
        // Searching in a BST
        if (bt.Search(root, key) == null)
            Console.WriteLine(key + " not found");
        else
            Console.WriteLine(key + " found");
 
        key = 60;
 
        // Searching in a BST
        if (bt.Search(root, key) == null)
            Console.WriteLine(key + " not found");
        else
            Console.WriteLine(key + " found");
    }
}

输出:

6 未找到
60 已找到

示例二:

using System;

public class Node
{
    public int value;
    public Node left, right;

    public Node(int item)
    {
        value = item;
        left = right = null;
    }
}

public class BinarySearchTree
{
    public Node root;

    public BinarySearchTree()
    {
        root = null;
    }

    public void Insert(int value)
    {
        root = InsertRec(root, value);
    }

    private Node InsertRec(Node root, int value)
    {
        if (root == null)
        {
            root = new Node(value);
            return root;
        }

        if (value < root.value)
        {
            root.left = InsertRec(root.left, value);
        }
        else if (value > root.value)
        {
            root.right = InsertRec(root.right, value);
        }

        return root;
    }

    public void InOrderTraversal(Node root)
    {
        if (root != null)
        {
            InOrderTraversal(root.left);
            Console.Write(root.value + " ");
            InOrderTraversal(root.right);
        }
    }
}

class Program
{
    static void Main()
    {
        BinarySearchTree tree = new BinarySearchTree();
        tree.Insert(5);
        tree.Insert(3);
        tree.Insert(7);
        tree.Insert(2);
        tree.Insert(4);

        Console.WriteLine("Inorder traversal of the binary search tree:");
        tree.InOrderTraversal(tree.root);
    }
}
        在这个示例中,定义了一个 `Node` 类来表示二叉搜索树的节点,以及一个 `BinarySearchTree` 类来表示二叉搜索树本身。`Insert` 方法用于插入节点,`InsertRec` 是一个递归方法用于递归地插入节点。`InOrderTraversal` 方法用于进行中序遍历输出节点值。

        在 `Main` 方法中创建了一个二叉搜索树对象 `tree`,并插入了一些节点,最后进行中序遍历输出节点的值。这个代码仅提供了一个基本的示例,实际应用中可能需要添加更多功能和错误处理机制。

  • 23
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值