第十四周项目2--二叉树排序树中查找的路径
来源:互联网 发布:公司机房网络拓扑图 编辑:程序博客网 时间:2024/05/19 11:50
问题及代码:
/* *Copyright(c) 2015, 烟台大学计算机学院 *All rights reserved. *文件名称:二叉树排序树中查找的路径.cpp *作 者:杜文文 *完成日期:2015年 12月 7日 *问题描述:设计一个算法,输出在二叉排序中查找时查找某个关键字经过的路径。*/
#include <stdio.h>#include <malloc.h>#define MaxSize 100typedef int KeyType; //定义关键字类型typedef char InfoType;typedef struct node //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据域 struct node *lchild,*rchild; //左右孩子指针} BSTNode;int path[MaxSize]; //全局变量,用于存放路径void DispBST(BSTNode *b); //函数说明int InsertBST(BSTNode *&p,KeyType k) //在以*p为根节点的BST中插入一个关键字为k的节点{ if (p==NULL) //原树为空, 新插入的记录为根节点 { p=(BSTNode *)malloc(sizeof(BSTNode)); p->key=k; p->lchild=p->rchild=NULL; return 1; } else if (k==p->key) return 0; else if (k<p->key) return InsertBST(p->lchild,k); //插入到*p的左子树中 else return InsertBST(p->rchild,k); //插入到*p的右子树中}BSTNode *CreatBST(KeyType A[],int n)//由数组A中的关键字建立一棵二叉排序树{ BSTNode *bt=NULL; //初始时bt为空树 int i=0; while (i<n) InsertBST(bt,A[i++]); //将A[i]插入二叉排序树T中 return bt; //返回建立的二叉排序树的根指针}//在二叉排序树中查找,记经过的节点记录在path中,返回值为最后查找节点在path中存储的下标int SearchBST(BSTNode *bt,KeyType k,KeyType path[],int i){ if (bt==NULL) return i; else if (k==bt->key) //找到了节点 { path[i+1]=bt->key; //输出其路径 return i+1; } else { path[i+1]=bt->key; if (k<bt->key) SearchBST(bt->lchild,k,path,i+1); //在左子树中递归查找 else SearchBST(bt->rchild,k,path,i+1); //在右子树中递归查找 }}//查找并显示经过的路径void SearchResult(BSTNode *bt, int k1){ int r, j; r = SearchBST(bt,k1,path,-1); for (j=0; j<=r; j++) printf("%3d",path[j]); printf("\n");}void DispBST(BSTNode *bt)//以括号表示法输出二叉排序树bt{ if (bt!=NULL) { printf("%d",bt->key); if (bt->lchild!=NULL || bt->rchild!=NULL) { printf("("); DispBST(bt->lchild); if (bt->rchild!=NULL) printf(","); DispBST(bt->rchild); printf(")"); } }}int main(){ BSTNode *bt; KeyType k1=65, k2=32; int a[]= {43,91,10,18,82,65,33,59,27,73},n=10; printf("创建的BST树:"); bt=CreatBST(a,n); DispBST(bt); printf("\n"); printf(" 查找%d关键字:",k1); SearchResult(bt,k1); printf(" 查找%d关键字:",k2); SearchResult(bt,k2); return 0;}
运行结果:
0 0
- 第十四周项目2-二叉树排序树中查找的路径
- 第十四周上机实践—项目2—二叉树排序树中查找的路径
- 第十四周项目2-二叉树排序树中查找的路径
- 第十四周——【项目2 - 二叉树排序树中查找的路径】
- 第十四周--项目2二叉树排序树中查找的路径
- 第十四周实践项目2—二叉树排序树中查找的路径
- 第十四周 项目2-二叉树排序树中查找的路径
- 第十四周项目2-二叉树排序树中查找的路径
- 第十四周项目2-二叉树排序树中查找的路径
- 第十四周项目2二叉树排序树中查找的路径
- (第十四周项目2)二叉树排序树中查找的路径
- 第十四周项目2二叉树排序树中查找的路径
- 第十四周 项目2 二叉树排序树中查找的路径
- 第十四周项目2--二叉树排序树中查找的路径
- 第十四周项目2—二叉树排序树中查找的路径
- 第十四周项目2-二叉树排序树中查找的路径
- 第十四周项目2-二叉树排序树中查找的路径
- 第十四周项目2---二叉树排序树中查找的路径
- [Golang] groupcache的简单例子
- 切换手机中英文,读取assets对应的资源文件,设置到ScrollView控件中
- Notification
- Magento调试页面空白 - 解决方法
- actionbar的以下各种属性的区别.
- 第十四周项目2--二叉树排序树中查找的路径
- Nginx的负载均衡 - 保持会话 (ip_hash)
- 关于数据库session连接数目问题
- 第十五周--项目三--B-树的基本操作
- iPhone 弹出对话框 让用户等待
- leetcode -- Binary Tree Paths -- 重点
- Spring AOP介绍及源码分析
- mysql日常运维
- 第四周项目三--单链表的应用