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

来源:互联网 发布:embed js获取播放时间 编辑:程序博客网 时间:2024/06/05 04:16
  • 题目
    04-树7 二叉搜索树的操作集 (30分)
  • 分析
    就是考察基本的二叉搜索树的操作,基本功,需要注意的是删除操作的一些细节处理。
  • 我的代码
#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;    //freopen("treeTest.txt","r",stdin);    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;}void PreorderTraversal( BinTree BST ){    if(BST == NULL) return;    printf(" %d ",BST->Data);    PreorderTraversal(BST->Left);       PreorderTraversal(BST->Right);}void InorderTraversal( BinTree BST ){    if(BST == NULL) return;    InorderTraversal(BST->Left);    printf(" %d ",BST->Data);    InorderTraversal(BST->Right);}/* 你的代码将被嵌在这里 */BinTree Insert( BinTree BST, ElementType X ){       if(BST == NULL){        BinTree tmp = (BinTree)malloc(sizeof(struct TNode));        tmp->Data = X;        tmp->Left = NULL;        tmp->Right = NULL;        BST = tmp;    }else if(BST->Data > X){        BST->Left = Insert(BST->Left, X);    }else{        BST->Right = Insert(BST->Right, X);    }    return BST;}//删除结点的代码是难点,需要重点掌握 BinTree Delete( BinTree BST, ElementType X ){    BinTree tmp;    if(BST == NULL){        printf("Not Found\n");        //return NULL;    }else if(BST->Data > X){        BST->Left = Delete(BST->Left, X);    }else if(BST->Data < X){        BST->Right = Delete(BST->Right, X);    }else{  //找到了         if(BST->Left && BST->Right){    //被删除的结点有两个子结点             tmp = FindMin(BST->Right);  //找到右子树中的最小值来替换            BST->Data = tmp->Data;            BST->Right = Delete(BST->Right, tmp->Data);         }else{          //被删除的结点有一个或0个子结点             tmp = BST;            if( !BST->Left ){                BST = BST->Right;                               } else if(!BST->Right){                BST = BST->Left;                }            free(tmp);//一定要记得free,否则造成内存泄漏!         }    }    return BST;}Position Find( BinTree BST, ElementType X ){    if(BST == NULL) return NULL;    if(BST->Data == X){        return BST;    }else if(BST->Data > X){        return Find(BST->Left, X);    }else{        return Find(BST->Right, X);    }}Position FindMin( BinTree BST ){    if(BST == NULL) return NULL;    while(BST->Left)    BST = BST->Left;    return BST;}Position FindMax( BinTree BST ){    if(BST == NULL) return NULL;    while(BST->Right)   BST = BST->Right;    return BST;}
0 0