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

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

运行结果:

0 0