#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; }二叉搜索树
张小明
前端开发工程师
中文提示超强解析!Z-Image-ComfyUI实战分享
中文提示超强解析!Z-Image-ComfyUI实战分享 在AI图像生成技术迅猛发展的今天,如何高效、精准地将自然语言转化为高质量视觉内容,已成为设计师、产品经理和开发者共同关注的核心问题。尤其是在中文语境下,传统文生图模型常因文化语…
DeepSeek-R1部署详解:多实例负载均衡
DeepSeek-R1部署详解:多实例负载均衡 1. 引言 1.1 本地化大模型推理的现实需求 随着大语言模型在逻辑推理、代码生成和数学推导等任务上的能力不断提升,越来越多企业与开发者希望将这类能力集成到本地系统中。然而,主流大模型通常依赖高性…
Qwen3-Embedding-4B技术解析:用户自定义指令功能
Qwen3-Embedding-4B技术解析:用户自定义指令功能 1. 技术背景与核心价值 随着大模型在信息检索、语义理解、跨语言处理等场景的广泛应用,高质量文本嵌入(Text Embedding)能力成为构建智能系统的关键基础设施。传统的通用语言模型…
亲自动手试了科哥镜像,AI抠图原来可以这么快
亲自动手试了科哥镜像,AI抠图原来可以这么快 1. 引言:为什么需要高效的图像抠图工具? 在数字内容创作、电商运营和视觉设计等领域,高质量的图像抠图是一项高频且关键的任务。传统依赖Photoshop等专业软件的手动操作不仅耗时&…
5分钟上手图像修复!fft npainting lama一键移除水印和物体
5分钟上手图像修复!fft npainting lama一键移除水印和物体 1. 快速入门:图像修复的现代解决方案 在数字图像处理领域,图像修复(Image Inpainting)是一项关键任务,旨在通过算法自动填充图像中缺失或被遮挡…
快速构建儿童内容库:批量生成萌宠图片的Qwen实战教程
快速构建儿童内容库:批量生成萌宠图片的Qwen实战教程 在儿童教育、绘本创作、动画设计等领域,高质量、风格统一的可爱动物图像资源需求巨大。然而,传统美术绘制成本高、周期长,难以满足快速迭代的内容生产需求。随着大模型技术的…