二进制搜索树;自组织搜索/旋转C++
Binary Search Tree; Self Organizing Search/Rotation C++
对于我的项目,我将创建一个自组织二进制搜索树。我已经成功地创建了BST,但由于某种原因,我不太清楚如何实现组织部分。
更具体地说,当我搜索一个值时,我要增加它的搜索计数。一旦搜索计数等于"阈值保持值"(通过构造函数设置),我就会将搜索到的节点向上旋转一个。
我相信我能弄清楚如何执行旋转,但我的问题在于整数变量searchCount和threshVal。出于某种原因,我不知道如何使searchCount只随搜索值递增,并在搜索新值时重置
例如:我的BST中有"1 2 3 4 5"。我搜索值"3",找到它,将搜索计数增加到1。然后,我执行另一次搜索,这次搜索值为"5"。然后,searchCount变量再次增加到2,因为我搜索了不同的值,所以它应该是1。
这是我的搜索功能。这是一个很大的.cpp文件,所以我只包含一个函数。
template <typename T>
bool BST<T>::contains(const T& v, BSTNode *&t)
{
if (t == nullptr)
return false;
else if(v < t->data)
return contains(v, t->left);
else if(t->data < v)
return contains(v, t->right);
else{
if(t->right == nullptr)
return true;
/*
Problem lies in the following segment, I just added the little
rotation portion to try and get something to work for testing
purposes. The problem still lies with threshVal and searchCount
*/
if (searchCount == threshVal){
BSTNode *temp = t->right;
t->right = temp->left;
temp->left = t;
t = temp;
if(t == root)
searchCount = 0;
}
return true;
}
}
如果我需要给你们更多的信息,或者添加.cpp文件的其余部分,请告诉我。非常感谢。
我无法添加注释,但您是否尝试为每个节点提供自己的int计数?
示例:
struct treeNode
{
treeNode* left;
treeNode* right;
T data;
treeNode() {left = NULL; right = NULL;};
treeNode(const T&v, treeNode* l, treeNode* r){data = v; left = l; right = r;};
int count = 0;
};
然后在进行搜索时增加该节点的单个计数?
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- 与互斥锁相比,旋转锁可以保证上下文切换
- 在C++中搜索嵌套多映射值
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 二进制搜索树叶数问题
- 为什么二进制搜索在我的测试中不起作用
- 绘制旋转的三角形
- 正在尝试重载二进制搜索树分配运算符
- 旋转模型矩阵时的形状失真
- c++binary_search函数排序数组(流行名称搜索)出现问题
- 四边形的 2D 旋转
- 向量上的线性搜索
- 如何在动态数组上使用搜索函数
- 垂直方向的 Gtk3+ 旋转按钮 (c/c++)
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- 发布旋转矩阵(openGL/glm)
- cmake:添加要搜索头文件的目录
- 二进制搜索树;自组织搜索/旋转C++
- 二进制搜索树左旋转
- 创建二叉搜索树类,一个节点在旋转后消失