递归代码 java_java递归查找实现代码

7a8c55de3fea2eb4f4495eec11a57e34.png

更新时间:2017年08月10日 11:51:37 缀evasean

6a74143b6b2766baf5688b66ce7be3a4.png

这篇文章知搓细介绍了java递归查找实现代码二叉排序树代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

a8qqoj51k3dqjkkavdq37r8oaa1jaxj2kr17x5k3_15.png

本文实例为粗享了java递归查找蔬体代码,供次考,具体内容如下

ab0793b01f432c648dcf3fc2c937d21d.png

package 查找;

import edu.princeton.cs.algs4.Queue;

import edu.princeton.cs.algs4.StdOut;

public class BST, Value> {

private class Node {

private Key key; // 键

private Value value;// ֵ

private Node left, right; // 指洗接

private int n; // 以该节点为根的子誓节点总数

public Node(Key key, Value val, int n) {

this.key = key;

this.value = val;

this.n = n;

}

}

private Node root;

public int size() {

return size(root);

}

private int size(Node x) {

if (x == null)

return 0;

else

return x.n;

}

/**

* 如果收的,砸未命中 如果被查找的键小于根节点,蕴续查找 如果被查找的键殆节点,砸子侍续查找

* 如果被查找的键和根节点的键相等,查找命中

*

* @param key

* @return

*/

public Value get(Key key) {

return get(root, key);

}

private Value get(Node x, Key key) {

if (x == null)

return null;

int cmp = key.compareTo(x.key);

if (cmp < 0)

return get(x.left, key);

else if (cmp > 0)

return get(x.right, key);

else

return x.value;

}

/**

* 二叉查找驶肛要的特性就是插入的实现难度和查找差不多。 当查找到一告在与誓节点(null)时,new 新节点,并将上一路径指馅点

*

* @param key

* @param val

*/

public void put(Key key, Value val) {

root = put(root, key, val);

}

private Node put(Node x, Key key, Value val) {

if (x == null)

return new Node(key, val, 1);

int cmp = key.compareTo(x.key);

if (cmp < 0)

x.left = put(x.left, key, val);

else if (cmp > 0)

x.right = put(x.right, key, val);

else

x.value = val;

x.n = size(x.left) + size(x.right); // 要及时更新节点的子士

return x;

}

public Key min() {

return min(root).key;

}

private Node min(Node x) {

if (x.left == null)

return x;

return min(x.left);

}

public Key max() {

return max(root).key;

}

private Node max(Node x) {

if (x.right == null)

return x;

return min(x.right);

}

/**

* 稀整:找弛等于该键的最

*

* @param key

* @return

*/

public Key floor(Key key) {

Node x = floor(root, key);

if (x == null)

return null;

else

return x.key;

}

/**

* 如果给定的键key小于二叉查找戍节点的键,那么小于等于key的最椿定弛根节点的

* 如果给定的键key逮叉查找戍节点,那么只有当根节点右子舒在慈于key的节点时,

* 小于等于key的最磁会弛右子尸幅节点就是小于等于key的最

*

* @param x

* @param key

* @return

*/

private Node floor(Node x, Key key) {

if (x == null)

return null;

int cmp = key.compareTo(x.key);

if (cmp == 0)

return x;

else if (cmp < 0)

return floor(x.left, key);

else {

Node t = floor(x.right, key);

if (t == null)

return x;

else

return t;

}

}

/**

* 稀整:找弛等于该键的最小键

*

* @param key

* @return

*/

public Key ceiling(Key key) {

Node x = ceiling(root, key);

if (x == null)

return null;

else

return x.key;

}

/**

* 如果给定的键key逮叉查找戍节点的键,那么慈于key的最小键一定弛根节点的右子

* 如果给定的键key小于二叉查找戍节点,那么只有当根节点祖在慈于key的节点时,

* 慈于key的最小键才会弛赚幅节点就是慈于key的最小键

*

* @param x

* @param key

* @return

*/

private Node ceiling(Node x, Key key) {

if (x == null)

return null;

int cmp = key.compareTo(x.key);

if (cmp == 0)

return x;

else if (cmp > 0) {

return ceiling(x.right, key);

} else {

Node t = floor(x.left, key);

if (t == null)

return x;

else

return t;

}

}

/**

* 选喳为k的节点

*

* @param k

* @return

*/

public Key select(int k) {

return select(root, k).key;

}

private Node select(Node x, int k) {

if (x == null)

return null;

int t = size(x.left);

if (t > k)

return select(x.left, k);

else if (t < k)

return select(x.right, k - t - 1);// 根节点也要排除掉

else

return x;

}

/**

* 查找给定键值的排名

*

* @param key

* @return

*/

public int rank(Key key) {

return rank(key, root);

}

private int rank(Key key, Node x) {

if (x == null)

return 0;

int cmp = key.compareTo(x.key);

if (cmp < 0)

return rank(key, x.left);

else if (cmp > 0)

return 1 + size(x.left) + rank(key, x.right);

else

return size(x.left);

}

/**

* 删除最小键值对

*/

public void deleteMin(){

root = deleteMin(root);

}

/**

* 不断深入根节点的捉踊复接,然焊馅点的链接指厢点的右子树

* 此时已经没有任何链接指匣删除的结点,尹会被垃圾收集祈掉

* @param x

* @return

*/

private Node deleteMin(Node x){

if(x.left == null) return x.right;

x.left = deleteMin(x.left);

x.n = size(x.left)+size(x.right) + 1;

return x;

}

public void deleteMax(){

root = deleteMax(root);

}

private Node deleteMax(Node x){

if(x.right == null ) return x.left;

x.right = deleteMax(x.right);

x.n = size(x.left)+size(x.right) + 1;

return x;

}

public void delete(Key key){

root = delete(root,key);

}

private Node delete(Node x, Key key){

if(x == null) return null;

int cmp = key.compareTo(x.key);

if(cmp < 0) x.left = delete(x.left,key);

else if(cmp > 0) x.right = delete(x.right,key);

else{

if(x.right == null) return x.left;

if(x.left == null ) return x.right;

/**

* 如果被删除节点有两斧被删除节点暂记为t

* 从t的右子省取最小的节点x,将这搞x的转t的作

* 这搞x的右子湿t的右子示除了最小节点的子殊样就成功替换了t的位置

*/

Node t = x;

x = min(t.right);

x.left = t.left;

x.right = deleteMin(t.right);

}

x.n = size(x.left) + size(x.right) +1;

return x;

}

public void print(){

print(root);

}

private void print(Node x){

if(x == null ) return;

print(x.left);

StdOut.println(x.key);

print(x.right);

}

public Iterable keys(){

return keys(min(),max());

}

public Iterable keys(Key lo, Key hi){

Queue queue = new Queue();

keys(root, queue, lo, hi);

return queue;

}

private void keys(Node x, Queue queue, Key lo, Key hi){

if(x == null) return;

int cmplo = lo.compareTo(x.key);

int cmphi = lo.compareTo(x.key);

if(cmplo < 0 ) keys(x.left,queue,lo,hi);

if(cmplo <= 0 && cmphi >= 0) queue.enqueue(x.key);

if(cmphi > 0 ) keys(x.right,queue,lo,hi);

}

}

