• 【PTA】6-12 二叉搜索树的操作集 (30分)


    【PTA】6-3 求链式表的表长 (10分)

    函数接口定义:

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

    其中BinTree结构定义如下:

    typedef struct TNode *Position;
    typedef Position BinTree;
    struct TNode{
        ElementType Data;
        BinTree Left;
        BinTree Right;
    };
    • 函数InsertX插入二叉搜索树BST并返回结果树的根结点指针;
    • 函数DeleteX从二叉搜索树BST中删除,并返回结果树的根结点指针;如果X不在树中,则打印一行Not Found并返回原树的根结点指针;
    • 函数Find在二叉搜索树BST中找到X,返回该结点的指针;如果找不到则返回空指针;
    • 函数FindMin返回二叉搜索树BST中最小元结点的指针;
    • 函数FindMax返回二叉搜索树BST中最大元结点的指针。

    裁判测试程序样例:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 typedef int ElementType;
     5 typedef struct TNode *Position;
     6 typedef Position BinTree;
     7 struct TNode{
     8     ElementType Data;
     9     BinTree Left;
    10     BinTree Right;
    11 };
    12 
    13 void PreorderTraversal( BinTree BT ); /* 先序遍历,由裁判实现,细节不表 */
    14 void InorderTraversal( BinTree BT );  /* 中序遍历,由裁判实现,细节不表 */
    15 
    16 BinTree Insert( BinTree BST, ElementType X );
    17 BinTree Delete( BinTree BST, ElementType X );
    18 Position Find( BinTree BST, ElementType X );
    19 Position FindMin( BinTree BST );
    20 Position FindMax( BinTree BST );
    21 
    22 int main()
    23 {
    24     BinTree BST, MinP, MaxP, Tmp;
    25     ElementType X;
    26     int N, i;
    27 
    28     BST = NULL;
    29     scanf("%d", &N);
    30     for ( i=0; i<N; i++ ) {
    31         scanf("%d", &X);
    32         BST = Insert(BST, X);
    33     }
    34     printf("Preorder:"); PreorderTraversal(BST); printf("
    ");
    35     MinP = FindMin(BST);
    36     MaxP = FindMax(BST);
    37     scanf("%d", &N);
    38     for( i=0; i<N; i++ ) {
    39         scanf("%d", &X);
    40         Tmp = Find(BST, X);
    41         if (Tmp == NULL) printf("%d is not found
    ", X);
    42         else {
    43             printf("%d is found
    ", Tmp->Data);
    44             if (Tmp==MinP) printf("%d is the smallest key
    ", Tmp->Data);
    45             if (Tmp==MaxP) printf("%d is the largest key
    ", Tmp->Data);
    46         }
    47     }
    48     scanf("%d", &N);
    49     for( i=0; i<N; i++ ) {
    50         scanf("%d", &X);
    51         BST = Delete(BST, X);
    52     }
    53     printf("Inorder:"); InorderTraversal(BST); printf("
    ");
    54 
    55     return 0;
    56 }
    57 /* 你的代码将被嵌在这里 */

    输入样例:

    10
    5 8 6 2 4 1 0 10 9 7
    5
    6 3 10 0 5
    5
    5 7 0 10 3

    输出样例:

    Preorder: 5 2 1 0 4 8 6 7 10 9
    6 is found
    3 is not found
    10 is found
    10 is the largest key
    0 is found
    0 is the smallest key
    5 is found
    Not Found
    Inorder: 1 2 4 6 8 9

    函数实现细节:

      1 BinTree Insert( BinTree BST, ElementType X ){
      2     if(BST==NULL){
      3         BinTree temp=(BinTree)malloc(sizeof(struct TNode));
      4         temp->Data=X,temp->Left=temp->Right=NULL;
      5         return temp;
      6     }else{
      7         BinTree BT=BST,Pre;
      8         BinTree temp=(BinTree)malloc(sizeof(struct TNode));
      9         temp->Data=X,temp->Left=temp->Right=NULL;
     10         while(BT){
     11             if(X>BT->Data){
     12                 Pre=BT;
     13                 BT=BT->Right;
     14             }else{
     15                 Pre=BT;
     16                 BT=BT->Left;
     17             }
     18         }
     19         if(X>Pre->Data){
     20             Pre->Right=temp;
     21         }else{
     22             Pre->Left=temp;
     23         }
     24         return BST;
     25     }
     26     return NULL;
     27 }
     28  
     29 BinTree Delete( BinTree BST, ElementType X ){
     30     BinTree Pre=BST,LMax,RMin,BT=NULL,head=BST;
     31     if(head){
     32         while(head&&head->Data!=X){
     33             if(X>head->Data){
     34                 Pre=head;
     35                 head=head->Right;
     36             }else{
     37                 Pre=head;
     38                 head=head->Left;
     39             }
     40         }
     41         if(head&&head->Data==X)BT=head;
     42         if(BT){
     43             LMax=BT->Left;RMin=BT->Right;
     44             if(LMax){
     45                 Pre=BT;
     46                 while(LMax->Right){
     47                     Pre=LMax;
     48                     LMax=LMax->Right;
     49                 }
     50                 BT->Data=LMax->Data;
     51                 if(LMax->Left==NULL){
     52                     if(Pre->Left==LMax){
     53                         Pre->Left=NULL;free(LMax);
     54                     }else{
     55                         Pre->Right=NULL;free(LMax);
     56                     }
     57                     return BST;
     58                 }else{
     59                     if(Pre->Left==LMax){
     60                         Pre->Left=LMax->Left;
     61                         free(LMax);
     62                     }else{
     63                         Pre->Right=LMax->Left;
     64                         free(LMax);
     65                     }
     66                     return BST;
     67                 }
     68             }
     69             else if(RMin){
     70                 Pre=BT;
     71                 while(RMin->Left){
     72                     Pre=RMin;
     73                     RMin=RMin->Left;
     74                 }
     75                 BT->Data=RMin->Data;
     76                 if(RMin->Right==NULL){
     77                     if(Pre->Left==LMax){
     78                         Pre->Left=NULL;free(LMax);
     79                     }else{
     80                         Pre->Right=NULL;free(LMax);
     81                     }
     82                     return BST;
     83                 }else{
     84                     if(Pre->Right==RMin){
     85                         Pre->Right=RMin->Right;
     86                         free(RMin);
     87                     }else{
     88                         Pre->Right=RMin->Left;
     89                         free(RMin);
     90                     }
     91                     return BST;
     92                 }
     93             }
     94             else{
     95                 if(Pre->Right==BT){
     96                     Pre->Right=NULL;free(BT);
     97                     return BST;
     98                 }else if(Pre->Left==BT){
     99                     Pre->Left=NULL;free(BT);
    100                     return BST;
    101                 }else if(Pre==BST){
    102                     free(BST);
    103                     return NULL;    
    104                 }
    105                 
    106             }
    107         }else{
    108             printf("Not Found
    ");
    109             return BST;
    110         }
    111     }else{
    112             printf("Not Found
    ");
    113             return BST;
    114         }
    115 }
    116 
    117 Position Find( BinTree BST, ElementType X ){
    118     if(BST){
    119         while(BST&&BST->Data!=X){
    120             if(X>BST->Data){
    121                 BST=BST->Right;
    122             }else{
    123                 BST=BST->Left;
    124             }
    125         }
    126         if(BST&&BST->Data==X)return BST;
    127         return NULL; 
    128     }
    129     return NULL;
    130 }
    131  
    132 Position FindMin( BinTree BST ){
    133     if(BST){
    134         while(BST->Left){
    135             BST=BST->Left;
    136         }
    137         return BST;
    138     }
    139     return NULL;
    140 }
    141 
    142 Position FindMax( BinTree BST ){
    143     if(BST){
    144         while(BST->Right){
    145             BST=BST->Right;
    146         }
    147         return BST;
    148     }
    149     return NULL;
    150 }
  • 相关阅读:
    Monkeyrunner环境搭建
    学习Monkeyrunner过程
    uiautomatorviewer使用报错
    安装JMeter
    如何测试网页的访问速度
    安装Android studio
    软件测试工程师具备技能
    WinRAR去除广告弹框(精华在末尾)
    android studio adb连接不上手机
    DOM的理解
  • 原文地址:https://www.cnblogs.com/wyjgr/p/13073661.html
Copyright © 2020-2023  润新知