关于二叉排序树 BST
#include<stdio.h>
#include<stdlib.h> typedef struct node
{
double w;
struct node *l,*r;
}*Node; void Build(Node &rt,double a)//建树
{
if(rt==NULL)
{
rt=new node;
rt->w=a;
rt->l=;
rt->r=;
}
else
{
if(a<rt->w)
Build(rt->l,a);
else
Build(rt->r,a);
}
}
double fin(struct node *rt) //找最小的权
{
if(!rt)
return ;
else if(!rt->l)
return rt->w;
else
return fin(rt->l);
} void delet(Node &rt,double w)//删除权为w点
{
if(!rt)
{
printf("没有\n");
return ;
}
else if(rt->w<w)
delet(rt->r,w);
else if(rt->w>w)
delet(rt->l,w);
else if(rt->l&&rt->r)
{
struct node *tmp;
tmp=rt;
tmp->w=fin(tmp->r);
delet(tmp->r,tmp->w);
}
else
{
struct node *t;
t=rt;
if(rt->l)
rt=rt->l;
else
rt=rt->r;
delete(t);
} }
void in(struct node *rt) //中序
{
if(rt)
{
in(rt->l);
printf("%lf ",rt->w);
in(rt->r);
}
else
return ;
}
double w;
int aim; void search(Node rt,int &cnt) //找第aim小
{
if(rt)
{
search(rt->l,cnt);
cnt++;
if(cnt==aim)
{
w=rt->w;
return;
}
search(rt->r,cnt); }
}
int main()
{
int n;
scanf("%d",&n);
struct node *root;
root=;
for(int i=;i<=n;i++) //插入
{
double a;
scanf("%lf",&a);
Build(root,a);
}
in(root);
printf("\n");
scanf("%d",&n);
for(int i=;i<=n;i++) //删除
{
double a;
scanf("%lf",&a);
delet(root,a);
in(root);
printf("\n");
}
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&aim); //第aim小
int cnt=;
search(root,cnt);
printf("%lf\n",w);
}
return ;
}
/*
9
3 4 2 5 7 6 3.5 3.1 3.6
0
3
6
7
3 */
关于二叉排序树 BST的更多相关文章
- 二叉排序树(BST)创建,删除,查找操作
binary search tree,中文翻译为二叉搜索树.二叉查找树或者二叉排序树.简称为BST 一:二叉搜索树的定义 他的定义与树的定义是类似的,也是一个递归的定义: 1.要么是一棵空树 2.如果 ...
- 二叉排序树(BST)构造与应用
二叉排序树(BST)构造与应用 本文取自<数据结构与算法>(C语言版)(第三版).出版社是清华大学出版社. 本博文作为学习资料整理. 源码是VC+ ...
- 【数据结构】简单谈一谈二分法和二叉排序树BST查找的比较
二分法查找: 『在有序数组的基础上通过折半方法不断缩小查找范围,直至命中或者查询失败.』 二分法的存储要求:要求顺序存储,以便于根据下标随机访问 二分法的时间效率:O(Log(n)) 二分 ...
- 二叉排序树BST代码(JAVA)
publicclassTest{ publicstaticvoid main(String[] args){ int[] r =newint[]{5,1,3,4,6,7 ...
- 二叉排序树BST+求树深度算法
#include "stdio.h" #include "malloc.h" typedef struct node { int key; struct nod ...
- 判断二叉树是否二叉排序树(BST)
算法思想:由于二叉排序树的中序遍历可以得到一个有序的序列,因此,我们可以使用中序遍历进行求解. 代码如下: #include <stack> using namespace std; ty ...
- 哈夫曼树;二叉树;二叉排序树(BST)
优先队列:priority_queue<Type, Container, Functional>Type 为数据类型, Container 为保存数据的容器,Functional 为元素比 ...
- 二叉排序树BST
注意:对一个二叉排序树进行中序遍历时,得到的序列是一个按值从小到大排列的有序序列 查找性能的分析:
- 4.5---判断是否是二叉排序树BST(CC150)
public boolean checkBST(TreeNode root) { return isBST(root, Long.MIN_VALUE, Long.MAX_VALUE); } publi ...
随机推荐
- 开启云时代,银狐H5游戏云通迅框架解决方案出炉!
没有时间开发服务器? 不懂服务器开发? 还在为WEB SOCKET烦恼?还在为网络卡,负载承受能力小烦恼? 银狐H5游戏云通迅框架,集成通讯SDK和开放API,1天即可完成 它也是开放平台,提供游戏需 ...
- Hoj2634 How to earn more?
How to earn more My Tags (Edit) Source : ww Time limit : 1 sec Memory limit : 64 M Submitted ...
- 包含文件函数include与require的区别
include或include_once一般用于动态包含,所谓动态包含就是根据不同条件包含不同文件 require或require_once一般用于静态包含,比如包含一个html文件的头部或者尾部 如 ...
- HTML 学习笔记 JavaScript (节点)
HTML 节点: HTML DOM定义了所有HTML元素的对象和属性 以及访问它们的方法. HTML DOM是关于如何获取 修改 添加 或 删除HTML元素的标准. 在js中通过document这个对 ...
- jquery工具方法proxy
proxy : 改变this指向 使用方法1:function show(){ alert(this); }$.proxy(show,document)(); //document 使用方法2:fu ...
- javascript中String的fromCharCode()方法
前几天遇到一个bug,后端的模板引擎在输出形如: <div title="111 aaa">内容</div> 这样的内容时,无法输出' '空格,所以只能用' ...
- toodifficult 题解
名字听起来十分厉害啊...一道lzz的提交答案题. 提答题,我们看看题目,给出一个解密程序,叫你加密. 每个点有一个加密的sample和一些要加密的文本. 从题目中我们可以得到一些信息: 加密后一般为 ...
- nginx访问白名单设置以及根据$remote_addr分发
在日常运维工作中,会碰到这样的需求:设置nginx的某个域名访问只对某些ip开放,其他ip的客户端都不能访问.达到这样的目的一般有下面两种设置方法:(1)针对nginx域名配置所启用的端口(一般是80 ...
- node基础06:回调函数
1.Node异步编程 Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了. 回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,No ...
- Qt学习笔记 QMessageBox
Qt的几种MessageBox 1.Infomation类型 QMessageBox::information(this,tr("hello"),tr("title&qu ...