编程进阶网编程进阶网
  • 基础组成体系
  • 程序编程原理
  • 异常和IO系统
  • 六大设计原则
  • 设计模式导读
  • 创建型设计模式
  • 结构型设计模式
  • 行为型设计模式
  • 设计模式案例
  • 面向对象思想
  • 基础入门
  • 高级进阶
  • JVM虚拟机
  • 数据集合
  • Java面试题
  • C语言入门
  • C综合案例
  • C标准库
  • C语言专栏
  • C++入门
  • C++综合案例
  • C++专栏
  • HTML
  • CSS
  • JavaScript
  • 前端专栏
  • Swift
  • iOS入门
  • 基础入门
  • 开源库解读
  • 性能优化
  • Framework
  • 方案设计
  • 媒体音视频
  • 硬件开发
  • Groovy
  • 常用工具
  • 大厂面试题
  • 综合案例
  • 网络底层
  • Https
  • 网络请求
  • 故障排查
  • 专栏
  • 数组
  • 链表
  • 栈
  • 队列
  • 树
  • 递归
  • 哈希
  • 排序
  • 查找
  • 字符串
  • 其他
  • Bash脚本
  • Linux入门
  • 嵌入式开发
  • 代码规范
  • Markdown
  • 开发理论
  • 开发工具
  • Git管理
  • 百宝箱
  • 开源协议
  • 技术招聘
  • 测试经验
  • 职场提升
  • 技术模版
  • 关于我
  • 目标清单
  • 学习框架
  • 育儿经验
  • 我的专栏
  • 底层能力
  • 读书心得
  • 随笔笔记
  • 职场思考
  • 中华历史
  • 经济学故事
  • 基础组成体系
  • 程序编程原理
  • 异常和IO系统
  • 六大设计原则
  • 设计模式导读
  • 创建型设计模式
  • 结构型设计模式
  • 行为型设计模式
  • 设计模式案例
  • 面向对象思想
  • 基础入门
  • 高级进阶
  • JVM虚拟机
  • 数据集合
  • Java面试题
  • C语言入门
  • C综合案例
  • C标准库
  • C语言专栏
  • C++入门
  • C++综合案例
  • C++专栏
  • HTML
  • CSS
  • JavaScript
  • 前端专栏
  • Swift
  • iOS入门
  • 基础入门
  • 开源库解读
  • 性能优化
  • Framework
  • 方案设计
  • 媒体音视频
  • 硬件开发
  • Groovy
  • 常用工具
  • 大厂面试题
  • 综合案例
  • 网络底层
  • Https
  • 网络请求
  • 故障排查
  • 专栏
  • 数组
  • 链表
  • 栈
  • 队列
  • 树
  • 递归
  • 哈希
  • 排序
  • 查找
  • 字符串
  • 其他
  • Bash脚本
  • Linux入门
  • 嵌入式开发
  • 代码规范
  • Markdown
  • 开发理论
  • 开发工具
  • Git管理
  • 百宝箱
  • 开源协议
  • 技术招聘
  • 测试经验
  • 职场提升
  • 技术模版
  • 关于我
  • 目标清单
  • 学习框架
  • 育儿经验
  • 我的专栏
  • 底层能力
  • 读书心得
  • 随笔笔记
  • 职场思考
  • 中华历史
  • 经济学故事
  • 01.Hash算法待完善

目录介绍

  • 01.开放地址法
  • 02.代码案例
  • 03.测试代码

好消息

  • 博客笔记大汇总【15年10月到至今】,包括Java基础及深入知识点,Android技术博客,Python学习笔记等等,还包括平时开发中遇到的bug汇总,当然也在工作之余收集了大量的面试题,长期更新维护并且修正,持续完善……开源的文件是markdown格式的!同时也开源了生活博客,从12年起,积累共计N篇[近100万字,陆续搬到网上],转载请注明出处,谢谢!所有博客陆续更新到GitHub上!
  • 链接地址:https://github.com/yangchong211/YCBlogs
  • 如果觉得好,可以star一下,谢谢!当然也欢迎提出建议,万事起于忽微,量变引起质变!

01.开放地址法

  • 在开放地址法中,通过再哈希法寻找一个空位解决冲突问题,另一个方法是在哈希表每个单元中设置链表(即链地址法),某个数据项的关键字值还是像通常一样映射到哈希表的单元,而数据项本身插入到这个单元的链表中。其他同样映射到这个位置的数据项只需要加到链表中,不需要在原始的数组中寻找空位。
    • image
      image

