zoukankan      html  css  js  c++  java
  • Map( 基于平衡二叉树实现)

    基于平衡二叉树实现Map

    public class AVLTreeMap<K extends  Comparable<K>,V> implements IMap<K,V> {
    
        private  AVLTree<K,V> avl;
    
        public AVLTreeMap(){
            avl = new AVLTree<K,V>();
        }
    
        public void add(K key, V value) {
            avl.add(key,value);
        }
    
        public V remove(K key) {
            return avl.remove(key);
        }
    
        public boolean contains(K key) {
            return avl.contains(key);
        }
    
        public V get(K key) {
            return avl.get(key);
        }
    
        public void set(K key, V newValue) {
            avl.set(key,newValue);
        }
    
        public int getSize() {
            return avl.getSize();
        }
    
        public boolean isEmpty() {
            return avl.isEmpty();
        }
    
    
    }
    

      

    三种Map的比较

     public static void main(String[] args) {
    
            ArrayList<String> list = new ArrayList<String>();
            String[] arr = {"张三", "李四","王五", "赵六","张三丰","李思明","王老五","赵明"};
            for(int i = 0; i < 10; i++){
                for(String str : arr){
                    String key = str +i;
                    list.add(key);
                }
            }
            for(int i = 0; i < 15000; i++){
                for(String str : arr){
                    String key = str +i;
                    list.add(key);
                }
            }
            System.out.println("准备的数据数量=" + list.size());
    
            System.out.println("-------------------------------");
            AVLTreeMap<String, Integer> map3 = new AVLTreeMap<String, Integer>();
            double time3 =   testMap(map3, list);
            System.out.println("平衡二叉树花费" + time3);
            System.out.println("-------------------------------");
            BinarySearchTreeMap<String, Integer> map2 = new BinarySearchTreeMap<String, Integer>();
            double time2 =   testMap(map2, list);
            System.out.println("二分搜索树花费" + time2);
            System.out.println("-------------------------------");
            LinkedListMap<String, Integer> map1 = new LinkedListMap<String, Integer>();
            double time1 =  testMap(map1, list);
            System.out.println("链表Map花费" + time1);
            System.out.println("-------------------------------");
    
    
    
        }
    

     

    输出结果如下:可以发现平衡二叉树时间比二分搜索树还短。

    准备的数据数量=120080
    -------------------------------
    Map size: 120000
    张三0出现的次数:2
    平衡二叉树花费0.097201387
    -------------------------------
    Map size: 120000
    张三0出现的次数:2
    二分搜索树花费0.10165089
    -------------------------------

    Map size: 120000
    张三0出现的次数:2
    链表Map花费100.172040309
    -------------------------------

    作者:Work Hard Work Smart
    出处:http://www.cnblogs.com/linlf03/
    欢迎任何形式的转载,未经作者同意,请保留此段声明!

  • 相关阅读:
    CentOS虚拟机和物理机共享文件夹实现
    集训第六周 数学概念与方法 概率 数论 最大公约数 G题
    集训第六周 数学概念与方法 概率 F题
    集训第六周 E题
    集训第六周 古典概型 期望 D题 Discovering Gold 期望
    集训第六周 古典概型 期望 C题
    集训第六周 数学概念与方法 UVA 11181 条件概率
    集训第六周 数学概念与方法 UVA 11722 几何概型
    DAG模型(矩形嵌套)
    集训第五周 动态规划 K题 背包
  • 原文地址:https://www.cnblogs.com/linlf03/p/14402796.html
Copyright © 2011-2022 走看看