第十三周项目二

  1. 烟台大学计算机学院   
  2.   
  3. 作者:王雪行  
  4.   
  5. 问题描述:设计一个算法,输出在二叉排序中查找时查找某个关键字经过的路径  
  6.   
  7. 输入描述:无 
  8.   
  9. 输出描述:输出路径 
  10.   
  11. */   
  12.   
  13.   
  14.   
  15.   
  16. #include <stdio.h>  
  17. #include <malloc.h>  
  18. #define MaxSize 100  
  19. typedef int KeyType;                    //定义关键字类型  
  20. typedef char InfoType;  
  21. typedef struct node                     //记录类型  
  22. {  
  23.     KeyType key;                        //关键字项  
  24.     InfoType data;                      //其他数据域  
  25.     struct node *lchild,*rchild;        //左右孩子指针  
  26. } BSTNode;  
  27. int path[MaxSize];                      //全局变量,用于存放路径  
  28. void DispBST(BSTNode *b);               //函数说明  
  29. int InsertBST(BSTNode *&p,KeyType k)    //在以*p为根节点的BST中插入一个关键字为k的节点  
  30. {  
  31.     if (p==NULL)                        //原树为空, 新插入的记录为根节点  
  32.     {  
  33.         p=(BSTNode *)malloc(sizeof(BSTNode));  
  34.         p->key=k;  
  35.         p->lchild=p->rchild=NULL;  
  36.         return 1;  
  37.     }  
  38.     else if (k==p->key)  
  39.         return 0;  
  40.     else if (k<p->key)  
  41.         return InsertBST(p->lchild,k);  //插入到*p的左子树中  
  42.     else  
  43.         return InsertBST(p->rchild,k);  //插入到*p的右子树中  
  44. }  
  45. BSTNode *CreatBST(KeyType A[],int n)  
  46. //由数组A中的关键字建立一棵二叉排序树  
  47. {  
  48.     BSTNode *bt=NULL;                   //初始时bt为空树  
  49.     int i=0;  
  50.     while (i<n)  
  51.         InsertBST(bt,A[i++]);       //将A[i]插入二叉排序树T中  
  52.     return bt;                          //返回建立的二叉排序树的根指针  
  53. }  
  54.   
  55. //在二叉排序树中查找,记经过的节点记录在path中,返回值为最后查找节点在path中存储的下标  
  56. int SearchBST(BSTNode *bt,KeyType k,KeyType path[],int i)  
  57. {  
  58.     if (bt==NULL)  
  59.         return i;  
  60.     else if (k==bt->key)    //找到了节点  
  61.     {  
  62.         path[i+1]=bt->key;  //输出其路径  
  63.         return i+1;  
  64.     }  
  65.     else  
  66.     {  
  67.         path[i+1]=bt->key;  
  68.         if (k<bt->key)  
  69.             SearchBST(bt->lchild,k,path,i+1);  //在左子树中递归查找  
  70.         else  
  71.             SearchBST(bt->rchild,k,path,i+1);  //在右子树中递归查找  
  72.     }  
  73. }  
  74.   
  75. //查找并显示经过的路径  
  76. void SearchResult(BSTNode *bt, int k1)  
  77. {  
  78.     int r, j;  
  79.     r = SearchBST(bt,k1,path,-1);  
  80.     for (j=0; j<=r; j++)  
  81.         printf("%3d",path[j]);  
  82.     printf("\n");  
  83. }  
  84.   
  85. void DispBST(BSTNode *bt)  
  86. //以括号表示法输出二叉排序树bt  
  87. {  
  88.     if (bt!=NULL)  
  89.     {  
  90.         printf("%d",bt->key);  
  91.         if (bt->lchild!=NULL || bt->rchild!=NULL)  
  92.         {  
  93.             printf("(");  
  94.             DispBST(bt->lchild);  
  95.             if (bt->rchild!=NULL) printf(",");  
  96.             DispBST(bt->rchild);  
  97.             printf(")");  
  98.         }  
  99.     }  
  100. }  
  101.   
  102. int main()  
  103. {  
  104.     BSTNode *bt;  
  105.     KeyType k1=65, k2=32;  
  106.     int a[]= {43,91,10,18,82,65,33,59,27,73},n=10;  
  107.     printf("创建的BST树:");  
  108.     bt=CreatBST(a,n);  
  109.     DispBST(bt);  
  110.     printf("\n");  
  111.     printf("  查找%d关键字:",k1);  
  112.     SearchResult(bt,k1);  
  113.     printf("  查找%d关键字:",k2);  
  114.     SearchResult(bt,k2);  
  115.     return 0;  
  116. }  

运行结果:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值