二叉搜索的各种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 ...
随机推荐
- 对PostgreSQL cmin和cmax的理解
看例子: 开两个终端来对比: 在终端A: [pgsql@localhost bin]$ ./psql psql () Type "help" for help. pgsql=# b ...
- HDU 5289 Assignment(2015 多校第一场二分 + RMQ)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- paip.提升性能---- 网站并发数的总结.txt
paip.提升性能---- 网站并发数的总结.txt 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.n ...
- iOS音频篇:AVPlayer的缓存实现
授权转载,作者:明仔Su(简书) 在上一篇文章<使用AVPlayer播放网络音乐>介绍了AVPlayer的基本使用,下面介绍如何通过AVAssetResourceLoader实现AVPla ...
- Linux HugePages及MySQL 大页配置
http://blog.csdn.net/dba_waterbin/article/details/9669929 ㈠ HugePages简介 HugePages是 ...
- RHEL6中ulimit的nproc限制
http://blog.csdn.net/kumu_linux/article/details/8301760 当前shell下更改用户可打开进程数 修改limits.conf配置文件生效 [root ...
- js中的preventDefault和stopPropagation
首先讲解一下js中preventDefault和stopPropagation两个方法的区别: preventDefault方法的起什么作用呢?我们知道比如<a href=" ...
- 如何去除掉inline-block元素之间的默认间距
前几天写一个页面 div{width:900px;} div li{ display:inline-block; width:300px;} <ul> <li></li& ...
- ArcGIS中的影像色彩校正(转)
ArcGIS中的影像色彩校正 在遥感影像处理工作中,影像镶嵌是最常用影像处理方式之一,关于影像镶嵌的相关功能,在ArcGIS中有多种实现途径,如使用Mosaic工具.创建镶嵌数据集来进行虚拟镶嵌等等. ...
- c++下new与delete基础用法
delete 释放new分配的单个对象指针指向的内存 delete[] 释放new分配的对象数组指针指向的内存那么,按照教科书的理解,我们看下下面的代码: ]; delete a; //方式1 del ...