How to write a binary search tree algorithm using C#

王林
Release: 2023-09-19 13:03:28
Original
1213 people have browsed it

How to write a binary search tree algorithm using C#

How to use C# to write a binary search tree algorithm, specific code examples are required

Binary Search Tree (BST) is a commonly used data Structure, which features fast insertion, search, and deletion operations. In C#, we can use object-oriented approach to write binary search tree algorithm.

First, we need to define a class of binary search tree nodes, which contains a value and two pointers to the left and right child nodes. The code is as follows:

public class BSTNode
{
    public int Value { get; set; }
    public BSTNode Left { get; set; }
    public BSTNode Right { get; set; }

    public BSTNode(int value)
    {
        Value = value;
        Left = null;
        Right = null;
    }
}
Copy after login

Next, we can define a binary search tree class, which contains methods for operations such as insertion, search, and deletion. The code is as follows:

public class BinarySearchTree
{
    private BSTNode root;

    public BinarySearchTree()
    {
        root = null;
    }

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

    private BSTNode InsertNode(BSTNode node, int value)
    {
        if (node == null)
        {
            node = new BSTNode(value);
        }
        else if (value < node.Value)
        {
            node.Left = InsertNode(node.Left, value);
        }
        else if (value > node.Value)
        {
            node.Right = InsertNode(node.Right, value);
        }
        return node;
    }

    public bool Search(int value)
    {
        return SearchNode(root, value);
    }

    private bool SearchNode(BSTNode node, int value)
    {
        if (node == null)
        {
            return false;
        }
        else if (value < node.Value)
        {
            return SearchNode(node.Left, value);
        }
        else if (value > node.Value)
        {
            return SearchNode(node.Right, value);
        }
        else
        {
            return true;
        }
    }

    public void Delete(int value)
    {
        root = DeleteNode(root, value);
    }

    private BSTNode DeleteNode(BSTNode node, int value)
    {
        if (node == null)
        {
            return node;
        }
        else if (value < node.Value)
        {
            node.Left = DeleteNode(node.Left, value);
        }
        else if (value > node.Value)
        {
            node.Right = DeleteNode(node.Right, value);
        }
        else
        {
            if (node.Left == null && node.Right == null)
            {
                node = null;
            }
            else if (node.Left == null)
            {
                node = node.Right;
            }
            else if (node.Right == null)
            {
                node = node.Left;
            }
            else
            {
                BSTNode minNode = FindMinNode(node.Right);
                node.Value = minNode.Value;
                node.Right = DeleteNode(node.Right, minNode.Value);
            }
        }
        return node;
    }

    private BSTNode FindMinNode(BSTNode node)
    {
        while (node.Left != null)
        {
            node = node.Left;
        }
        return node;
    }
}
Copy after login

The above is a detailed code example of using C# to write a binary search tree algorithm. By creating the BSTNode class and BinarySearchTree class, we can easily perform operations such as insertion, search, and deletion. The time complexity of these operations is O(h), where h is the height of the binary search tree.

The sample code is as follows:

BinarySearchTree bst = new BinarySearchTree();
bst.Insert(5);
bst.Insert(3);
bst.Insert(8);
bst.Insert(2);
bst.Insert(4);
bst.Insert(7);
bst.Insert(9);

Console.WriteLine(bst.Search(4)); // 输出:True
Console.WriteLine(bst.Search(6)); // 输出:False

bst.Delete(8);
Console.WriteLine(bst.Search(8)); // 输出:False
Copy after login

Through the above code, we can see that the insertion, search and deletion operations of the binary search tree are very simple and efficient, and are suitable for many practical applications. Application scenarios. I hope the code examples in this article can help you understand and use C# to write binary search tree algorithms.

The above is the detailed content of How to write a binary search tree algorithm using C#. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template