PTA-BinarySearchTree BasicOperation
/* 二叉查找树 基本操作 */
#include <stdio.h>
#include <stdlib.h> typedef int ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode {
ElementType Element;
BinTree Left;
BinTree Right;
}; int PreOrderJudge( BinTree T ); /* 判断是否满足BST */
void PreorderTraversal( BinTree BST ); /* 先序遍历 */
void InorderTraversal( BinTree BST ); /* 中序遍历 */ BinTree Insert( BinTree BST, ElementType X );
BinTree Delete( BinTree BST, ElementType X );
Position Find( BinTree BST, ElementType X );
Position FindMin( BinTree BST );
Position FindMax( BinTree BST );
int main() {
BinTree BST, MinP, MaxP, Tmp;
ElementType X;
int N, i;
/* Insert node to build a BST */
BST = NULL;
scanf("%d", &N);
for ( i = ; i < N; i++ ) {
scanf("%d", &X);
BST = Insert(BST, X);
}
printf("Preorder: ");
PreorderTraversal(BST);
printf("\nInorder: ");
InorderTraversal(BST);
printf("\n");
MinP = FindMin(BST);
MaxP = FindMax(BST);
scanf("%d", &N); /* search N numebr */
for ( i = ; i < N; i++ ) {
scanf("%d", &X);
Tmp = Find(BST, X);
if ( Tmp == NULL )
printf("%d is not found\n", X);
else {
printf("%d is found\n", Tmp->Element);
if ( Tmp == MinP )
printf("%d is the smallest key\n", Tmp->Element);
if ( Tmp == MaxP )
printf("%d is the largest key\n", Tmp->Element);
}
} /* for */
/* Delete all nodes */
scanf("%d", &N);
for ( i = ; i < N; i++ ) {
scanf("%d", &X);
BST = Delete(BST, X);
}
return ;
}
BinTree Insert( BinTree BST, ElementType X )
{
if ( !BST ) {
/* 若原树为空,生成并返回一个结点的二叉搜索树 */
BST = (BinTree)malloc(sizeof(struct TNode));
BST->Element = X;
BST->Left = BST->Right = NULL;
} else {
/* 开始找要插入元素的位置 */
if ( X < BST->Element )
BST->Left = Insert( BST->Left, X ); /*递归插入左子树*/
else if ( X > BST->Element )
BST->Right = Insert( BST->Right, X ); /*递归插入右子树*/
/* else X已经存在,什么都不做 */
}
return BST;
}
BinTree Delete( BinTree BST, ElementType X )
{
Position Tmp;
if ( !BST )
printf("Not Found\n");
else {
if ( X < BST->Element )
BST->Left = Delete( BST->Left, X ); /* 从左子树递归删除 */
else if ( X > BST->Element )
BST->Right = Delete( BST->Right, X ); /* 从右子树递归删除 */
else { /* BST就是要删除的结点 */
/* 如果被删除结点有左右两个子结点 */
if ( BST->Left && BST->Right ) {
/* 从右子树中找最小的元素填充删除结点 */
Tmp = FindMin( BST->Right );
BST->Element = Tmp->Element;
/* 从右子树中删除最小元素 */
BST->Right = Delete( BST->Right, BST->Element );
} else {
/* 被删除结点有一个或无子结点 */
Tmp = BST;
if( !BST->Left ) /* 只有右孩子或无子结点 */
BST = BST->Right;
else /* 只有左孩子 */
BST = BST->Left;
free( Tmp );
}
} /* else */
}
return BST;
}
Position Find( BinTree BST , ElementType X )
{
if ( !BST ) return NULL; /*查找失败*/
if ( X > BST->Element )
return Find( BST->Right, X ); /*在右子树中继续查找*/
else if ( X < BST->Element )
return Find( BST->Left, X); /*在左子树中继续查找*/
else /* X == BST->Element */
return BST; /*查找成功,返回结点的找到结点的地址*/
}
Position FindMin( BinTree BST )
{
if ( !BST ) return NULL; /*空的二叉搜索树,返回NULL*/
else if ( !BST->Left )
return BST; /*找到最左叶结点并返回*/
else
return FindMin( BST->Left ); /*沿左分支继续查找*/
}
Position FindMax( BinTree BST )
{
if ( BST ) {
while( BST->Right )
BST = BST->Right;
/*沿右分支继续查找,直到最右叶结点*/
return BST;
}
}
void InorderTraversal( BinTree BST )
{
if ( BST ) {
InorderTraversal( BST->Left );
printf("%d", BST->Element);
InorderTraversal( BST->Right );
}
}
void PreorderTraversal( BinTree BT )
{
if( BT ) {
printf("%d ", BT->Element);
PreorderTraversal( BT->Left );
PreorderTraversal( BT->Right );
}
}
void PreOrderJudge( BinTree BST )
{
if ( BST == NULL ) {
printf("Empty Tree!");
return;
} else if ( BST ) {
if ( BST->Left ) {
/* 左儿子更大 */
if( BST->Left->Element >= BST->Element )
return;
}
if ( BST->Right ) {
/* 右儿子更小 */
if ( BST->Right->Element <= BST->Element )
return;
}
PreOrderJudge( BST->Left );
PreOrderJudge( BST->Right );
}
}

