23.二叉树搜索树(下)

只要是能比较大小的都能进二叉树

上次代码用key命名,下面用key_value

参与比较的只有key没有value

没找到就返回空,车走了要删除,跟value没关系

字典:

cpp 复制代码
int main()
{
	key_value::BSTree<string, string> dict;
	//BSTree<string, string> copy = dict;
	dict.Insert("left", "左边");
	dict.Insert("right", "右边");
	dict.Insert("insert", "插入");
	dict.Insert("string", "字符串");

	string str;
	while (cin >> str)
	{
		auto ret = dict.Find(str);
		if (ret)
		{
			cout << "->" << ret->_value << endl;
		}
		else
		{
			cout << "无此单词,请重新输入" << endl;
		}
	}

	return 0;
}

ctrlZ 给io流一个错误标志就结束输入了

统计次数:

cpp 复制代码
int main()
{
	string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜",
	"苹果", "香蕉", "苹果", "香蕉" };
	key_value::BSTree<string, int> countTree;

	for (const auto& str : arr)
	{
		// 先查找水果在不在搜索树中
		// 1、不在,说明水果第一次出现,则插入<水果, 1>
		// 2、在,则查找到的结点中水果对应的次数++
		//BSTreeNode<string, int>* ret = countTree.Find(str);
		auto ret = countTree.Find(str);
		if (ret == nullptr)
		{
			countTree.Insert(str, 1);
		}
		else
		{
			// 修改value
			ret->_value++;
		}
	}
	countTree.InOrder();


	key_value::BSTree<string, int> copy = countTree;
	copy.InOrder();

	return 0;
}

最下面还有个拷贝构造,没写拷贝构造就是浅拷贝,没崩,写上析构就崩了

这写个二叉树析构就崩了深拷贝:

报错说没有合适默认构造,因为默认构造不能写任何构造

这样强制生成默认构造

相关推荐
罗西的思考2 小时前
【轻量化复刻大模型 / KIMI K3】Multi-Token Prediction(MTP / EAGLE-3)(2)
人工智能·算法·机器学习
罗西的思考2 小时前
【轻量化复刻大模型 / KIMI K3】Multi-Token Prediction(MTP / EAGLE-3)(1)
人工智能·算法·机器学习
武子康3 小时前
H3-World 接入 Game Agent:谁来判断门真的打开了?
人工智能·算法·机器学习
南风84115 小时前
C++ 四种强制类型转换详解
c++
微笑星河q5 小时前
C++STL【vector】常用接口函数使用和迭代器失效问题
开发语言·c++
HaiXCoder8 小时前
C++ 表达式怎么读:从一行代码到类型、值类别和生命周期
c++
武子康8 小时前
H3-World 扩展跳跃与开门:新动作怎样进入训练数据?
大数据·人工智能·算法
卡尔曼的BD SLAMer8 小时前
北斗导航 |GNSS ION论文集汇总1997到2022(链接在文末)
算法·北斗导航
时间的拾荒人8 小时前
Redis Cluster 集群详解:分片算法、搭建、故障处理与扩容实战
数据库·redis·算法·集群
老师我太想进步了202610 小时前
【数据结构-8】栈的实现
c语言·数据结构·算法