4-12 二叉搜索树的操作集
来源:互联网 发布:date php 格式化 编辑:程序博客网 时间:2024/06/06 01:49
4-12 二叉搜索树的操作集 (30分)
本题要求实现给定二叉搜索树的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;}/* 你的代码将被嵌在这里 */
输入样例
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
点击访问 PTA-测验
/* 你的代码将被嵌在这里 */BinTree Insert( BinTree BST, ElementType X ) { //二分生长树 BinTree NewT=(BinTree)malloc(sizeof(struct TNode)); NewT->Data=X; NewT->Left=NULL; NewT->Right=NULL; BinTree head=BST; while(BST) { if(BST->Data>=X) { if(BST->Left)BST=BST->Left; else { BST->Left=NewT; return head; } } else { if(BST->Right)BST=BST->Right; else { BST->Right=NewT; return head; } } } return NewT;//若为空树 则新建}BinTree Delete( BinTree BST, ElementType X ) { BinTree head=BST; BinTree t=Find(BST,X); if(!t) { printf("Not Found\n"); return BST; } BinTree m=FindMax(t->Left); if(m) {//左子树处理 t->Data=m->Data; if(t->Left==m)t->Left=m->Left; else { t=t->Left; while(t->Right!=m)t=t->Right; t->Right=m->Left; } } else { m=FindMin(t->Right); if(m) {//左子树为空时 t->Data=m->Data; if(t->Right==m)t->Right=m->Right; else { t=t->Right; while(t->Left!=m)t=t->Left; t->Left=m->Right; } } else { //当其为叶节点时 if(BST==t)return NULL; while(BST) { if(BST->Data>=t->Data) { if(BST->Left==t)BST->Left=NULL; BST=BST->Left; } else { if(BST->Right==t)BST->Right=NULL; BST=BST->Right; } } } } return head;}Position Find( BinTree BST, ElementType X ) { BinTree h=BST; while(h) { if(h->Data==X)break; else if(h->Data<X)h=h->Right; else h=h->Left; } return h;}Position FindMin( BinTree BST ) { if(!BST)return NULL; BinTree h=BST; while(h->Left)h=h->Left; return h;}Position FindMax( BinTree BST ) { if(!BST)return NULL; BinTree h=BST; while(h->Right)h=h->Right; return h;}
0 0
- 4-12 二叉搜索树的操作集 (30分)
- 4-12 二叉搜索树的操作集
- 4-12 二叉搜索树的操作集 (30分)
- 4-12 二叉搜索树的操作集*
- 二叉搜索树的操作集
- 二叉搜索树的操作集
- 二叉搜索树的操作集
- 二叉搜索树的操作题集
- 二叉搜索树的操作集
- 二叉搜索树的操作集
- 二叉搜索树的操作集
- 二叉搜索树的操作集
- 搜索二叉树的操作
- 二叉搜索树的操作
- 二叉搜索树的操作
- 二叉搜索树的操作
- 二叉搜索树操作集
- 【二叉搜索树】二叉搜索树的基本操作
- Gradle安装与简单使用
- 继承、封装
- Enable ADB integration' to be enabled.
- Struts2的深入理解与使用(一)
- 机器学习(周志华)习题解答1.4: 两个算法到底孰优孰劣?没有免费午餐定理
- 4-12 二叉搜索树的操作集
- JavaScript趣题:成双成对的括弧
- 背景属性、文本属性、表格属性、边框属性
- 4-6 带头结点的链式表操作集
- 4-7 在一个数组中实现两个堆栈 (20分)
- 程序猿之路--Android基础
- 4-8 求二叉树高度 (20分)
- 数值字节与范围
- 4-9 二叉树的遍历 (25分)