C BST中的范围搜索
我需要在二元搜索树中执行范围搜索功能,该功能将给出给定范围内的项数。我不知道如何在找到这些项数时增加计数值。因为,我必须使用递归函数&如果我在递归过程中将count变量初始化为0,它将始终从0开始计数值,而不是在count中更新的计数值C BST中的范围搜索,c,binary-search-tree,C,Binary Search Tree,我需要在二元搜索树中执行范围搜索功能,该功能将给出给定范围内的项数。我不知道如何在找到这些项数时增加计数值。因为,我必须使用递归函数&如果我在递归过程中将count变量初始化为0,它将始终从0开始计数值,而不是在count中更新的计数值 int rangeSearch(struct treeNode * node, int leftBound, int rightBound) { int count=0; if( node->item &
int rangeSearch(struct treeNode * node, int leftBound, int rightBound)
{
int count=0;
if( node->item >= leftBound & node->item <= rightBound)
{
printf("%d ",node->item);
count++;
}
if( node->left!=0 & node->item > leftBound) rangeSearch( node -> left, leftBound , rightBound );
else if( node->right!=0 & node->item < rightBound )rangeSearch( node -> right, leftBound , rightBound );
return count;
}
int-rangeSearch(结构树节点*node,int-leftBound,int-righbound)
{
整数计数=0;
如果(节点->项目>=leftBound&节点->项目);
计数++;
}
如果(节点->左!=0&node->item>leftBound)范围搜索(节点->左、左、右);
否则如果(节点->右!=0&node->item右、左、右);
返回计数;
}
据我所知,由于count
在rangeSearch
中是本地的,因此必须按以下方式更改实现以实现所需的评估
int rangeSearch(struct treeNode * node, int leftBound, int rightBound)
{
int count=0;
if( node->item >= leftBound & node->item <= rightBound)
{
printf("%d ",node->item);
count++;
}
if( node->left!=0 & node->item > leftBound)
count += rangeSearch( node->left, leftBound, rightBound );
if( node->right!=0 & node->item < rightBound )
count += rangeSearch( node->right, leftBound, rightBound );
return count;
}
int-rangeSearch(结构树节点*node,int-leftBound,int-righbound)
{
整数计数=0;
如果(节点->项目>=leftBound&节点->项目);
计数++;
}
如果(节点->左!=0&节点->项目>左边界)
计数+=范围搜索(节点->左、左、右);
如果(节点->右侧!=0&node->item右、左、右);
返回计数;
}
这是我的第一个答案,所以我很抱歉我的英语不好
在每一个这样的递归问题中,思考问题的最简单方法是:
-解决通常微不足道的“基本情况”。(对于大多数数据结构,这通常是空结构或一个元素的结构。)
-根据构成结构的子结构解决一般情况(例如,在考虑树时,这是在考虑左子树和右子树的情况下完成的),假设您可以依赖子结构的解决方案
我肯定我没有解释清楚,所以让我举一个简单的例子:
我们要计算BST中的元素总数。
解决方法如下:
int countElement(struct treeNode* node)
{
if(node == null)
{
//we are in the base case: the tree is empty, so we can return zero.
return 0;
}
else
{
/*the tree is not empty:
We return 1 (the element that we are considering)
+ the elements of the left subtree
+ the elements of the right subtree.*/
return 1 + countElement(node->left) + countElement(node->right);
}
}
如果您清楚这一点,我们可以继续您的请求:
int rangeSearch(struct treeNode * node, int leftBound, int rightBound)
{
if(node == 0)
{
//base case: the tree is empty, we can return 0
return 0;
}
else
{
/*our tree is not empty.
Remember that we can assume that the procedure called on
the left and right child is correct.*/
int countLeft = rangeSearch(node->left, leftBound, rightBound);
int countRight = rangeSearch(node->right, leftBound, rightBound);
/*So what we have to return?
if the current node->item is between leftbound and rightbound,
we return 1 (our node->item is valid) + rangeSearch called
on the left and child subtree with the same identical range.*/
if(node->item > leftBound && node->item < rightBound)
{
/*the element is in the range: we MUST count it
in the final result*/
return 1 + countLeft + countRight;
}
else
{
/*the element is not in the range: we must NOT count it
in the final result*/
return 0 + countLeft + countRight;
}
}
}
int-rangeSearch(结构树节点*node,int-leftBound,int-righbound)
{
如果(节点==0)
{
//基本情况:树为空,我们可以返回0
返回0;
}
其他的
{
/*我们的树不是空的。
请记住,我们可以假设调用了
左边和右边的孩子是正确的*/
int countLeft=rangeSearch(节点->左、左、右);
int countRight=rangeSearch(节点->右、左、右);
/*那么,我们必须回报什么呢?
如果当前节点->项介于左边界和右边界之间,
我们返回1(我们的节点->项目有效)+rangeSearch调用
在左子树和子子树上,具有相同的范围*/
如果(节点->项目>左边界和节点->项目<右边界)
{
/*元素在范围内:我们必须计算它
最终结果*/
返回1+countLeft+countRight;
}
其他的
{
/*元素不在范围内:我们不能计算它
最终结果*/
返回0+countLeft+countRight;
}
}
}
请记住,关键的部分是,如果你定义和解决了基本情况,那么当你考虑更大的结构时,您可以假设在子结构上调用的递归过程执行正确的操作并返回正确的值。您需要使用递归调用的返回值…您可能需要在
else中删除else,如果解释得很好!!:)谢谢你,伙计!!另一个问题:U-PLZ能用C++解释模板中的函数声明格式吗?