分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net

package chimomo.learning.java.datastructure;/*** Probing table implementation of hash tables.* Note that all "matching" is based on the equals method.** @author Created by Chimomo*/
public class QuadraticProbingHashTable<T> {private static final int DEFAULT_TABLE_SIZE = 101;// The array of elements.private HashEntry<T>[] array;// The number of occupied cells.private int occupied;// Current size.private int size;/*** Construct the hash table.*/public QuadraticProbingHashTable() {this(DEFAULT_TABLE_SIZE);}/*** Construct the hash table.** @param size The approximate initial size.*/public QuadraticProbingHashTable(int size) {allocateArray(size);doClear();}/*** Internal method to find a prime number at least as large as n.** @param n The starting number (must be positive).* @return A prime number larger than or equal to n.*/private static int nextPrime(int n) {if (n % 2 == 0) {n++;}for (; !isPrime(n); n += 2) {}return n;}/*** Internal method to test if a number is prime.* Not an efficient algorithm.** @param n The number to test.* @return The result of the test.*/private static boolean isPrime(int n) {if (n == 2 || n == 3) {return true;}if (n == 1 || n % 2 == 0) {return false;}for (int i = 3; i * i <= n; i += 2) {if (n % i == 0) {return false;}}return true;}// Test program.public static void main(String[] args) {// Create quadratic probing hash table.QuadraticProbingHashTable<String> H = new QuadraticProbingHashTable<>();long startTime = System.currentTimeMillis();final int NUMS = 2000000;final int GAP = 37;System.out.println("Checking... (no more output means success)");// Insert.for (int i = GAP; i != 0; i = (i + GAP) % NUMS) {H.insert("" + i);}for (int i = GAP; i != 0; i = (i + GAP) % NUMS) {if (H.insert("" + i)) {System.out.println("OOPS!!! " + i);}}// Remove.for (int i = 1; i < NUMS; i += 2) {H.remove("" + i);}// Contains.for (int i = 2; i < NUMS; i += 2) {if (!H.contains("" + i)) {System.out.println("Find fails " + i);}}for (int i = 1; i < NUMS; i += 2) {if (H.contains("" + i)) {System.out.println("OOPS!!! " + i);}}long endTime = System.currentTimeMillis();System.out.println("Elapsed time: " + (endTime - startTime));}/*** Insert into the hash table.* If the item is already present, do nothing.** @param x The item to insert.*/public boolean insert(T x) {// Insert x as active.int currentPos = findPos(x);if (isActive(currentPos)) {return false;}if (array[currentPos] == null) {++occupied;}array[currentPos] = new HashEntry<>(x, true);size++;// Rehash.if (occupied > array.length / 2) {rehash();}return true;}/*** Expand the hash table.*/private void rehash() {HashEntry<T>[] oldArray = array;// Create a new double-sized, empty table.allocateArray(2 * oldArray.length);occupied = 0;size = 0;// Copy table over.for (HashEntry<T> entry : oldArray) {if (entry != null && entry.isActive) {insert(entry.element);}}}/*** Method that performs quadratic probing resolution.** @param x The item to search for.* @return The position where the search terminates.*/private int findPos(T x) {int offset = 1;int currentPos = myHash(x);while (array[currentPos] != null && !array[currentPos].element.equals(x)) {// Compute ith probe.currentPos += offset;offset += 2;if (currentPos >= array.length) {currentPos -= array.length;}}return currentPos;}/*** Remove from the hash table.** @param x The item to remove.* @return True if item removed.*/public boolean remove(T x) {int currentPos = findPos(x);if (isActive(currentPos)) {array[currentPos].isActive = false;size--;return true;} else {return false;}}/*** Get current size.** @return The current size.*/public int size() {return size;}/*** Get length of internal table.** @return The array length.*/public int capacity() {return array.length;}/*** Find an item in the hash table.** @param x The item to search for.* @return The matching item.*/public boolean contains(T x) {int currentPos = findPos(x);return isActive(currentPos);}/*** Return true if current position exists and is active.** @param currentPosition The result of a call to find position.* @return True if current position is active.*/private boolean isActive(int currentPosition) {return array[currentPosition] != null && array[currentPosition].isActive;}/*** Make the hash table logically empty.*/public void makeEmpty() {doClear();}/*** Do clear.*/private void doClear() {occupied = 0;for (int i = 0; i < array.length; i++) {array[i] = null;}}private int myHash(T x) {int hashVal = x.hashCode();hashVal %= array.length;if (hashVal < 0) {hashVal += array.length;}return hashVal;}/*** Internal method to allocate array.** @param arraySize The size of the array.*/private void allocateArray(int arraySize) {array = new HashEntry[nextPrime(arraySize)];}/*** Hash entry class for QuadraticProbingHashTable.** @param <AnyType> Any type.*/private static class HashEntry<AnyType> {public AnyType element; // The element.public boolean isActive; // False if marked deleted.public HashEntry(AnyType e) {this(e, true);}public HashEntry(AnyType e, boolean i) {element = e;isActive = i;}}
}/*
Output:
Checking... (no more output means success)
Elapsed time: 3968
*/