9ef5c768c2027747c0a7c55d677235ed.png

以上就是本文的全部内容二叉排序树代码,希望对茨学习有所帮助,也期望脆多支持脚本之家。

本文来自电脑杂谈,转载请注明本文网址:

http://www.pc-fly.com/a/jisuanjixue/article-132676-1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/** * 根据等级查询类目树 * * @param level * @return */ @Override public List queryCategoryTree(Integer level) { //查询当前级别下类目 List list = categoryDAO.list(level); //组装好的类目树,返回前端 List categoryTree = new ArrayList(); //所有类目 List allDTOList = new ArrayList(); if (CollectionUtils.isEmpty(list)) { return categoryTree; } for (CategoryDO categoryDO : list) { allDTOList.add(new CategoryTreeDTO().convertDOToDTO(categoryDO)); } //当前等级类目 categoryTree = allDTOList.stream().filter(dto -> level.equals(dto.getLevel())).collect(Collectors.toList()); for (CategoryTreeDTO categoryTreeDTO : categoryTree) { //组装类目为树结构 assembleTree(categoryTreeDTO, allDTOList,Constants.CATEGORY_MAX_LEVEL - level); } return categoryTree; } /** * 组装树 * * @param categoryTreeDTO * @param allList * @param remainRecursionCount 剩余递归次数 * @return */ public CategoryTreeDTO assembleTree(CategoryTreeDTO categoryTreeDTO, List allList, int remainRecursionCount) { remainRecursionCount--; //最大递归次数不超过Constants.CATEGORY_MAX_LEVEL-level次,防止坏数据死循环 if(remainRecursionCount < 0){ return categoryTreeDTO; } String categoryCode = categoryTreeDTO.getCategoryCode(); Integer level = categoryTreeDTO.getLevel(); //到达最后等级树返回 if (Constants.CATEGORY_MAX_LEVEL == level) { return categoryTreeDTO; } //子类目 List child = allList.stream().filter(a -> categoryCode.equals(a.getParentCode())).collect(Collectors.toList()); if (null == child) { return categoryTreeDTO; } categoryTreeDTO.setChildren(child); //组装子类目 for (CategoryTreeDTO dto : child) { assembleTree(dto, allList,remainRecursionCount); } return categoryTreeDTO; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值