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——重复递增序列的更多相关文章

  1. Java实现 LeetCode 501 二叉搜索树中的众数

    501. 二叉搜索树中的众数 给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等于当前结点的值 结点 ...

  2. 算法dfs——二叉搜索树中最接近的值 II

    901. 二叉搜索树中最接近的值 II 中文 English 给定一棵非空二叉搜索树以及一个target值,找到 BST 中最接近给定值的 k 个数. 样例 样例 1: 输入: {1} 0.00000 ...

  3. 刷题-力扣-230. 二叉搜索树中第K小的元素

    230. 二叉搜索树中第K小的元素 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kth-smallest-element-in-a ...

  4. [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 ...

  5. 【转载】图解:二叉搜索树算法(BST)

    原文:图解:二叉搜索树算法(BST) 摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢!“岁月极美,在于它必然的流逝”“春花 秋月 夏日 冬雪”— ...

  6. 二叉搜索树算法详解与Java实现

    二叉查找树可以递归地定义如下,二叉查找树或者是空二叉树,或者是满足下列性质的二叉树: (1)若它的左子树不为空,则其左子树上任意结点的关键字的值都小于根结点关键字的值. (2)若它的右子树不为空,则其 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Asp.Net事务和异常处理:

    Asp.Net事务和异常处理: 一.什么是事务处理? 事务处理是一组组和成逻辑工作单元的数据库操作,虽然系统中可能会出错,但事务将控制和维护每个数据库的一致性和完整性. 如果在事务过程中没有遇到错误, ...

  2. 记一次js中和php中的字符串长度计算截取的终极问题和完美解决方案

    1.js是用unicode算长度的,比如单字节的算1,中文也算1,但是正常我们想让两个单字节算1,如何计算这个长度 第一种解决方案,用正则,如下 /[\u0x00-\u0xff]/,天真的想着,这样就 ...

  3. multiselect获取选中的多个下拉项的值(逗号分割的字符串)

    /*传入下拉select标签*/ function get_selected(mslt_employees) { var emplo =mslt_employees.multiselect(" ...

  4. SQLServer2005日志传送常见的几个问题

    1.STANDBY 只读方式还原数据库:[备份数据库服务器]将完全备份文件复制到备份数据库服务器上,并以STANDBY的方式进行恢复 . SQL语句: RESTORE DATABASE [CNBlog ...

  5. Reviewing the Blog Module

    Reviewing the Blog Module Throughout the tutorial, we have created a fully functional CRUD module us ...

  6. Asp.Net 之 缓存机制

    asp.net缓存有三种:页面缓存,数据源缓存,数据缓存. 一.页面缓存 原理:页面缓存是最常用的缓存方式,原理是用户第一次访问的时候asp.net服务器把动态生成的页面存到内存里,之后一段时间再有用 ...

  7. Android之HTTP网络通信--GET传递

    说明 在做一个项目的时候难免会与服务器打交道,这里我就做一个小的Demo来简单的说明一下HTTP的使用,我这里使用的是图灵的接口,你也可以登陆www.tuling123.com进行申请.我使用的是上面 ...

  8. ios代理设计模式

    代理设计模式的作用:     1.A对象监听B对象的一些行为,A成为B的代理     2.B对象想告诉A对象一些事情,A成为B的代理   代理设计模式的总结:     如果你想监听别人的一些行为,那么 ...

  9. 关于javascript的slice方法

    slice方法在javascript中既可以在Array对象的原型下也可以是在String对象的原型下;其中w3c上面说的slice方法的第一个参数是必须的;这里的说法有误; slice的参数可以是0 ...

  10. Session对象的集合

    Session StaticObjects 集合 StaticObjects 集合包含 Session 对象范围中用 <OBJECT> 标记创建的所有对象.该集合可用于确定对象特定属性的值 ...