Data Structure - Quadratic Probing Hash Table (Java)相关推荐

  1. 十、散列表(Hash Table)

    一.概述 散列表(Hash Table),也称"哈希表"或者"Hash 表" 1.相关概念 原始数据叫作键(键值)或关键字(key): 将原始数据转化为数组下标 ...

  2. 数据结构学习笔记(七):哈希表(Hash Table)

    目录 1 哈希表的含义与结构特点 1.1 哈希(Hash)即无序 1.2 从数组看哈希表的结构特点 2 哈希函数(Hash Function)与哈希冲突(Hash Collision) 2.1 哈希函 ...

  3. 数据结构——散列表(Hash Table)(哈希表)

    散列表 散列表英文是hash table,经常被叫做Hash表,或者哈希表. 哈希表其实就是由数组演化而来的,利用的就是数组支持按照下标随机访问数据的特性,可以说散列表就是数组的一种扩展. 百度文库对 ...

  4. [C++]怎么样实现一个较快的Hash Table

    我们服务器一直在用boost/sgl stl的hash table,但是从来没有考虑过其中的效率问题,虽然hash_map/unordered_map跑的可能真的比map快一些,可能应该不是你理解的那 ...

  5. 【散列表(哈希表) Hash Table(上)】:Word文档中的单词拼写检查功能是如何实现的?

    Word 这种文本编辑器你平时应该经常用吧,那你有没有留意过它的拼写检查功能呢?一旦我们在 Word 里输入一个错误的英文单词,它就会用标红的方式提示"拼写错误".Word 的这个 ...

  6. 哈希表(Hash table)

    Hash table 哈希表的原理 哈希函数 冲突解决 [705. 设计哈希集合](https://leetcode.cn/problems/design-hashset/) 拉链法 1.定长拉链数组 ...

  7. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

  8. php Hash Table(四) Hash Table添加和更新元素

    HashTable添加和更新的函数: 有4个主要的函数用于插入和更新HashTable的数据: int zend_hash_add(HashTable *ht, char *arKey, uint n ...

  9. 什么是散列表(Hash Table)

    散列表(Hash table,也叫哈希表),是根据键(Key)而直接访问在内存存储位置的数据结构.也就是说,它通过计算一个关于键值的函数,将所需查询的数据映射到表中一个位置来访问记录,这加快了查找速度 ...

  10. 哈希表(Hash Table)及散列法(Hashing)

    bigshuai 哈希 源地址 http://www.cnblogs.com/bigshuai/articles/2398116.html 哈希表(Hash Table)及散列法(Hashing) 数 ...

最新文章

  1. Linux vsftp配置详解
  2. FTP虚拟账户的创建(1)
  3. 线程的堆栈——Windows核心编程学习手札之十六
  4. 新一代蓝牙对工业物联网(IIOT)的应用
  5. 【Elasticsearch】es Timelion是Kibana中时间序列的可视化工具
  6. 如何重置 RHEL7/CentOS7 系统的密码
  7. springmvc错点集结
  8. Atitit 分期理论 attilax总结
  9. 解决Access to Message Queuing system is denied.权限问题
  10. 使用nginx的ngx_upstream_jdomain模块实现k8s容器的负载均衡
  11. matlab无法打开wps的xls文件,xls文件怎么打开,wps无法打开xls文件怎么办
  12. html怎么设置一个banner图像,css如何设置banner图自适应
  13. 如何快速有效的推广微信公众帐号
  14. php微信端获取头像不显示不出来,解决微信转发到朋友圈没有获取页面头像以图标展示。...
  15. eNSP实验日记二划分Vlan
  16. easyui label 编辑_EasyUI datagrid 行编辑
  17. Win10下安装Spark的尝试总结(尚未出坑)
  18. 国民经济行业代码查询系统-公司行业代码查询
  19. source命令的作用
  20. C# WinForm需要用到的图标easyicon

热门文章

  1. HDU 4475 Downward paths (推公式)
  2. Swiper参数说明(swiper参数配置)
  3. java8的stream写法实现去重
  4. 虚拟机忘记root密码重置(转载
  5. git创建分支,以及提交到远程创库
  6. IPv6动态地址DNS解析
  7. mysql 本月老客户次月留存率_用mysql计算用户留存率
  8. win7命名计算机无法下一步,win7还原系统不能点下一步怎么办(无响应)
  9. win7计算机属性恢复,win7怎么打开系统还原功能?win7打开系统还原功能的方法步骤...
  10. React Native引用三方库报错underfined is not an object(evaluating 'viewproptypes.style')