第十三周项目2

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