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. linux下Memcached安装以及PHP的调用

    一:安装libevent 由于memcached安装时,需要使用libevent类库,所以先安装libevent 1.官网下载:http://libevent.org/ #wget   https:/ ...

  2. Yale CAS + .net Client 实现 SSO 的完整版

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  3. 【转】安装Ubuntu 15.10后要做的事

    Ubuntu 15.10发布了,带来了很多新特性,同样也依然带着很多不习惯的东西,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java ...

  4. 并发与同步 (一) ThreadLocal与Synchronized 用哪一个好

    ThreadLocal是什么? 历史 早在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以 ...

  5. PROC简单的用例--VC连接ORACLE

    操作系统:windows 7 数据库版本号:oracle 10g VS版本号:VS2010 前言:连接ORACLE有许多方法,这里只PROC外壳,说明如何连接oracle,有事吗,希望你告诉我指出,一 ...

  6. Java和.NET下socket转换错误记录

    一个项目,需要JAVA的Socket客户端转换为C#的Socket客户端,使用的是短连接. 首先,java客户端代码如下: Socket socket = new Socket(); socket.c ...

  7. 文件和目录之stat、fstat和lstat函数

    #include <sys/stat.h> int stat( const char *restrict pathname, struct stat *restrict buf ); in ...

  8. 深入理解C++中的mutable关键字

      mutalbe的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词. 在C++中,mutable也是为了突破const的限制而设置的.被mutable修饰的变量,将 ...

  9. 从你的u盘启动:30天自制操作系统第四天u盘启动学习笔记

    暑假学习小日本的那本书:30天自制操作系统 qq交流群:122358078    ,更多学习中的问题.资料,群里分享 developing environment:ubuntu 关于u盘启动自己做的操 ...

  10. xss脚本攻击

    xss脚本攻击不仅仅只是alert(1)就算完了,xss脚本攻击真正的用处是盗取普通用户的cookie,或者盗取管理员的cookie. xss分类(类型): 1. 反射型xss2. 存储型xss3. ...