二叉搜索的各种bugs——重复递增序列
int binary_search(int* A, int value, int p, int r);
int main(int argc, char *argv[]){
int A[] = {, , , , , , , , , };
int index = binary_search(A, , , );
printf("%d\n", index);
return ;
}
int binary_search(int* A, int value, int p, int r){
if(A==NULL || p>r){
return -;
}
int q;
while(p<r){
q = p/ + r/;
if(value==A[q]){
if(A[q] == A[q-]){
r = q-;
}
else{
return q;
}
}
else if(value > A[q]){
p = q+;
}
else if(value < A[q]){
r = q-;
}
}
return -1;
}
1 while(p<r) error case:

查找value 5
q = (p+r)/2 = 4
p = q + 1 = 5
(p < r) != true
while 结束,没有找到5
2 q = p/2 + r/2 error case:

查找value 5
q = p/2 + r/2 = 4
q 超出了p和r的范围,陷入死循环之中
3 q = (p+r)/2 可能会越界
4 A[q] == A[q-1] 可能会越界
正确的方案(忽略 p+r 越界)
int binary_search(int* A, int value, int p, int r){
if(A==NULL || p>r){
return -;
}
int q;
while(p<=r){
q = (p + r)/;
if(value==A[q]){
if(q> && A[q] == A[q-]){
r = q-;
}
else{
return q;
}
}
else if(value > A[q]){
p = q+;
}
else if(value < A[q]){
r = q-;
}
二叉搜索的各种bugs——重复递增序列的更多相关文章
- Java实现 LeetCode 501 二叉搜索树中的众数
501. 二叉搜索树中的众数 给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等于当前结点的值 结点 ...
- 算法dfs——二叉搜索树中最接近的值 II
901. 二叉搜索树中最接近的值 II 中文 English 给定一棵非空二叉搜索树以及一个target值,找到 BST 中最接近给定值的 k 个数. 样例 样例 1: 输入: {1} 0.00000 ...
- 刷题-力扣-230. 二叉搜索树中第K小的元素
230. 二叉搜索树中第K小的元素 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kth-smallest-element-in-a ...
- [LeetCode] Delete Node in a BST 删除二叉搜索树中的节点
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- 【转载】图解:二叉搜索树算法(BST)
原文:图解:二叉搜索树算法(BST) 摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢!“岁月极美,在于它必然的流逝”“春花 秋月 夏日 冬雪”— ...
- 二叉搜索树算法详解与Java实现
二叉查找树可以递归地定义如下,二叉查找树或者是空二叉树,或者是满足下列性质的二叉树: (1)若它的左子树不为空,则其左子树上任意结点的关键字的值都小于根结点关键字的值. (2)若它的右子树不为空,则其 ...
- [Swift]LeetCode450. 删除二叉搜索树中的节点 | Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- [Swift]LeetCode701. 二叉搜索树中的插入操作 | Insert into a Binary Search Tree
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
随机推荐
- 取消jQuery validate验证
有时候当我们在编辑页面点保存后加上了validate错误验证后又想用表单提交的方式返回界面没有清除验证就返回不了 加上这句话就清除验证了 注意:remove()是删除了相关标签 我这需求是 ...
- JNI函数复杂对象传递
主要操作内容,包括如下几个部分: 1.在Native层返回一个字符串 2.从Native层返回一个int型二维数组(int a[ ][ ]) 3.从Native层操作Java层的类: 读取/设置类属性 ...
- 通过存储过程进行分页查询的SQL示例
--创建人:zengfanlong --创建时间:-- :: --说明:根据公司简写代码获取当前待同步的气瓶档案数据(分页获取) ALTER PROCEDURE [UP_GasBottles_GetS ...
- 3、VS2010+ASP.NET MVC4+EF4+JqueryEasyUI+Oracle项目开发之——用户登录
近期因为项目赶着上线,一直没时间接着写博客,今天最终空出了时间.声名:我不是专业美工,所以界面问题,希望大家不要拍砖.登录界面例如以下: 在ASP.NET MVC中,要新增一个功能,我们首先要加入一个 ...
- Android 编程下 java.lang.NoClassDefFoundError: cn.jpush.android.api.JPushInterface 报错
使用了极光推送的 jar 包项目在从 SVN 中检出后,假设不又一次对 jar 包和 Bulid Path 进行配置就会抛出 java.lang.NoClassDefFoundError: cn.jp ...
- thinPHP中多维数组的遍历
$drug=array( 'ACEI'=>array(array('ch_name'=>'卡托普利','en_name'=>'captopril'),array('ch_nam ...
- [Practical Git] Configure global settings with git config
You can set up global "git config" settings that apply to all git projects on your system. ...
- 优麒麟(UbuntuKylin)不是国产Linux操作系统
2014年5月10日,CCTV新闻频道"新闻直播间"栏目播报了"谁来替代Windows XP,工信部希望用户使用国产操作系统"报道.同一时候,央视也报道了眼下包 ...
- Windows二进制文件的Python扩展包
http://www.lfd.uci.edu/~gohlke/pythonlibs/ https://pypi.python.org/simple/
- careercup-中等难度 17.11
17.11 给定rand5(),实现一个方法rand7().也即,给定一个产生0到4(含)随机数的方法,编写一个产生0到6(含)随机数的方法. 解法: 这个函数要正确实现,则返回0到6之间的值,每个值 ...