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

二叉搜索树(Binary Search Tree,BST)是一种常见的二叉树数据结构,它具有以下特性:

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

这些特性使得二叉搜索树具有高效的查找、插入和删除操作。通过保持节点的值有序排列,可以在 O(log n) 的时间复杂度内进行这些操作。

在 C++ 中实现二叉搜索树通常需要定义一个节点类和一个树类。节点类用来表示树中的节点,包含节点的值、左子节点指针和右子节点指针。而树类则用来实现树的各种操作,比如插入节点、删除节点和查找节点等。

利用二叉搜索树的特性,可以进行快速的搜索和排序操作,因此在实际应用中经常被使用。在编写二叉搜索树时,需要注意平衡性问题,即确保树的高度平衡,以维持二叉搜索树的高效性能。

示例一:

// C++ function to search a given key in a given BST
 
#include <iostream>
 
using namespace std;
 
struct node {
    int key;
    struct node *left, *right;
};
 
// A utility function to create a new BST node
struct node* newNode(int item)
{
    struct node* temp
        = new struct node;
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}
 
// A utility function to insert
// a new node with given key in BST
struct node* insert(struct 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
struct node* search(struct 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
int main()
{
    struct node* root = NULL;
    root = insert(root, 50);
    insert(root, 30);
    insert(root, 20);
    insert(root, 40);
    insert(root, 70);
    insert(root, 60);
    insert(root, 80);
 
    // Key to be found
    int key = 6;
 
    // Searching in a BST
    if (search(root, key) == NULL)
        cout << key << " not found" << endl;
    else
        cout << key << " found" << endl;
 
    key = 60;
 
    // Searching in a BST
    if (search(root, key) == NULL)
        cout << key << " not found" << endl;
    else
        cout << key << " found" << endl;
    return 0;
}

输出:

6 未找到

60 已找到

示例二:

        1、二叉搜索树节点类:节点类应包含节点的值、指向左子树的指针、指向右子树的指针。这个类可以定义如下:
class TreeNode {
public:
    int val;
    TreeNode* left;
    TreeNode* right;

    TreeNode(int value) : val(value), left(nullptr), right(nullptr) {}
};


        2、二叉搜索树类:二叉搜索树类应该包含插入节点、删除节点、查找节点等操作。以下是一个简单示例:
class BinarySearchTree {
private:
    TreeNode* root;

    TreeNode* insertNode(TreeNode* node, int value) {
        if (node == nullptr) {
            return new TreeNode(value);
        }

        if (value < node->val) {
            node->left = insertNode(node->left, value);
        } else {
            node->right = insertNode(node->right, value);
        }

        return node;
    }

public:
    BinarySearchTree() : root(nullptr) {}

    void insert(int value) {
        root = insertNode(root, value);
    }

    TreeNode* search(int value) {
        TreeNode* current = root;

        while (current != nullptr && current->val != value) {
            if (value < current->val) {
                current = current->left;
            } else {
                current = current->right;
            }
        }

        return current;
    }
};
        以上只是一个简单的示例,实际上,实现一个完整的二叉搜索树类还需要包括删除节点、中序遍历等更多操作。二叉搜索树作为一种常见的数据结构,可以帮助进行排序、查找和插入等操作。

  • 18
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值