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 = 0; 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 = 0; 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 = 0; i < N; i++ ) {
    scanf("%d", &X);
    BST = Delete(BST, X);
  }
  return 0;
}

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 );
  }}

原文地址:https://www.cnblogs.com/justLittleStar/p/10390508.html

时间: 2024-11-02 15:01:32

PTA-BinarySearchTree BasicOperation的相关文章

ERROR&lt;53761&gt; - 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-Directory/6.3 B2008.0311.0224 (32-bit) starting up[04/May/2016:21:10:39 +0800] - Listening on all interfaces port 11111 for LDAP requests[04/May/2016:21:10

PTA 5-8(English) File Transfer (25) - 并查集 - 数组实现

题目:http://pta.patest.cn/pta/test/16/exam/4/question/670 PTA - Data Structures and Algorithms (English) - 5-8 We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer t

数据结构(Java语言)——BinarySearchTree简单实现

二叉树的一个重要应用是它们在查找中的使用.使二叉树成为二叉查找树的性质是,对于树中的每个节点X,它的左子树中所有项的值都大于X中的项.注意,这意味着该树所有的元素都可以用某种一致的方式排序. 现在给出通常对二叉查找树进行的操作的简单描述.注意,由于树的递归定义,通常是递归地编写这些操作的例程.因为二叉查找树的平均深度是O(logN),所以一般不必担心栈空间耗尽. 二叉查找树要求所有的项都能够排序.要写出一个一般的类,我们需要提供一个接口来表示这个性质.这个接口就是Comparable,它告诉我们

PTA Huffman Codes

题目重现 In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redundancy Codes", and hence printed his name in the history of computer science. As a professor who gives the final exam problem on Huffman codes, I am

BinarySearchTree查找二叉树独立实现

先看看实现了哪些功能吧? (1)构造二叉树 (2)遍历二叉树结点 (3)搜索二叉树结点 (4)删除二叉树结点 (5)判断结点是否存在二叉树 看看源码: package hk.inso.service; /** * Created by IntelliJ IDEA. * Date: 8/17/15 11:45 PM * Author: Richard */ public class BinarySearchTree { /** * 根结点,是所有遍历的入口 */ private Node root

基本数据结构之BinarySearchTree

问题描述: BinarySearchTree 问题分析: 基本的实现 代码实现: package c04; /**  * @project: DataStructureAndAlgorithmAnalysis  * @filename: BinarySearchTree.java  * @version: 0.10  * @author: JM Han  * @date: 18:38 2015/10/19  * @comment: Test Purpose  * @result:  */ imp

PTA 10-排序6 Sort with Swap(0, i) (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/678 5-16 Sort with Swap(0, i)   (25分) Given any permutation of the numbers {0, 1, 2,..., N-1N?1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is

PTA 10-排序4 统计工龄 (20分)

题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/721 5-13 统计工龄   (20分) 给定公司NN名员工的工龄,要求按工龄增序输出每个工龄段有多少员工. 输入格式: 输入首先给出正整数NN(\le 10^5≤10?5??),即员工总人数:随后给出NN个整数,即每个员工的工龄,范围在[0, 50]. 输出格式: 按工龄的递增顺序输出每个工龄的员工个数,格式为:"工龄:人数".每项占一行.如果人数为0则不输出该项. 输入样

PTA 09-排序2 Insert or Merge (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 Insert or Merge   (25分) According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort rem

PTA 10-排序5 PAT Judge (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/677 5-15 PAT Judge   (25分) The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT. Input Spe