三木社区

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 417|回复: 0

C语言数据结构-algo9-3.c

[复制链接]

1562

主题

1564

帖子

4904

积分

博士

Rank: 8Rank: 8

积分
4904
发表于 2017-9-1 09:40:52 | 显示全部楼层 |阅读模式
  1. /* algo9-3.c 静态查找表(静态树表)的操作 */
  2. #include"c1.h"
  3. #define N 9 /* 数据元素个数 */
  4. typedef char KeyType; /* 设关键字域为字符型 */
  5. typedef struct /* 数据元素类型 */
  6. {
  7.    KeyType key;
  8.    int weight;
  9. }ElemType;
  10. ElemType r[N]={{'A',1},{'B',1},{'C',2},{'D',5},{'E',3},
  11.                 {'F',4},{'G',4},{'H',3},{'I',5}}; /* 数据元素(以教科书例9-1为例),全局变量 */
  12. int sw[N+1]; /* 累计权值,全局变量 */
  13. #include"c9.h"
  14. #include"c9-1.h"
  15. #include"bo9-1.c"

  16. typedef ElemType TElemType;
  17. #include"c6-2.h"
  18. Status SecondOptimal(BiTree *T, ElemType R[],int sw[],int low,int high)
  19. { /* 由有序表R[low..high]及其累计权值表sw(其中sw[0]==0)递归构造 */
  20.    /* 次优查找树T。算法9.3 */
  21.    int i,j;
  22.    double min,dw;
  23.    i=low;
  24.    min=fabs(sw[high]-sw[low]);
  25.    dw=sw[high]+sw[low-1];
  26.    for(j=low+1;j<=high;++j) /* 选择最小的△Pi值 */
  27.      if(fabs(dw-sw[j]-sw[j-1])<min)
  28.      {
  29.        i=j;
  30.        min=fabs(dw-sw[j]-sw[j-1]);
  31.      }
  32.    *T=(BiTree)malloc(sizeof(BiTNode));
  33.    if(!*T)
  34.      return ERROR;
  35.    (*T)->data=R[i]; /* 生成结点 */
  36.    if(i==low)
  37.      (*T)->lchild=NULL; /* 左子树空 */
  38.    else
  39.      SecondOptimal(&(*T)->lchild,R,sw,low,i-1); /* 构造左子树 */
  40.    if(i==high)
  41.      (*T)->rchild=NULL; /* 右子树空 */
  42.    else
  43.      SecondOptimal(&(*T)->rchild,R,sw,i+1,high); /* 构造右子树 */
  44.    return OK;
  45. }

  46. void FindSW(int sw[],SSTable ST)
  47. { /* 按照有序表ST中各数据元素的Weight域求累计权值表sw */
  48.    int i;
  49.    sw[0]=0;
  50.    for(i=1;i<=ST.length;i++)
  51.      sw[i]=sw[i-1]+ST.elem[i].weight;
  52. }

  53. typedef BiTree SOSTree; /* 次优查找树采用二叉链表的存储结构 */
  54. Status CreateSOSTree(SOSTree *T,SSTable ST)
  55. { /* 由有序表ST构造一棵次优查找树T。ST的数据元素含有权域weight。算法9.4 */
  56.    if(ST.length==0)
  57.      *T=NULL;
  58.    else
  59.    {
  60.      FindSW(sw,ST); /* 按照有序表ST中各数据元素的Weight域求累计权值表sw */
  61.      SecondOptimal(T,ST.elem,sw,1,ST.length);
  62.    }
  63.    return OK;
  64. }

  65. Status Search_SOSTree(SOSTree *T,KeyType key)
  66. { /* 在次优查找树T中查找关键字等于key的元素。找到则返回OK,否则返回FALSE */
  67.    while(*T) /* T非空 */
  68.      if((*T)->data.key==key)
  69.        return OK;
  70.      else if((*T)->data.key>key)
  71.        *T=(*T)->lchild;
  72.      else
  73.        *T=(*T)->rchild;
  74.    return FALSE; /* 顺序表中不存在待查元素 */
  75. }

  76. void print(ElemType c) /* Traverse()调用的函数 */
  77. {
  78.    printf("(%c %d) ",c.key,c.weight);
  79. }

  80. void main()
  81. {
  82.    SSTable st;
  83.    SOSTree t;
  84.    Status i;
  85.    KeyType s;
  86.    Creat_Ord(&st,N); /* 由全局数组产生非降序静态查找表st */
  87.    Traverse(st,print);
  88.    CreateSOSTree(&t,st); /* 由有序表构造一棵次优查找树 */
  89.    printf("\n请输入待查找的字符: ");
  90.    scanf("%c",&s);
  91.    i=Search_SOSTree(&t,s);
  92.    if(i)
  93.      printf("%c的权值是%d\n",s,t->data.weight);
  94.    else
  95.      printf("表中不存在此字符\n");
  96. }
  97. 
复制代码


回复

使用道具 举报

Archiver|手机版|小黑屋|三木电子社区 ( 辽ICP备11000133号-4 )

辽公网安备 21021702000620号

GMT+8, 2025-11-18 18:43 , Processed in 0.044147 second(s), 23 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表