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. 一个通用数据库访问类(C#,SqlClient)

    本文转自:http://www.7139.com/jsxy/cxsj/c/200607/114291.html使用ADO.NET时,每次数据库操作都要设置connection属性.建立connecti ...

  2. linux教程:[4]配置Tomcat开机启动

    http://jingyan.baidu.com/article/6525d4b1382f0aac7d2e9421.html 方法/步骤 1 请自行下载安装配置tomcat的服务器环境 本经验仅仅介绍 ...

  3. SQL Server重建索引计划

    每周日2点进行”一致性检查“ 每周六1点进行”重建索引“,重建索引会自动完成更新统计信息操作

  4. Unity3d 网络编程(一)(Unity3d内建网络Network介绍)

    首先个人说说题外话,Unity3d使用的网络库核心是用C++实现的一款商业网络游戏引擎库. RakNet.所以对于移动设备来说,用Unity3d来写server是全然能够的,而且内建网络库的各项功能封 ...

  5. careercup-中等难度 17.9

    17.9 设计一个方法,找出任意指定单词在一本书中的出现频率. 解法: 1 单次查询 遍历这本书的每个单词,计算给定单词出现的次数.时间复杂度O(n),我们无法继续优化它,因为书中的每个单次都需要访问 ...

  6. [Effective C++ --019]设计class犹如设计type

    前言 我们在编写程序的时候,无论哪一种语言,总是会告诉你这种语言应该有的类型.我们将它们直接拿来使用,可是从来没有考虑过这些类到底是怎么设计出来的! 所幸的是:在OOP中,每当我们定义一个新的clas ...

  7. android中的界面编程

    主要是用View以及ViewGroup,同时ViewGroup又是View的子类,充当容器. 主要有两种方法控制view的行为: 1.在XML布局文件中通过XML属性进行控制 2.在Java代码中通过 ...

  8. sql根据表名获取字段及对应说明

    SELECT TableName = OBJECT_NAME(c.object_id), ColumnsName = c.name, Description = ex.value, ColumnTyp ...

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

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

  10. 拖拽js

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...