02.代码案例

  • 有序链表:
    public class SortLink {
        private LinkNode first;
        public SortLink(){
            first = null;
        }
        public boolean isEmpty(){
            return (first == null);
        }
        public void insert(LinkNode node){
            int key = node.getKey();
            LinkNode previous = null;
            LinkNode current = first;
            while(current != null && current.getKey() < key){
                previous = current;
                current = current.next;
            }
            if(previous == null){
                first = node;
            }else{
                previous.next = node;
            }
          node.next = curent;
        }
        public void delete(int key){
            LinkNode previous = null;
            LinkNode current = first;
            if(isEmpty()){
                System.out.println("Linked is Empty!!!");
                return;
            }
            while(current != null && current.getKey() != key){
                previous = current;
                current = current.next;
            }
            if(previous == null){
                first = first.next;
            }else{
                previous.next = current.next;
            }
        }
         
        public LinkNode find(int key){
            LinkNode current = first;
            while(current != null && current.getKey() <= key){
                if(current.getKey() == key){
                    return current;
                }
                            current = current.next;
            }
            return null;
        }
         
        public void displayLink(){
            System.out.println("Link(First->Last)");
            LinkNode current = first;
            while(current != null){
                current.displayLink();
                current = current.next;
            }
            System.out.println("");
        }
        class LinkNode{
            private int iData;
            public LinkNode next;
            public LinkNode(int iData){
                this.iData = iData;
            }
            public int getKey(){
                return iData;
            }
            public void displayLink(){
                System.out.println(iData + " ");
            }
        }
    }
  • 链地址法:
    public class HashChain {
        private SortLink[] hashArray;//数组中存放链表
        private int arraySize;
        public HashChain(int size){
            arraySize = size;
            hashArray = new SortLink[arraySize];
            //new 出每个空链表初始化数组
            for(int i = 0 ; i < arraySize ; i++){
                hashArray[i] = new SortLink();
            }
        }
         
        public void displayTable(){
            for(int i = 0 ; i < arraySize ; i++){
                System.out.print(i + ":");
                hashArray[i].displayLink();
            }
        }
         
        public int hashFunction(int key){
            return key%arraySize;
        }
         
        public void insert(LinkNode node){
            int key = node.getKey();
            int hashVal = hashFunction(key);
            hashArray[hashVal].insert(node);//直接往链表中添加即可
        }
         
        public LinkNode delete(int key){
            int hashVal = hashFunction(key);
            LinkNode temp = find(key);
            hashArray[hashVal].delete(key);//从链表中找到要删除的数据项,直接删除
            return temp;
        }
         
        public LinkNode find(int key){
            int hashVal = hashFunction(key);
            LinkNode node = hashArray[hashVal].find(key);
            return node;
        }
     
    }
  • 链地址法中,装填因子(数据项数和哈希表容量的比值)与开放地址法不同,在链地址法中,需要有N个单元的数组中转入N个或更多的数据项,因此装填因子一般为1,或比1大(有可能某些位置包含的链表中包含两个或两个以上的数据项)。
  • 找到初始单元需要O(1)的时间级别,而搜索链表的时间与M成正比,M为链表包含的平均项数,即O(M)的时间级别。

参考文献

  • 极客时间,王争大神,数据结构和算法之美

01.关于博客汇总链接

  • 1.技术博客汇总
  • 2.开源项目汇总
  • 3.生活博客汇总
  • 4.喜马拉雅音频汇总
  • 5.其他汇总

02.关于我的博客

  • github:https://github.com/yangchong211
  • 知乎:https://www.zhihu.com/people/yczbj/activities
  • 简书:http://www.jianshu.com/u/b7b2c6ed9284
  • csdn:http://my.csdn.net/m0_37700275
  • 喜马拉雅听书:http://www.ximalaya.com/zhubo/71989305/
  • 开源中国:https://my.oschina.net/zbj1618/blog
  • 泡在网上的日子:http://www.jcodecraeer.com/member/content_list.php?channelid=1
  • 邮箱:yangchong211@163.com
  • 阿里云博客:https://yq.aliyun.com/users/article?spm=5176.100- 239.headeruserinfo.3.dT4bcV
  • segmentfault头条:https://segmentfault.com/u/xiangjianyu/articles
  • 掘金:https://juejin.im/user/5939433efe88c2006afa0c6e
贡献者: yangchong211