浙大数据结构MOOC作业题目集2020——04-树7 二叉搜索树的操作集 (30分)

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

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

#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);
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
10 is found
10 is the largest key
0 is found
0 is the smallest key
5 is 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=NULL;
BST->Right=NULL;
}
else if(X>BST->Data) BST->Right=Insert(BST->Right,X);
else if(X<BST->Data) BST->Left=Insert(BST->Left,X);
return BST;
}
BinTree Delete( BinTree BST, ElementType X ){
BinTree Tmp;
if(!BST){
}
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,BST->Data);
}
else{//只存在左子树或右子树
Tmp=BST;
if(!BST->Left) BST=BST->Right;//左子树为空
else if(!BST->Right) BST=BST->Left;//右子树为空
free(Tmp);
}
}
return BST;
}
Position Find( BinTree BST, ElementType X ){
if(!BST) return NULL;
else if(X>BST->Data) return Find(BST->Right,X);
else if(X<BST->Data) return Find(BST->Left,X);
return BST;
}
Position FindMin( BinTree BST ){
if(BST!=NULL){
while(BST->Left)
BST=FindMin(BST->Left);
}
return BST;
}
Position FindMax( BinTree BST ){
if(!BST) return NULL;
else if(!BST->Right) return BST;
else return FindMax(BST->Right);
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

最烦装逼的人

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
03-20 9375

10-18 1940
03-25 1557
09-22