数据结构--查找(静态查找表)

构造了次优先查找树
// Nearly Optimal Search Tree.cpp : Defines the entry point for the console application.
/*-----CODE FOR FUN---------------
-------CREATED BY Dream_Whui------
-------2015-3-4-------------------------*/  

#include "stdafx.h"
#include <cmath>
#include <iostream>  
using namespace std;

#define        TRUE                     1  
#define        FALSE                    0  
#define        OK                         1  
#define        ERROR                   0  
#define        OVERFLOW            -2  
#define        INFEASIBLE            -1

#define        EQ(a,b)        (a == b)
#define        LT(a,b)        (a < b)
#define        LQ(a,b)        (a > b)

#define        N                9            //有序表中元素个数

typedef        char            KeyType;

struct ElemType
{
    KeyType    key;
    int            weight;
};

typedef struct BiTNode                    //定义二叉树的结构  
{  
    ElemType    data;                        //数据域  
    struct BiTNode    *lchild,*rchild;//左子树,右子树  
}BiTNode,*BiTree;

typedef struct    SSTable            //定义表的结构
{
    ElemType        *elem;                
    int                length;
};

typedef        BiTree        SOSTree;

void FindSW(float sw[], SSTable ST)//累计权值
{
    int i;
    sw[0] = 0;
    for(i=1; i<=ST.length; i++)
        sw[i] = sw[i-1]+ST.elem[i].weight;
}

int Create_Seq(SSTable &st, ElemType r[], int n)//构造有序表
{
    st.elem = (ElemType*)malloc(sizeof(ElemType) * (n+1));
    if(!st.elem)
        return ERROR;
    int i;
    for(i=1; i<=n; i++)
        st.elem[i] = r[i-1];
    st.length = n;
    return OK;
}

void SecondOptimal(BiTree &T, ElemType R[], float sw[], int low, int high)
{                //由有序表R[low..high]及其累计权值表sw递归构造次优先查找树T
    int i,j,min,dw;
    i = low;
    min = abs(sw[high] - sw[low]);
    dw = sw[high] + sw[low-1];
    for(j=low+1; j<=high; j++)            //选择最小的△Pi值
        if(abs(dw-sw[j]-sw[j-1]) < min)
        {
            i = j;
            min = abs(dw-sw[j]-sw[j-1]);
        }
    T = (BiTree)malloc(sizeof(BiTNode));// 生成结点  
    if(!T)
        return ;
    T->data = R[i];
    if(i==low)
        T->lchild = NULL;                            //左子树空
    else
        SecondOptimal(T->lchild, R, sw, low, i-1);//构造左子树
    if(i == high)
        T->rchild = NULL;                            //右子树空
    else
        SecondOptimal(T->rchild, R, sw, i+1, high);//构造右子树
}

int CreateSOSTree(SOSTree &T, SSTable ST)//由有序表ST构造一颗次优先查找树T,ST的数据元素含有权域weight
{
    float sw[N+1];
    if(ST.length == 0)
        T = NULL;
    else
    {
        FindSW(sw,ST);//按照由有序表ST中各元素的weight域求累计权值表sw
        //for(int i=0; i<=N;i++)
        //    cout<<sw[i]<<endl;
        SecondOptimal(T, ST.elem, sw, 1, ST.length);
    }
    return OK;
}

int Visit(KeyType e)  
{  
    cout<<e<<" ";  
    return OK;  
}

int PreOrderTraverse(BiTree T, int(*Visit)(KeyType))//先序遍历  
{  
    if(T)  
    {  
        Visit(T->data.key);//先访问根  
        PreOrderTraverse(T->lchild,Visit);//再访问左子树  
        PreOrderTraverse(T->rchild,Visit);//最后右子树  
    }  
    return OK;  
}  

// 在次优查找树T中查找关键字等于key的元素。找到则返回OK,否则返回FALSE  
int Search_SOSTree(SOSTree &T,KeyType key)  
{   
    while(T) // T非空  
        if(T->data.key==key)  
            return OK;  
        else if(T->data.key>key)  
            T=T->lchild;  
        else  
            T=T->rchild;  
    return FALSE; // 顺序表中不存在待查元素  
}  
int _tmain(int argc, _TCHAR* argv[])
{
    SSTable st;
    SOSTree T;
    ElemType R[9] = {{'A',1},{'B',1},{'C',2},{'D',5},{'E',3},{'F',4},{'G',4},{'H',3},{'I',5}};
    Create_Seq(st,R,9);
    int i;
    for(i=1; i<=st.length; i++)
        cout<<i<<":"<<st.elem[i].key<<" "<<st.elem[i].weight<<endl;
    CreateSOSTree(T,st);
    PreOrderTraverse(T,Visit);
    if(Search_SOSTree(T,'D'))
        cout<<"成功!"<<endl;
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值