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]; ...
随机推荐
- text-overflow修剪文本,以省略号显示
overflow: hidden; 必须设置,不然无法修剪文本 white-space: nowrap; 规定段落中的文本不进行换行 text-overflow: ellipsis; 当文本溢出 ...
- 让索引包含null值的两种方法
1. 把有NULL值的列与一个常数,或者一个带有not null约束的列一同索引 create index ind_01 on t01(col01,1); 或者 create index ind_01 ...
- Oracle闪回(FlashBack)数据库
Flashback Database功能非常类似与RMAN的不完全恢复,它可以把整个数据库回退到过去的某个时点的状态,这个功能依赖于Flashback log日志.比RMAN更快速和高效,因此Flas ...
- Oracle常见等待事件
1Buffer busy waits从本质上讲,这个等待事件的产生仅说明了一个会话在等待一个Buffer(数据块),但是导致这个现象的原因却有很多种.常见的两种是: · 当一个会话视图 ...
- etcd 分布式数据库概念初探
Lease(租约): 其实就是一个定时器.首先申请一个TTL=N的lease(定时器),然后创建key的时候传入该lease,那么就实现了一个定时的key. 在程序中可以定时为该lease续约,也就是 ...
- 名词解释:Linux内存管理之RSS和VSZ
Linux内存管理中不管是top命令还是pmap命令,都会有RSS和VSZ这两个名词,这里解释一下: RSS( Resident Set Size )常驻内存集合大小,表示相应进程在RAM中占用了多少 ...
- UITableVIew与UICollectionView带动画删除cell时崩溃的处理
UITableVIew与UICollectionView带动画删除cell时崩溃的处理 -会崩溃的原因是因为没有处理好数据源与cell之间的协调关系- 效果: tableView的源码: ModelC ...
- UNIX高级环境编程(8)进程环境(Process Environment)- 进程的启动和退出、内存布局、环境变量列表
在学习进程控制相关知识之前,我们需要了解一个单进程的运行环境. 本章我们将了解一下的内容: 程序运行时,main函数是如何被调用的: 命令行参数是如何被传入到程序中的: 一个典型的内存布局是怎样的: ...
- 一、JavaScript概述 二、JavaScript的语法 三、JavaScript的内置对象
一.JavaScript的概述###<1>JavaScript的概念 又称ECMAScript,和java没有任何关系 嵌入在HTML元素中的 被浏览器解释运行的 一种脚本语言. ###& ...
- November 9th 2016 Week 46th Wednesday
Love is the poetry of the scenes. 爱是感官之诗. Recently I always feel lonely, I badly hope that I can fin ...