二叉搜索的各种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 ...
随机推荐
- cocos2d-x中,简单html富文本显示
作者:HU 转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4037414.html 虽然自从cocos2d-x更新到3.0后,使用freetype, ...
- sqlninja 说明 (转)
首先来介绍一下sqlninja的优点. 一个专门针对Microsoft SQL Server的sql注入工具 可找到远程SQL服务器的标志和特征(版本.用户执行的查询.用户特权.xp_cmdshell ...
- Python、C和Java对比
先上一个Python小测试: #!/usr/bin/env python #coding=utf-8 ''' 等腰三角形 ''' for i in range(1,6): print ' '*(6-i ...
- SVN 中trunk、branches、tags都什么意思?
1.trunk是主分支,是日常开发进行的地方. 2.branches是分支.一些阶段性的release版本,这些版本是可以继续进行开发和维护的,则放在branches目录中.又比如为不同用户客制化的版 ...
- ThinkPHP3.1快速入门(13)自动完成
自动完成是ThinkPHP提供用来完成数据自动处理和过滤的方法,使用create方法创建数据对象的时候会自动完成数据处理.因此,在ThinkPHP使用create方法来创建数据对象是更加安全的方式,而 ...
- C++11 std::bind std::function 高级使用方法
从最基础的了解,std::bind和std::function /* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ # ...
- RapidXml用法
一.写xml 文件 #include <iostream> #include "rapidxml/rapidxml.hpp" #include "rapidx ...
- iOS开发——基本常识篇&各种控件默认高度
各种控件默认高度 1.状态栏 状态栏一般高度为20像素,在打手机或者显示消息时会放大到40像素高,注意,两倍高度的状态栏在好像只能在纵向的模式下使用.如下图 用户可以隐藏状态栏,也可以将状态栏设置 ...
- Debug Certificate expired on 11-5-8 上午 6:26
好久没用电脑上的eclipse,今天新建了个安卓项目,发现报下面的错误: 后来得知: 是Debug证书过期所致. android应用必须经过签名证书进行数字签名后,才能安装,在开发调试阶段,默认情 ...
- 【转】如何在Mac上撰寫C++程式
原文: http://www.macuknow.com/node/4901 本文使用的开发环境:Xcode 5.其实步骤很简单,只需要简单的几步就ok了. 點選Create a new Xcode p ...