Skip to content

Commit 296d3cf

Browse files
committed
修改排序算法,添加插值、斐波那契、哈希、二叉树、红黑树、2-3树、B/B+树查找算法
1 parent 91d24f1 commit 296d3cf

18 files changed

+454
-386
lines changed

Algorithm/BSTSearch.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
二叉搜索树的查找算法:
3+
4+
在二叉搜索树b中查找x的过程为:
5+
6+
1. 若b是空树,则搜索失败,否则:
7+
2. 若x等于b的根节点的数据域之值,则查找成功;否则:
8+
3. 若x小于b的根节点的数据域之值,则搜索左子树;否则:
9+
4. 查找右子树。
10+
11+
*/
12+
13+
// 在根指针T所指二叉查找树中递归地查找其关键字等于key的数据元素,若查找成功,
14+
// 则指针p指向該数据元素节点,并返回TRUE,否则指针指向查找路径上访问的最终
15+
// 一个节点并返回FALSE,指针f指向T的双亲,其初始调用值为NULL
16+
Status SearchBST(BiTree T, KeyType key, BiTree f, BiTree &p){
17+
18+
if(!T) { //查找不成功
19+
p=f;
20+
return false;
21+
}
22+
else if (key == T->data.key) { //查找成功
23+
p=T;
24+
return true;
25+
}
26+
else if (key < T->data.key) //在左子树中继续查找
27+
return SearchBST(T->lchild, key, T, p);
28+
else //在右子树中继续查找
29+
return SearchBST(T->rchild, key, T, p);
30+
}

Algorithm/BruteForceStringMatch.h

-14
This file was deleted.
File renamed without changes.
File renamed without changes.

Algorithm/FibonacciSearch.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// 斐波那契查找
2+
3+
#include "stdafx.h"
4+
#include <memory>
5+
#include <iostream>
6+
using namespace std;
7+
8+
const int max_size=20;//斐波那契数组的长度
9+
10+
/*构造一个斐波那契数组*/
11+
void Fibonacci(int * F)
12+
{
13+
F[0]=0;
14+
F[1]=1;
15+
for(int i=2;i<max_size;++i)
16+
F[i]=F[i-1]+F[i-2];
17+
}
18+
19+
/*定义斐波那契查找法*/
20+
int FibonacciSearch(int *a, int n, int key) //a为要查找的数组,n为要查找的数组长度,key为要查找的关键字
21+
{
22+
int low=0;
23+
int high=n-1;
24+
25+
int F[max_size];
26+
Fibonacci(F);//构造一个斐波那契数组F
27+
28+
int k=0;
29+
while(n>F[k]-1)//计算n位于斐波那契数列的位置
30+
++k;
31+
32+
int * temp;//将数组a扩展到F[k]-1的长度
33+
temp=new int [F[k]-1];
34+
memcpy(temp,a,n*sizeof(int));
35+
36+
for(int i=n;i<F[k]-1;++i)
37+
temp[i]=a[n-1];
38+
39+
while(low<=high)
40+
{
41+
int mid=low+F[k-1]-1;
42+
if(key<temp[mid])
43+
{
44+
high=mid-1;
45+
k-=1;
46+
}
47+
else if(key>temp[mid])
48+
{
49+
low=mid+1;
50+
k-=2;
51+
}
52+
else
53+
{
54+
if(mid<n)
55+
return mid; //若相等则说明mid即为查找到的位置
56+
else
57+
return n-1; //若mid>=n则说明是扩展的数值,返回n-1
58+
}
59+
}
60+
delete [] temp;
61+
return -1;
62+
}
63+
64+
int main()
65+
{
66+
int a[] = {0,16,24,35,47,59,62,73,88,99};
67+
int key=100;
68+
int index=FibonacciSearch(a,sizeof(a)/sizeof(int),key);
69+
cout<<key<<" is located at:"<<index;
70+
return 0;
71+
}

Algorithm/FileSearch/README.md

-20
This file was deleted.

Algorithm/FileSearch/input.txt

-5
This file was deleted.

Algorithm/FileSearch/search.cpp

-105
This file was deleted.

Algorithm/FileSearch/search.exe

-122 KB
Binary file not shown.

Algorithm/FileSort/README.md

-24
This file was deleted.

Algorithm/FileSort/input.txt

-1
This file was deleted.

Algorithm/FileSort/output.txt

-1
This file was deleted.

0 commit comments

Comments
 (0)