二叉搜索例子

#include <stdio.h>

//not find will return -1
//find will return the index
int searchR(int a[], int l, int r, int key)
{
    if(l>r) return -1;
    int m=(l+r)/2;
    if(a[m] == key) return m;
    if(l==r) return -1;
    if(key < a[m])
        return searchR(a, l, m-1, key);
    else
        return searchR(a, m+1, r, key);
}

int main()
{
    int a[10] = {1,2,3,4,5,6,6,7,8,9};
    int n = searchR(a,0,9, 5);
    if(n != -1)
        printf("find: index is %d\n", n);
    return 0;
}
原文地址:https://www.cnblogs.com/wouldguan/p/2767548.html