#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string.h> using namespace std; template<class K, class V> struct BSTreeNode { K _key; V _value; BSTreeNode<K, V>* _left; BSTreeNode<K, V>* _right; BSTreeNode(const K& key, const V& value) :_key(key) , _value(value) , _left(nullptr) , _right(nullptr) {} }; template<class K, class V> class BSTree { typedef BSTreeNode<K, V> Node; public: bool Insert(const K& key, const V& value) { if (_root == nullptr) { _root =new Node(key, value); return true; } Node* parent = nullptr; Node* cur = _root; while (cur) { if (cur->_key < key) { parent = cur; cur = cur->_right; } else if (cur->_key > key) { parent = cur; cur = cur->_left; } else { return false; } } cur = new Node(key, value); if (parent->_key < key) { parent->_right = cur; } else { parent->_left = cur; } return true; } Node* Find(const K& key) { Node* cur = _root; while (cur) { if (cur->_key < key) { cur = cur->_right; } else if (cur->_key > key) { cur = cur->_left; } else { return cur; } } return nullptr; } bool Erase(const K& key) { Node* parent = nullptr; Node* cur = _root; while (cur) { if (cur->_key < key) { parent = cur; cur = cur->_right; } else if (cur->_key > key) { parent = cur; cur = cur->_left; } else { // 删除 // 左为空 if (cur->_left == nullptr) { if (cur == _root) { _root = cur->_right; } else { if (parent->_left == cur) { parent->_left = cur->_right; } else { parent->_right = cur->_right; } } delete cur; } else if (cur->_right == nullptr) { if (cur == _root) { _root = cur->_left; } else { // 右为空 if (parent->_left == cur) { parent->_left = cur->_left; } else { parent->_right = cur->_left; } } delete cur; } else { // 左右都不为空 // 右子树最左节点 Node* replaceParent = cur; Node* replace = cur->_right; while (replace->_left) { replaceParent = replace; replace = replace->_left; } cur->_key = replace->_key; if (replaceParent->_left == replace) replaceParent->_left = replace->_right; else replaceParent->_right = replace->_right; delete replace; } return true; } } return false; } void InOrder() { _InOrder(_root); cout << endl; } private: void _InOrder(Node* root) { if (root == nullptr) { return; } _InOrder(root->_left); cout << root->_key << ":" << root->_value << endl; _InOrder(root->_right); } private: Node* _root = nullptr; }; void TestBSTree() { /*BSTree<string, string> dict; dict.Insert("insert", "插入"); dict.Insert("erase", "删除"); dict.Insert("left", "左边"); dict.Insert("string", "字符串"); string str; while (cin >> str) { auto ret = dict.Find(str); if (ret) { cout << str << ":" << ret->_value << endl; } else { cout << "单词拼写错误" << endl; } }*/ string strs[] = { "苹果", "西瓜", "苹果", "樱桃", "苹果", "樱桃", "苹果", "樱桃", "苹果" }; // 统计水果出现的次 BSTree<string, int> countTree; for (auto str : strs) { auto ret = countTree.Find(str); if (ret == NULL) { countTree.Insert(str, 1); } else { ret->_value++; } } countTree.InOrder(); } int main() { TestBSTree(); return 0; }二叉搜索树
张小明
前端开发工程师
BSHM抠图经济方案:云端GPU用多少付多少,不花冤枉钱
BSHM抠图经济方案:云端GPU用多少付多少,不花冤枉钱 你是不是也遇到过这样的烦恼?想用AI技术帮孩子制作一份精美的成长相册,记录下他/她每一个可爱的瞬间。可家里唯一能用的电脑是老公办公用的轻薄本,没有独立显卡&…
PaddleOCR-VL性能分析:元素级识别准确率评测
PaddleOCR-VL性能分析:元素级识别准确率评测 1. 引言 随着数字化转型的加速,文档解析技术在金融、教育、政务等领域的应用日益广泛。传统OCR系统通常依赖多阶段流水线架构,难以高效处理复杂版式和多样化语言内容。百度开源的PaddleOCR-VL-W…
Z-Image-Turbo依赖管理:确保PyTorch与ModelScope版本兼容
Z-Image-Turbo依赖管理:确保PyTorch与ModelScope版本兼容 1. 背景与环境概述 随着文生图大模型在创意设计、内容生成等领域的广泛应用,高效、稳定的本地部署环境成为开发者和研究人员的核心需求。Z-Image-Turbo作为阿里达摩院基于ModelScope平台推出的…
ComfyUI GPU选型指南:最适合ComfyUI的显卡推荐
ComfyUI GPU选型指南:最适合ComfyUI的显卡推荐 1. 引言:为什么ComfyUI需要合适的GPU支持 随着AI生成内容(AIGC)技术的快速发展,ComfyUI作为一款基于节点式工作流的图形化界面工具,正在被越来越多开发者和…
亲测Sambert语音合成:中文多情感效果超预期
亲测Sambert语音合成:中文多情感效果超预期 1. 引言:当语音合成不再“冷冰冰” 在传统文本转语音(Text-to-Speech, TTS)系统中,机器朗读往往语调单一、缺乏情绪起伏,给人以机械感和距离感。随着人机交互体…
动手试了GLM-TTS,3秒音频克隆出我的声音太神奇
动手试了GLM-TTS,3秒音频克隆出我的声音太神奇 1. 引言:零样本语音克隆的现实体验 在语音合成技术快速演进的今天,GLM-TTS 正以“3秒克隆人声”的能力引发广泛关注。作为智谱开源的一款AI文本转语音模型,它不仅支持高保真音色复…