forked from huihut/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
修改排序算法,添加插值、斐波那契、哈希、二叉树、红黑树、2-3树、B/B+树查找算法
- Loading branch information
Showing
18 changed files
with
454 additions
and
386 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
二叉搜索树的查找算法: | ||
在二叉搜索树b中查找x的过程为: | ||
1. 若b是空树,则搜索失败,否则: | ||
2. 若x等于b的根节点的数据域之值,则查找成功;否则: | ||
3. 若x小于b的根节点的数据域之值,则搜索左子树;否则: | ||
4. 查找右子树。 | ||
*/ | ||
|
||
// 在根指针T所指二叉查找树中递归地查找其关键字等于key的数据元素,若查找成功, | ||
// 则指针p指向該数据元素节点,并返回TRUE,否则指针指向查找路径上访问的最终 | ||
// 一个节点并返回FALSE,指针f指向T的双亲,其初始调用值为NULL | ||
Status SearchBST(BiTree T, KeyType key, BiTree f, BiTree &p){ | ||
|
||
if(!T) { //查找不成功 | ||
p=f; | ||
return false; | ||
} | ||
else if (key == T->data.key) { //查找成功 | ||
p=T; | ||
return true; | ||
} | ||
else if (key < T->data.key) //在左子树中继续查找 | ||
return SearchBST(T->lchild, key, T, p); | ||
else //在右子树中继续查找 | ||
return SearchBST(T->rchild, key, T, p); | ||
} |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// 斐波那契查找 | ||
|
||
#include "stdafx.h" | ||
#include <memory> | ||
#include <iostream> | ||
using namespace std; | ||
|
||
const int max_size=20;//斐波那契数组的长度 | ||
|
||
/*构造一个斐波那契数组*/ | ||
void Fibonacci(int * F) | ||
{ | ||
F[0]=0; | ||
F[1]=1; | ||
for(int i=2;i<max_size;++i) | ||
F[i]=F[i-1]+F[i-2]; | ||
} | ||
|
||
/*定义斐波那契查找法*/ | ||
int FibonacciSearch(int *a, int n, int key) //a为要查找的数组,n为要查找的数组长度,key为要查找的关键字 | ||
{ | ||
int low=0; | ||
int high=n-1; | ||
|
||
int F[max_size]; | ||
Fibonacci(F);//构造一个斐波那契数组F | ||
|
||
int k=0; | ||
while(n>F[k]-1)//计算n位于斐波那契数列的位置 | ||
++k; | ||
|
||
int * temp;//将数组a扩展到F[k]-1的长度 | ||
temp=new int [F[k]-1]; | ||
memcpy(temp,a,n*sizeof(int)); | ||
|
||
for(int i=n;i<F[k]-1;++i) | ||
temp[i]=a[n-1]; | ||
|
||
while(low<=high) | ||
{ | ||
int mid=low+F[k-1]-1; | ||
if(key<temp[mid]) | ||
{ | ||
high=mid-1; | ||
k-=1; | ||
} | ||
else if(key>temp[mid]) | ||
{ | ||
low=mid+1; | ||
k-=2; | ||
} | ||
else | ||
{ | ||
if(mid<n) | ||
return mid; //若相等则说明mid即为查找到的位置 | ||
else | ||
return n-1; //若mid>=n则说明是扩展的数值,返回n-1 | ||
} | ||
} | ||
delete [] temp; | ||
return -1; | ||
} | ||
|
||
int main() | ||
{ | ||
int a[] = {0,16,24,35,47,59,62,73,88,99}; | ||
int key=100; | ||
int index=FibonacciSearch(a,sizeof(a)/sizeof(int),key); | ||
cout<<key<<" is located at:"<<index; | ||
return 0; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.