第十四周 项目二二叉树排序树中查找的路径

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值