Skip to content

Commit

Permalink
修改排序算法,添加插值、斐波那契、哈希、二叉树、红黑树、2-3树、B/B+树查找算法
Browse files Browse the repository at this point in the history
  • Loading branch information
huihut committed Apr 16, 2018
1 parent 91d24f1 commit 296d3cf
Show file tree
Hide file tree
Showing 18 changed files with 454 additions and 386 deletions.
30 changes: 30 additions & 0 deletions Algorithm/BSTSearch.h
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);
}
14 changes: 0 additions & 14 deletions Algorithm/BruteForceStringMatch.h

This file was deleted.

File renamed without changes.
File renamed without changes.
71 changes: 71 additions & 0 deletions Algorithm/FibonacciSearch.cpp
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;
}
20 changes: 0 additions & 20 deletions Algorithm/FileSearch/README.md

This file was deleted.

5 changes: 0 additions & 5 deletions Algorithm/FileSearch/input.txt

This file was deleted.

105 changes: 0 additions & 105 deletions Algorithm/FileSearch/search.cpp

This file was deleted.

Binary file removed Algorithm/FileSearch/search.exe
Binary file not shown.
24 changes: 0 additions & 24 deletions Algorithm/FileSort/README.md

This file was deleted.

1 change: 0 additions & 1 deletion Algorithm/FileSort/input.txt

This file was deleted.

1 change: 0 additions & 1 deletion Algorithm/FileSort/output.txt

This file was deleted.

Loading

0 comments on commit 296d3cf

Please sign in to comment.