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

问题:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /*   
  2. * Copyright (c)2016,烟台大学计算机与控制工程学院   
  3. * All rights reserved.   
  4. * 文件名称:项目2.cbp   
  5. * 作    者:陈晓琳  
  6. * 完成日期:2016年12月9日   
  7. * 版 本 号:v1.0   
  8.    
  9. * 问题描述:设计一个算法,输出在二叉排序中查找时查找某个关键字经过的路径。   
  10.    
  11. * 输入描述:无   
  12. * 程序输出:测试数据   
  13. */     

代码:

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

运行结果:


知识点总结:

二叉树排序树的应用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值