15. 【C++】详解搜索二叉树 | KV模型




#include <iostream>
#include <string>
#include <vector>
 
// 定义节点结构体
struct Node {
    std::string key;
    std::string value;
    Node* left;
    Node* right;
 
    Node(const std::string& k, const std::string& v) : key(k), value(v), left(nullptr), right(nullptr) {}
};
 
// 插入节点
void insert(Node*& node, const std::string& key, const std::string& value) {
    if (node == nullptr) {
        node = new Node(key, value);
    } else if (key < node->key) {
        insert(node->left, key, value);
    } else if (key > node->key) {
        insert(node->right, key, value);
    } else {
        node->value = value; // 更新值
    }
}
 
// 查找节点
bool search(Node* node, const std::string& key, std::string& value) {
    if (node == nullptr) {
        return false;
    } else if (key < node->key) {
        return search(node->left, key, value);
    } else if (key > node->key) {
        return search(node->right, key, value);
    } else {
        value = node->value;
        return true;
    }
}
 
// 中序遍历
void inorder(Node* node, std::vector<std::string>& output) {
    if (node == nullptr) {
        return;
    }
    inorder(node->left, output);
    output.push_back(node->key + " " + node->value);
    inorder(node->right, output);
}
 
int main() {
    Node* root = nullptr;
 
    insert(root, "apple", "fruit");
    insert(root, "banana", "fruit");
    insert(root, "pear", "fruit");
    insert(root, "carrot", "vegetable");
    insert(root, "lettuce", "vegetable");
 
    std::string search_value;
    if (search(root, "banana", search_value)) {
        std::cout << "Found: " << search_value << std::endl;
    } else {
        std::cout << "Not found" << std::endl;
    }
 
    std::vector<std::string> inorder_traversal;
    inorder(root, inorder_traversal);
    for (const auto& kv : inorder_traversal) {
        std::cout << kv << std::endl;
    }
 
    // 清理内存
    // ...
 
    return 0;
}

这段代码实现了二叉搜索树的插入、查找以及中序遍历功能。它使用了KV模型,其中每个节点包含一个键值对,并且在插入时如果键已存在则更新值。代码中包含了递归的搜索和中序遍历方法,以及对应的递归结束条件。这个例子简单易懂,适合作为二叉搜索树和递归算法的入门教程。

none
最后修改于:2024年08月26日 13:12

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日