04-树7 二叉搜索树的操作集(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=malloc(sizeof(struct TNode));BST->Data=X;BST->Left=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 ){Position Tmp;if(!BST) printf("Not Found"); //树为空 找不到else if(X<BST->Data)BST->Left=Delete(BST->Left,X); //左子树递归删除else if(X>BST->Data)BST->Right=Delete(BST->Right,X); //右子树递归删除else //找到要删除的结点if(BST->Right && BST->Left){ //被删除结点有左右两个子结点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 ){while(BST){if(X>BST->Data)BST=BST->Right;else if(X<BST->Data)BST=BST->Left;elsereturn 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;
}
提交结果
04-树7 二叉搜索树的操作集(c语言实现)相关推荐
- 算法 树7 二叉搜索树的操作集
全部每周作业和视频思考题答案和解析 见 浙江大学 数据结构 思考题+每周练习答案汇总 题目:本题要求实现给定二叉搜索树的5种常用操作. 函数接口定义: BinTree Insert( BinTree ...
- 6-12 二叉搜索树的操作集
6-12 二叉搜索树的操作集(30 分) 本题要求实现给定二叉搜索树的5种常用操作. 函数接口定义: BinTree Insert( BinTree BST, ElementType X ); Bin ...
- 6-1 二叉搜索树的操作集 (30 分)
大一下半期数据结构 二叉搜索树的操作集 本题要求实现给定二叉搜索树的5种常用操作. 函数接口定义: BinTree Insert( BinTree BST, ElementType X ); BinT ...
- 04-树7 二叉搜索树的操作集 (30 分)
本题要求实现给定二叉搜索树的5种常用操作. 函数接口定义: BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree ...
- 6-12 二叉搜索树的操作集 (30 分)
本题要求实现给定二叉搜索树的5种常用操作. 函数接口定义: BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree ...
- 数据结构 - 从二叉搜索树说到AVL树(一)之二叉搜索树的操作与详解(Java)
二叉搜索树(Binary Search Tree),简称BST,顾名思义,一颗可以用于搜索的二叉树.BST在数据结构中占有很重要的地位,一些高级树结构都是其的变种,例如AVL树.红黑树等,因此理解BS ...
- (一)BST树(二叉搜索树)
(一)BST树(二叉搜索树) 1.BST二叉搜索树 1.1BST树的定义 1 BST树也是一个二叉树,故满足递归定义; 2 其次每个节点只存在一个值; 3 需满足左子树值<=根值<=右子树 ...
- 数据结构(三):非线性逻辑结构-特殊的二叉树结构:堆、哈夫曼树、二叉搜索树、平衡二叉搜索树、红黑树、线索二叉树
在上一篇数据结构的博文<数据结构(三):非线性逻辑结构-二叉树>中已经对二叉树的概念.遍历等基本的概念和操作进行了介绍.本篇博文主要介绍几个特殊的二叉树,堆.哈夫曼树.二叉搜索树.平衡二叉 ...
- 树形结构:二叉排列树,二叉搜索树
二叉排列树,二叉搜索树 这个数听见得比较多,含义是:左边<=中间<=右边,换句话来说使用中序遍历最后得到结果就是一个有序得序列. 建立一颗二叉排列树 class BinTNode:def ...
最新文章
- 运维和开发慌了,Redis突然 慢 了,到底谁背锅?
- nginx 在负载均衡中 的配置 以获取真实IP
- 查看linux服务器硬件信息
- mysql启动触发器_MYSQL中禁用/启动触发器
- springboot static访问不到_Spring Boot 的静态资源处理
- 你必须要知道的数据备份工具rsync
- 数据结构和算法——八种常用的排序算法----选择排序
- 机器视觉的四大类应用
- 人月神话札记:未雨绸缪
- IOS 检测摇一摇动作的实现方式
- Geforce Experience无法登录的解决方法
- 剁馅机器人图片_河南大妈开店十年卖饺子, 剁馅用上机器人, 莲菜大肉馅还真好吃...
- Android开发最新所有框架总结排行榜
- 统计量 估计量 概念
- Node.js之npm ERR code EPERM npm ERR syscall open npm ERR
- Android Framework 记录之一
- 更易用的OceanBase|生态工具征文大赛正式开启!
- 计算机任务计划程序已损坏,win10创建任务提示“该任务映像已损坏或已篡改”的解决方法...
- 计算机三级网络技术资料分享
- 2021-2027全球与中国工业锂电池市场现状及未来发展趋势