04-树7 二叉搜索树的操作集 (30分)

来源:互联网 发布:淘宝工作室是怎么赚钱 编辑:程序博客网 时间:2024/05/16 15:19

本题要求实现给定二叉搜索树的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;};
  • 函数InsertX插入二叉搜索树BST并返回结果树的根结点指针;
  • 函数DeleteX从二叉搜索树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;}/* 你的代码将被嵌在这里 */

输入样例:

105 8 6 2 4 1 0 10 9 756 3 10 0 555 7 0 10 3

输出样例:

Preorder: 5 2 1 0 4 8 6 7 10 96 is found3 is not found10 is found10 is the largest key0 is found0 is the smallest key5 is foundNot FoundInorder: 1 2 4 6 8 9

Position Find( BinTree BST, ElementType X ){//在BST中找到X,返回该结点的指针;找不到则返回空指针if(!BST){return NULL;}if(X==BST->Data){return BST;}if(X<BST->Data){return Find(BST->Left,X);//这里如果不写return,则返回值给不到最初的函数 }else if(X>BST->Data){return Find(BST->Right,X);//这里如果不写return,则返回值给不到最初的函数}//上面这个方法是递归的。下面这个方法是迭代,效率更高while(BST){if(X<BST->Data)BST=BST->Left;else if(X>BST->Data)BST=BST->Right;else /* X == BST->Data */return BST;/* 查找成功 , 返回结点的找到结点的地址*/} return NULL;/* 查找失败*/}Position FindMin( BinTree BST ){if(!BST)/* 空的二叉搜索树,返回NULL*/{return NULL;}else if(!BST->Left)/* 找到最左叶结点并返回*/{return BST;}else if(BST)/* 沿左分支继续查找*/{return FindMin(BST->Left);}//上面这个方法是递归的。下面这个方法是迭代,效率更高 //if(BST)//{//while(BST->Left)//不断地让BST指向其左孩子,直到没有左孩子为止 //{//BST=BST->Left;//}//}//return BST;}Position FindMax( BinTree BST ){if(!BST){return NULL;}else if(!BST->Right){return BST;}else if(BST){return FindMax(BST->Right);}//上面这个方法是递归的。下面这个方法是非递归,效率更高 //if(BST)//{//while(BST->Right)//不断地让BST指向其左孩子,直到没有左孩子为止 //{//BST=BST->Right;//}//}//return BST;}BinTree Insert( BinTree BST, ElementType X ){//将X插入二叉搜索树BST并返回结果树的根结点指针;if(!BST)/* 若原树为空 , 生成并返回一个结点的二叉搜索树*/{BST=(BinTree)malloc(sizeof(struct TNode));BST->Data=X;BST->Left=NULL;BST->Right=NULL;}else{/* 开始找要插入元素的位置*/if(X<BST->Data){BST->Left=Insert(BST->Left,X);}else if(X>BST->Data){BST->Right=Insert(BST->Right,X); }}return BST;} BinTree Delete( BinTree BST, ElementType X ){//将X从BST中删除,并返回根结点指针;如果X不在树中,则打印一行Not Found并返回原树的根结点指针;BinTree temp;if(!BST){printf("Not Found\n");return BST;}if(X<BST->Data){BST->Left=Delete(BST->Left,X);}else if(X>BST->Data){BST->Right=Delete(BST->Right,X); }else{if(BST->Left&&BST->Right)/* 被删除结点有左右两个子结点 */{temp=FindMin(BST->Right);//要么找右子树最小的,要么左子树最大的 BST->Data=temp->Data;BST->Right=Delete(BST->Right,BST->Data);}else/* 被删除结点有一个或无子结点*/{temp=BST;if(!BST->Left)/*  有右孩子或无子结点*/{BST=BST->Right;}else if(!BST->Right)/* 有左孩子或无子结点*/{BST=BST->Left;}free(temp);}}return BST;}



0 0
原创粉丝点击