当前位置:网站首页>二叉搜索树中求得给定元素的下界
二叉搜索树中求得给定元素的下界
2022-08-08 21:22:00 【lhf2112】
public static BianrySearchTreeNode FloorInBST(BianrySearchTreeNode root,int data){
if(root==null)
return null;
if(root.getData()==data)
return root;
if(data<root.getData()){ //查询值小于当前节点的值
if(data<FindMin(root).getData())//小于整个树的最小值
return null;
if(data>=FindMax(root.getLeft()).getData())
return FindMax(root.getLeft());
else
return FloorInBST(root.getLeft(),data);
}
if(root.getData()<data){ //查询值大于当前节点的值
if(data>=FindMax(root.getRight()).getData())// 大于整棵树的最大值
return FindMax(root);
if(data<FindMin(root.getRight()).getData())//小于右子树的最小值
return root;
else
return FloorInBST(root.getRight(),data);
}
return null;
}
顺便记录一下FindMax与FindMin的函数如下:
//搜索最小元素
public static BianrySearchTreeNode FindMin(BianrySearchTreeNode root){
if(root==null)
return null;
while(root.getLeft()!=null)
root = root.getLeft();
return root;
}
//搜索最大元素
public static BianrySearchTreeNode FindMax(BianrySearchTreeNode root){
if(root==null)
return null;
while(root.getRight()!=null)
root = root.getRight();
return root;
}
边栏推荐
猜你喜欢
随机推荐
爬虫系列:读取文档
"New Infrastructure of Cultural Digital Strategy and Ecological Construction of Cultural Art Chain" was successfully held
数据告诉你:中国足球还有理论性出线的可能吗?
Move your office environment anywhere with a solid state USB drive
玩转C#网页抓取
音视频技术开发周刊 | 257
Contextual Transformer Networks for Visual Recognition paper and code analysis
How do I use cURL with a proxy?
ORACLE数据库常用操作
全国基础地理数据库数据预处理
分别用BeautifulSoup和scrapy爬取某一城市天气预报
零数科技深耕苏州,受邀参加中国金融科技产业峰会
CrossFormer:A Versatile Vision Transformer Based on Cross-Scale Transformer论文以及代码解析
如何判断一个 IP 是爬虫
课设系列:51单片机制作智能时钟闹钟
selenium基本使用
Conformer papers and code parsing (below)
分布式文件存储——文件秒传
世界经济和金融秩序再定义 | 零数科技受邀出席第三届世界金融论坛
金山WPS支持xlookup了?亲自上手实战好不好用。