/*  二叉查找树 基本操作  */
#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的更多相关文章

  1. 二叉搜索树BinarySearchTree(C实现)

    头文件—————————————————————————————— #ifndef _BINARY_SEARCH_TREE_H_ #define _BINARY_SEARCH_TREE_H_ #inc ...

  2. 浙大PTA - - 堆中的路径

    题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21731 本题即考察最小堆的基本操作: #include "iostrea ...

  3. 浙大PTA - - File Transfer

    题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21732 #include "iostream" #includ ...

  4. 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 ...

  5. PTA中提交Java程序的一些套路

    201708新版改版说明 PTA与2017年8月已升级成新版,域名改为https://pintia.cn/,官方建议使用Firefox与Chrome浏览器. 旧版 PTA 用户首次在新版系统登录时,请 ...

  6. PTA分享码-Java

    主要用于Java语法练习,非竞赛类题目.   1. Java入门          959dbf0b7729daa61d379ec95fb8ddb0   2. Java基本语法   23bd8870e ...

  7. 二叉树终极教程--BinarySearchTree

    BinarySearchTreeMap 的 实现 public interface Map<K extends Comparable<K>, V> { void put(K k ...

  8. C语言第一次实验报告————PTA实验1.2.3内容

    一.PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度100°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...

  9. PTA题---求两个有序序列中位数所体现的思想。

    ---恢复内容开始--- 近日,在做PTA题目时,遇到了一个这样的题,困扰了很久.题目如下:已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数.有序序列A​0​​,A​1​​, ...

  10. 第十四,十五周PTA作业

    1.第十四周part1 7-3 #include<stdio.h> int main() { int n; scanf("%d",&n); int a[n]; ...

随机推荐

  1. canvas验证码 - 滑块拼图

    滑块拼图型的验证方式已经流行起来,多数的实现方式是直接加载两张分割好的图片.现在用canvas去自动修剪图片,节省修图工作量和http请求: 加载一张整图,用canvas切割缺口,缺口位置在固定范围内 ...

  2. java基础(四) java运算顺序的深入解析

    1. 从左往右的计算顺序   与C/C++不同的是,在Java中,表达式的计算与结果是确定的,不受硬件与环境的影响.如: int i = 5; int j = (i++) + (i++) +(i++) ...

  3. CSS 内外边距 float positio属性

    一.外边距和内边 margin:            用于控制元素与元素之间的距离 外边距:margin的最基本用途就是控制元素周围空间的间隔,从视觉角度上达到相互隔开的目的. padding:   ...

  4. CSS 颜色 字体 背景 文本 边框 列表 display属性

    1  颜色属性 <div style="color:blueviolet">ppppp</div> <div style="color:#f ...

  5. springMVC入门-08

    这一讲介绍用户登录实现以及两种异常处理controller控制器的方法,最后提一下在springMVC中对静态资源访问的实现方法. 用户登录需要一个登录页面login.jsp,对应代码如下所示: &l ...

  6. Windows+Git+TortoiseGit+COPSSH 安装教程及问题收集

    准备工作: 1. git-1.8.1.2-preview20130201.exe 下载地址: https://code.google.com/p/msysgit/downloads/list 2. C ...

  7. ActiveMq--消息队列

    ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ的目录: bin存放的是脚本文件 conf存放的是基本配置文件 data ...

  8. centos安装epel源后,使用报错(Error: Cannot retrieve repository metadata (repomd.xml) for repository: epel. Please verify its path and try again)

    报错如下: Error: Cannot retrieve repository metadata (repomd.xml) for repository: epel. Please verify it ...

  9. php面试题2018

    一 .PHP基础部分 1.PHP语言的一大优势是跨平台,什么是跨平台? PHP的运行环境最优搭配为Apache+MySQL+PHP,此运行环境可以在不同操作系统(例如windows.Linux等)上配 ...

  10. QuickBI助你成为分析师-数据建模(二)

    摘要: 数据集编辑功能界面介绍以及常见问题总结. 在数据集编辑界面可以进行数据建模来更好的展示数据,创建数据集默认将数值类型字段作为度量,日期.字符串等类型作为维度,度量可以根据维度分组展示.下面来介 ...