查找

//算法7.3 折半查找



#include
using namespace std;
#define MAXSIZE 100
#define OK 1;

typedef struct{
    int key;//关键字域
}ElemType;

typedef struct{
    ElemType *R;
    int length;
}SSTable;

int InitList_SSTable(SSTable &L)
{
    L.R=new ElemType[MAXSIZE];
    if (!L.R)
    {
        cout<<"初始化错误";
        return 0;
    }
    L.length=0;
    return OK;
}

int Insert_SSTable(SSTable &L) 
{
    int j=1;
    for(int i=1;iST.R[mid].key)return Search_Bin2(ST,key,mid+1,high);  
      else return 0;
      //继续在后一子表进行查找
   //}//while
     
       
         
           
}// Search_Bin

void Show_End(int result,int testkey)
{
    if(result==0)
        cout<<"未找到"<>testkey1>>testkey2;

    cout<<"非递归折半查找"<
//文件名:exp9-5.cpp
#include
using namespace std;
#define MAXWORD 100
typedef struct tnode 
{
    char ch;      //字符
    int count;    //出现次数
    struct tnode *lchild,*rchild;
}  tnode ,*BTree;
void CreaTree(BTree &p,char c) //采用递归方式构造一棵二叉排序树
{
    if (p==NULL)                //p为NULL,则建立一个新结点
    {
        p=new tnode;
        p->ch=c;
        p->count=1;
        p->lchild=p->rchild=NULL;
    }
    else if (c==p->ch){
        p->count++;
    }
    else if (cch) {
        p = p->lchild;
        p->ch=c;
    }
    else {
        p = p->rchild;
        p->ch=c;
    }
}
void InOrder(BTree p)   //中序遍历BST
{
    if (p!=NULL) 
    {
        InOrder(p->lchild);                 //中序遍历左子树
        cout<ch<<":"<count<rchild);                 //中序遍历右子树
    }
}
int main()
{
    BTree root=NULL;
    int i=0;
    char str[MAXWORD];
    cout<<("输入字符串:");
    gets(str);
    while (str[i]!='\0') 
    {
        CreaTree(root,str[i]);
        i++;
    }
    cout<<"字符及出现次数:\n";
    InOrder(root);
    cout<

你可能感兴趣的:(查找)