PTA-BinarySearchTree BasicOperation的更多相关文章
- 二叉搜索树BinarySearchTree(C实现)
头文件—————————————————————————————— #ifndef _BINARY_SEARCH_TREE_H_ #define _BINARY_SEARCH_TREE_H_ #inc ...
- 浙大PTA - - 堆中的路径
题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21731 本题即考察最小堆的基本操作: #include "iostrea ...
- 浙大PTA - - File Transfer
题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21732 #include "iostream" #includ ...
- ERROR<53761> - Plugins - conn=-1 op=-1 msgId=-1 - Connection Bind through PTA failed (91). Retrying...
LDAP6.3在DSCC控制台启动实例完成,但是操作状态显示“意外错误”,查看日志如下: 04/May/2016:21:10:39 +0800] - Sun-Java(tm)-System-Direc ...
- PTA中提交Java程序的一些套路
201708新版改版说明 PTA与2017年8月已升级成新版,域名改为https://pintia.cn/,官方建议使用Firefox与Chrome浏览器. 旧版 PTA 用户首次在新版系统登录时,请 ...
- PTA分享码-Java
主要用于Java语法练习,非竞赛类题目. 1. Java入门 959dbf0b7729daa61d379ec95fb8ddb0 2. Java基本语法 23bd8870e ...
- 二叉树终极教程--BinarySearchTree
BinarySearchTreeMap 的 实现 public interface Map<K extends Comparable<K>, V> { void put(K k ...
- C语言第一次实验报告————PTA实验1.2.3内容
一.PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度100°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...
- PTA题---求两个有序序列中位数所体现的思想。
---恢复内容开始--- 近日,在做PTA题目时,遇到了一个这样的题,困扰了很久.题目如下:已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数.有序序列A0,A1, ...
- 第十四,十五周PTA作业
1.第十四周part1 7-3 #include<stdio.h> int main() { int n; scanf("%d",&n); int a[n]; ...
随机推荐
- oracle lz047中的REGEXP_LIKE(cust_first_name,'[[:digit:]]')) .
转自http://blog.csdn.net/dream19881003/article/details/6680982 今天在看OCP题库的时候有一道题是考字段约束的,意思是要在表CUSTOMERS ...
- centos7 安装mariadb最新版并配置
打开http://mirrors.aliyun.com/,查找mariadb,然后拼装地址http://mirrors.aliyun.com/mariadb/yum打开,点开你想要的版本,选择你的操作 ...
- apache ftp server的简单入门(java应用内嵌ftp server)
Apache Ftp Server:(强调) Apache Ftp Server 是100%纯Java的FTP服务器软件,它采用MINA网络框架开发具有非常好的性能.Apache FtpServer ...
- MongoDB学习笔记(一)——Windows 下安装MongoDB
首先从官网下载mongodb的windows安装包,根据自己系统类型选择32位或者64位版本安装即可,然后根据提示一路下一步即可安装完成.如果没有修改安装目录会默认安装在C:\Program Fil ...
- MySql EF6 DBFirst 向导无法生成 edmx 解决方法(同:您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库提供程序)
使用 MySql EF6 DBfirst 生成模型时经常会遇到EF6模式无法选择的情况究其原因, 还是因为没有正确的使用 Connector/Net. 下面说一下使用方法. 使用 MySql DBFi ...
- wxpython 窗口排版- proportion/flag/border参数说明
新学习wxpython,一直纠结于窗口控件的排版,经过几天的查资料.试验,总结如下. 1.需求实例 来个实例,窗口有3行控件 第一行是文本提示(大小不变,文字左对齐,控件居左). 第二行依次为文本提示 ...
- Redis学习---Redis操作之Hash
hash表现形式上有些像pyhton中的dict,可以存储一组关联性较强的数据[有点像嵌套字典] hset(name, key, value) --> 设置hash的操作 # 参数: # ...
- Redis学习---CentOs/RedHat下Redis的安装
redis是C语言开发,建议在linux上运行,本教程使用Centos6.4作为安装环境. 安装redis需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gc ...
- Linux 系统的日志目录
连接时间的日志 连接时间日志一般由/var/log/wtmp和/var/run/utmp这两个文件记录,通过who查看 who /var/log/wtmp [连接时间日志] who /var/log/ ...
- Hadoop HBase概念学习系列之HBase里的时间戳(二十六)
HBase集群要求每个节点的时间必须同步.HBase对于节点的时间扭曲(time skew)容忍度很低(这和HDFS是不一样的). 这主要是因为HBase需要使用系统时间来产生时间戳.如果系统时间不同 ...