java实现二叉查找树

1.什么是二叉查找树
               二叉查找树是应用了链表的灵活性和数组的查找的高效性来实现对数据的操作和查找。
               二叉查找树要么是一棵空树,要么就必须具有如下特征:
                         1.二叉查找树只能有左右两棵子树
                         2.根节点的值大于左子树的值,小于右子树的值(左右子树不为空的情况)
                         3.每个节点都有相应的键和值,且不存在键值相同的点
2.二叉查找树的作用
               对数据进行排序以实现对数据的高效查找。
3.二叉查找树与二分法的区别
               二叉查找树是基于节点的,通过链表结构实现的,而二分法是通过数组实现的,所以当
          插入数据或者删除数据的时候,二叉查找树不需要移动后面的那些节点数据,只需要改变节
          点的所指的方向就可以了,而二分法是数组实现的,插入和删除数据需要将后面的数据移动
          一位,因此二叉查找树比二分法更加高效。
4.二叉查找树的实现
  1. import java.util.Iterator;

  2. /**
  3.  * 二叉查找树
  4.  * @author spenglu
  5.  *
  6.  */
  7. public class BinarySearchTree<Key extends Comparable<Key>,Value> {
  8.     private Node root;
  9.     private class Node{
  10.          private Key key;
  11.          private Value value;
  12.          private Node left;
  13.          private Node right;
  14.          private int n;  //以该节点为根的子数中的节点总数

  15.          public Node(Key key,Value value,int n){
  16.              this.key = key;
  17.              this.value = value;
  18.              this.n = n;
  19.          }
  20.     }
  21.     //节点总数
  22.     public int size(){
  23.          return size(root);
  24.     }

  25.     private int size(Node root2) {
  26.          // TODO Auto-generated method stub
  27.          if (root2==null) return 0;
  28.          return root2.n;
  29.     }

  30.     //通过键获取相应的值
  31.     public Value get(Key key){
  32.          return get(root,key);
  33.     }

  34.     private Value get (Node root2, Key key) {
  35.          // TODO Auto-generated method stub
  36.          if (root2==null) return null;
  37.          int cmp = key.compareTo(root2.key);
  38.          if (cmp<0) return get(root2.left, key);
  39.          if (cmp>0) return get(root.right, key);
  40.          else return root2.value;
  41.     }

  42.     //插入相应的键值对
  43.     //如果是已存在这个节点则更新这个节点,否则插入这个新节点
  44.     //实际就是构建二叉查找树的方法
  45.     public void put (Key key,Value value) {
  46.          root = put(root, key,value);
  47.     }

  48.     private Node put(Node root2, Key key, Value value) {
  49.          // TODO Auto-generated method stub
  50.          if(root2==null) return new Node(key, value, 1);
  51.          int cmp = key.compareTo(root2.key);
  52.          if(cmp<0) root2.left = put(root2.left, key, value);
  53.          if(cmp>0) root2.right = put(root2.right, key, value);
  54.          else root2.value=value;
  55.          root2.n = size(root2.left)+size(root2.right)+1;
  56.          return root2;
  57.     }

  58.     //获取值最大的键
  59.     public Key max(){
  60.          return max(root).key;
  61.     }

  62.     private Node max(Node root2) {
  63.          // TODO Auto-generated method stub
  64.          if(root2==null) return root2;
  65.          return max(root2.right);
  66.     }

  67.     //获取值最小的键
  68.     public Key min(){
  69.          return min(root).key;
  70.     }

  71.     private Node min(Node root2) {
  72.          // TODO Auto-generated method stub
  73.          if(root2==null) return root2;
  74.          return min(root2.left);
  75.     }

  76.     //向下取整,获取小于等于某个值的键
  77.     public Key floor(Key key){
  78.          Node x = floor(root,key);
  79.          if(x==null) return null;
  80.          return x.key;
  81.     }

  82.     private Node floor(Node root2, Key key) {
  83.          // TODO Auto-generated method stub
  84.          if(root2==null) return null;
  85.          int cmp = key.compareTo(root2.key);
  86.          if(cmp==0) return root2;
  87.          if(cmp<0) return floor(root.left, key);
  88.          Node x = floor(root2.right, key);
  89.          if(x!=null) return x;
  90.          else return root2;
  91.     }

  92.     //向上去整,获取大于等于某个值的键
  93.     public Key ceiling(Key key){
  94.          Node x = ceiling(root,key);
  95.          if(x==null) return null;
  96.          return x.key;
  97.     }

  98.     private Node ceiling(Node root2, Key key) {
  99.          // TODO Auto-generated method stub
  100.          if(root2==null) return null;
  101.          int cmp = key.compareTo(root2.key);
  102.          if(cmp==0) return root2;
  103.          if(cmp>0) return ceiling(root2.right, key);
  104.          Node x = ceiling(root2.left, key);
  105.          if(x!=null) return x;
  106.          else return root2;
  107.     }

  108.     //找到排名为k的键
  109.     public Key select(int k){
  110.          return select(root,k).key;
  111.     }

  112.     private Node select(Node root2, int k) {
  113.          // TODO Auto-generated method stub
  114.          if(root2==null) return null;
  115.          int temp = size(root2);
  116.          if(k>temp) return select(root2.right, k-temp-1);
  117.          else if(k<temp) return select(root2.left, k);
  118.          else return root2;
  119.     }

  120.     //返回给定键的排名
  121.     public int rank(Key key){
  122.          return rank(root,key);
  123.     }

  124.     private int rank(Node root2, Key key) {
  125.          // TODO Auto-generated method stub
  126.          if(root2==null) return 0;
  127.          int cmp = key.compareTo(root2.key);
  128.          if(cmp<0) return rank(root2.left, key);
  129.          else if(cmp>0) return size(root2.left)+1+rank(root2.right,key);
  130.          else return size(root2.left);
  131.     }

  132.     //删除最小值
  133.     public void deleteMin(){
  134.          root = deleteMin(root);
  135.     }

  136.     private Node deleteMin(Node root2) {
  137.          // TODO Auto-generated method stub
  138.          if(root2.left==null) return root2.right;
  139.          root2.left = deleteMin(root2.left);
  140.          root2.n = size(root2.left)+size(root2.right)+1;
  141.          return root2;
  142.     }

  143.     //删除最大值
  144.     public void deleteMax(){
  145.          root = deleteMax(root);
  146.     }

  147.     private Node deleteMax(Node root2) {
  148.          // TODO Auto-generated method stub
  149.          if(root2.right==null) return root2.left;
  150.          root2.right = deleteMax(root2.right);
  151.          root2.n = size(root2.left)+size(root2.right)+1;
  152.          return root2;
  153.     }

  154.     //删除给定某个给定键的值
  155.     public void delete(Key key){
  156.          root = delete(root,key);
  157.     }

  158.     private Node delete(Node root2, Key key) {
  159.          // TODO Auto-generated method stub
  160.          if(root2==null) return null;
  161.          int cmp = key.compareTo(root2.key);
  162.          if(cmp<0) root2.left = delete(root2.left, key);
  163.          else if(cmp>0) root2.right = delete(root2.right, key);
  164.          else{
  165.              if(root2.right==null) return root2.left;
  166.              if(root2.left==null) return root2.right;
  167.              Node x = root2;
  168.              root2 = min(x.right);
  169.              root2.right = deleteMin(x.right);
  170.              root2.left = x.left;
  171.          }
  172.          root2.n = size(root2.left)+size(root2.right)+1;
  173.          return root2;
  174.     }

  175.     //二叉查找树的构建
  176.     public void buildTree(Key key,Value value){
  177.          root = buildTree(root, key,value);
  178.     }

  179.     private Node buildTree(Node root2, Key key, Value value) {
  180.          // TODO Auto-generated method stub
  181.          if (root2==null) return new Node(key, value, 1);
  182.          int cmp = key.compareTo(root2.key);
  183.          if (cmp<0) root2.left = buildTree(root2.left, key, value);
  184.          else if (cmp>0) root2.right = buildTree(root2.right, key, value);
  185.          else root2.value=value;
  186.          root2.n = size(root2.left)+size(root2.right)+1;
  187.          return root2;
  188.     }

  189.     public static void main(String[] args) {

  190.     }
  191. }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值