(二叉搜索树6-1)二叉搜索树的操作集--C语言

本题要求实现给定二叉搜索树的5种常用操作。

函数接口定义:

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;
};
  • 函数Insert将X插入二叉搜索树BST并返回结果树的根结点指针;
  • 函数Delete将X从二叉搜索树BST中删除,并返回结果树的根结点指针;如果X不在树中,则打印一行Not Found并返回原树的根结点指针;
  • 函数Find在二叉搜索树BST中找到X,返回该结点的指针;如果找不到则返回空指针;
  • 函数FindMin返回二叉搜索树BST中最小元结点的指针;
  • 函数FindMax返回二叉搜索树BST中最大元结点的指针。

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

void PreorderTraversal( BinTree BT ); /* 先序遍历,由裁判实现,细节不表 */
void InorderTraversal( BinTree BT );  /* 中序遍历,由裁判实现,细节不表 */

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;

    BST = NULL;
    scanf("%d", &N);
    for ( i=0; i<N; i++ ) {
        scanf("%d", &X);
        BST = Insert(BST, X);
    }
    printf("Preorder:"); PreorderTraversal(BST); printf("\n");
    MinP = FindMin(BST);
    MaxP = FindMax(BST);
    scanf("%d", &N);
    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->Data);
            if (Tmp==MinP) printf("%d is the smallest key\n", Tmp->Data);
            if (Tmp==MaxP) printf("%d is the largest key\n", Tmp->Data);
        }
    }
    scanf("%d", &N);
    for( i=0; i<N; i++ ) {
        scanf("%d", &X);
        BST = Delete(BST, X);
    }
    printf("Inorder:"); InorderTraversal(BST); printf("\n");

    return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:

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

代码

BinTree Insert( BinTree BST, ElementType X )
{
    if(!BST)//原树为空,插入一个新节点
    {
        BST=(BinTree)malloc(sizeof(struct TNode));
        BST->Data=X;
        BST->Left=BST->Right=NULL;
    }
    else//原树不为空,根据根结点的大小判断插入方向
    {
        if(X>BST->Data)//X大于根结点,往右子树方向插入
            BST->Right=Insert(BST->Right,X);
        else if(X<BST->Data)//X小于根结点,往左子树方向插入
            BST->Left=Insert(BST->Left,X);
    }
    return BST;
}
/*
    1.判断是否存在结点,不存在直接打印Not Found;
    2.存在,判断X与BST->Data的大小。大于,往右子树方向;小于,往左子树方向;
    3.等于,判断该结点有几个孩子。两个孩子,用FindMin函数找BST右子树的最小元结点,用最小元结点代替BST,然后删除最小元结点;
    只有一个孩子或没有孩子,判断是否有右孩子。有右孩子,直接用右孩子代替被删除结点,然后删除右孩子;
    没有则用左孩子代替被删除结点,然后删除左孩子。
*/
BinTree Delete( BinTree BST, ElementType X )
{
    BinTree tmp;
    if(!BST)
        printf("Not Found\n");
    else
    {
        if(X>BST->Data)//大于,右子树
            BST->Right=Delete(BST->Right,X);
        else if(X<BST->Data)//小于,左子树
            BST->Left=Delete(BST->Left,X);
        else//等于,找到结点
        {
            if(BST->Left&&BST->Right)//左右子树都存在
            {
                tmp=FindMin(BST->Right);//找到右子树的最小元结点
                BST->Data=tmp->Data;//右子树的最小值代替被删除节点
                BST->Right=Delete(BST->Right,tmp->Data);//删除右子树的最小值
            }
            else
            {
                tmp=BST;
                if(!BST->Right)//有左节点或无节点
                    BST=BST->Left;//左结点代替被删除结点,因为没有右子树,因此左子树为最大
                else//有右子树
                    BST=BST->Right;//右结点代替被删除结点
                free(tmp);
            }
        }
    }
    return BST;
}
Position Find( BinTree BST, ElementType X )
{
    while(BST)
    {
        if(X>BST->Data)//大于,在右子树上
            BST=BST->Right;
        else if(X<BST->Data)//小于,在左子树上
            BST=BST->Left;
        else//等于,返回找到的结点
            return BST;
    }
    return NULL;
}
Position FindMin( BinTree BST )//最小元结点在左子树的最后一个结点
{
    if(BST)
    {
        while(BST->Left)
            BST=BST->Left;
    }
    return BST;
}
Position FindMax( BinTree BST )//最大元结点在右子树的最后一个结点
{
    if(BST)
    {
        while(BST->Right)
            BST=BST->Right;
    }
    return BST;
}
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值