当前位置:网站首页>二叉搜索树中求得给定元素的下界
二叉搜索树中求得给定元素的下界
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;
}
边栏推荐
猜你喜欢
随机推荐
ZERO Technology "Chain on the South"——deeply cultivated in the field of digital finance
Conformer papers and code parsing (below)
Chrome Proxy Manager Plugin
微信小程序小说云开发免费源码
小程序模拟淘宝京东商品轮播滑动展示功能模块
MATLAB综合实例:部门工资统计图分析
21天打卡挑战学习MySQL——《SQL基础入门》第二周 第四篇
Conditional - DETR papers parsing
超外差半导体收音机:各个元器件的作用,如何进行调试,以及工作原理
分布式文件存储——分块上传和断点续传
Idea修改全部变量名
01背包问题,简易AC代码加详细讲解,地宫寻宝,波动数列等DP问题。
又来了!今天再分享几个Jupyter Notebook的使用技巧!
ES6新特性未命名参数
Contextual Transformer Networks for Visual Recognition paper and code analysis
Redis之HyperLogLog
一、Canvas应用的背景(个人理解)及基础语法
Excel摸鱼技巧:快速实现分列转到行
leetcode 217存在重复元素
力扣每日一题----求第n位斐波那契数