swust oj 1011
二叉排序树的实现和查找
输入
关键字个数n;
关键字集合;
要查找的关键字;
输出
查找成功输出比较的次数,否则输出-1。
样例输入
12
25 18 46 2 53 39 32 4 74 67 60 11
74
样例输出
4
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cstdio>
typedef int Datetype;
using namespace std;
int x; typedef struct link{
Datetype date;
struct link *lchild;
struct link *rchild;
}tree; void creat(tree *&L, int arr[] ,int l,int r)
{
if(l>r)
{
L=NULL;
return ;
}
int s=(l+r)>>;
L=new tree;
L->date=arr[s];
creat(L->lchild,arr,l,s-);
creat(L->rchild,arr,s+,r);
} void display(tree *&L)
{
if(L!=NULL)
{
cout<<L->date<<" ";
display(L->lchild);
display(L->rchild);
}
} void delet(tree *&L)
{
if(L!=NULL)
{
delet(L->lchild);
delet(L->rchild);
delete(L);
}
} void find(tree *&L, int a)
{
if(L!=NULL)
{
x++;
if(a>L->date)
find(L->rchild,a);
else if(a<L->date)
find(L->lchild,a);
else
return ;
}
if(L==NULL)
x=;
} int main()
{
int arr[];
int n,a;
tree *L;
cin>>n;
for(int i=;i<n;i++)
{
cin>>arr[i];
}
sort(arr,arr+n);
cin>>a;
creat(L,arr,,n-);
// display(L);
find(L,a);
if(x)
cout<<x;
else
cout<<"-1";
delet(L);
return ;
}
swust oj 1011的更多相关文章
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- [Swust OJ 1023]--Escape(带点其他状态的BFS)
解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535 Descript ...
- [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)
题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)
题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...
- [Swust OJ 1026]--Egg pain's hzf
题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf ...
- [九度OJ]1011.最大连续子序列
原题链接:http://ac.jobdu.com/problem.php?pid=1011 题目描述: 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ N ...
- [Swust OJ 1139]--Coin-row problem
题目链接: http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...
随机推荐
- 阻塞IO,非阻塞IO,IO多路复用模型
#服务端 import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen() while True: conn, ad ...
- Python2.7与3.6的一些区别
2.7实现了一部分3的功能, 更早版本可能会稍稍涉及一点 首先是关键字的差别 python3.6 import keyword print(keyword.kwlist) ['False', 'Non ...
- noip2018游(AFO)记
Day 0 到学车了,已经差不多四点了,领完一小袋比赛要用的就匆匆回了宾馆. 话说之前看地图的时候我们的宾馆最远,而且名字听起来并没有怎么高大上, 一看隔壁度豪大酒店就感觉应该比我们的酒店好.然鹅到了 ...
- 【转】iPython入门技巧
[转]http://www.cnblogs.com/cuiyubo/p/6823478.html 学习<利用python进行数据分析> 第三章 IPython:一种交互式计算和开发环境的笔 ...
- Navicat之MySQL连接(二)
1.下载Navicat软件包及相应的破解补丁: 2.双击navicat120_premium_cs_x64.exe开始安装,修改安装位置,一般选择安装在非系统盘! 注意:安装完成后,解压破解补丁文件拷 ...
- hadoop mkdir: Cannot create directory /usr. Name node is in safe mode.
今天在hdfs上面创建文件夹的时候报了:org.apache.hadoop.dfs.SafeModeException: Cannot delete /user/hadoop/input. Name ...
- MVC设计模式的简单理解
MVC介绍 众所周知MVC不是设计模式,是一个比设计模式更大一点的模式,称作设计模式不合理,应该说MVC它是一种软件开发架构模式,它包含了很多的设计模式,最为密切是以下三种:Observer (观察者 ...
- postgre 常用语法,如 group_concat用法
1.查询postgre的表所有字段列 select table_name, column_name from information_schema.columns where table_schema ...
- JFinal Druid 配置
/** * 数据库密码加密,执行如下命令,生成加密密码 * java -cp druid-1.1.14.jar com.alibaba.druid.filter.config.ConfigTools ...
- js中ASCII码和字符互相转换的方法
目录 十进制ASCII码转换成字符 字符转换成十进制ASCII码 十进制ASCII码转换成字符 使用String.fromCodePoint(num1[, ...[, numN]])方法 String ...