怎么在java项目中实现一个二叉查找树算法
短信预约 -IT技能 免费直播动态提醒
今天就跟大家聊聊有关怎么在java项目中实现一个二叉查找树算法,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
具体内容如下
package 查找;import edu.princeton.cs.algs4.Queue;import edu.princeton.cs.algs4.StdOut;public class BST<Key extends Comparable<Key>, 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; } 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; } 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); } public Key floor(Key key) { Node x = floor(root, key); if (x == null) return null; else return x.key; } 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; } } public Key ceiling(Key key) { Node x = ceiling(root, key); if (x == null) return null; else return x.key; } 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; } } 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; } 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); } 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; 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<Key> keys(){ return keys(min(),max()); } public Iterable<Key> keys(Key lo, Key hi){ Queue<Key> queue = new Queue<Key>(); keys(root, queue, lo, hi); return queue; } private void keys(Node x, Queue<Key> 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); }}
看完上述内容,你们对怎么在java项目中实现一个二叉查找树算法有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网行业资讯频道,感谢大家的支持。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341