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. SQLite使用教程5 分离数据库

    http://www.runoob.com/sqlite/sqlite-detach-database.html SQLite 分离数据库 SQLite的 DETACH DTABASE 语句是用来把命 ...

  2. JQuery上传插件Uploadify API详解

    一.相关key值介绍uploader:uploadify.swf文件的相对路径,该swf文件是一个带有文字BROWSE的按钮,点击后淡出打开文件对话框,默认值:uploadify.swf. scrip ...

  3. java.lang.UnsupportedClassVersionError: Bad version number in .class file 解决办法

    java.lang.UnsupportedClassVersionError: Bad version number in .class file 造成这种错误的原因是支撑Tomcat运行的JDK版本 ...

  4. 利用存储过程将表中的数据生成Insert语句

    1.创建存储过程 CREATE PROC [dbo].[sp_get_InsertSql] @dbName VARCHAR ( )= '' , -- 数据库名称 @tabList VARCHAR ( ...

  5. android小笔记

    1.启动其他应用程序 Intent launchIntent = getPackageManager().getLaunchIntentForPackage(currentAppInfo.getPac ...

  6. ASC(1)G(上升时间最长的序列)

    G - Beautiful People Time Limit: 10000/5000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...

  7. careercup-中等难度 17.12

    17.12 设计一个算法,找出数组中两数之和为指定值的所有整数对. 解答 时间复杂度O(n)的解法 我们可以用一个哈希表或数组或bitmap(后两者要求数组中的整数非负)来保存sum-x的值, 这样我 ...

  8. POJ3056:The Bavarian Beer Party(区间DP)

    Description The professors of the Bayerische Mathematiker Verein have their annual party in the loca ...

  9. FBReaderJ 编译Jni

    最近要做一个电子书项目用到FBReaderJ 第一步,也是最难的一步,要编译他的Jni 文件,对于android开发还是小白的我,只能说难!好难!非常难!,于是乎百度了一下,找到几篇有价值性的文章 第 ...

  10. Android(java)学习笔记124:Android权限大全

    访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permissio ...