Searching: Chained Hash Table Search And Insertion-2
2015-05-31 12:39
483 查看
Separate chaining
Java program
In this program, R1,…,RN were simplified to K1,…,KN.Node.java
package com.algorithms.searching; /** * Created with IntelliJ IDEA. * User: 1O1O * Date: 12/13/13 * Time: 6:52 PM * :)~ * Chained Hash Table Search And Insertion-2:Searching */ public class Node { int KEY; Node LINK; Node(int key){ KEY = key; LINK=null; } }
Main.java
package com.algorithms.searching; /** * Created with IntelliJ IDEA. * User: 1O1O * Date: 12/13/13 * Time: 6:52 PM * :)~ * Chained Hash Table Search And Insertion-2:Searching */ public class Main { public static Node[] HEAD = new Node[20]; public static int M = 19; public static int R = M+1; public static void searchAndInsertNode(int key){ int hash = key%19+1; /*Hash function:h(K)*/ Node pointer; if(HEAD[hash] == null){ Node node = new Node(key); HEAD[hash] = node; System.out.println(String.format("%4s",key+":")+" not found in the hash table, then insert it in HEAD["+hash+"] list"); }else { pointer = HEAD[hash]; do{ if(key == pointer.KEY){ System.out.println(key+" found in the hash table: HEAD["+hash+"]"); break; } if(pointer.LINK != null){ pointer = pointer.LINK; }else { Node node = new Node(key); pointer.LINK = node; System.out.println(String.format("%4s",key+":")+" not found in the hash table, then insert it in HEAD["+hash+"] list"); break; } }while (true); } } public static void main(String[] args) { /*Prepare the Hash Table*/ System.out.println("Prepare the Hash Table:"); searchAndInsertNode(503); searchAndInsertNode(87); searchAndInsertNode(512); searchAndInsertNode(61); searchAndInsertNode(908); searchAndInsertNode(170); searchAndInsertNode(897); searchAndInsertNode(275); searchAndInsertNode(653); searchAndInsertNode(426); searchAndInsertNode(154); searchAndInsertNode(509); searchAndInsertNode(612); searchAndInsertNode(677); searchAndInsertNode(765); searchAndInsertNode(703); System.out.println(); /*Print the current Hash Table with 16 nodes*/ System.out.println("Print the current Hash Table with 16 nodes:"); for(int j=1; j<=19; j++){ Node point = HEAD[j]; if(point != null){ System.out.print(String.format("%3s", j+":")+String.format("%4s", point.KEY)); point = point.LINK; while (point != null){ System.out.print(" ->"+String.format("%4s", point.KEY)); point = point.LINK; } System.out.println(); } } System.out.println(); /*Search Node 765 in current Hash Table*/ System.out.println("Search Node 765 in current Hash Table:"); searchAndInsertNode(765); System.out.println(); searchAndInsertNode(1); searchAndInsertNode(2); searchAndInsertNode(3); searchAndInsertNode(4); searchAndInsertNode(5); } }
Outputs
Prepare the Hash Table: 503: not found in the hash table, then insert it in HEAD[10] list 87: not found in the hash table, then insert it in HEAD[12] list 512: not found in the hash table, then insert it in HEAD[19] list 61: not found in the hash table, then insert it in HEAD[5] list 908: not found in the hash table, then insert it in HEAD[16] list 170: not found in the hash table, then insert it in HEAD[19] list 897: not found in the hash table, then d3a8 insert it in HEAD[5] list 275: not found in the hash table, then insert it in HEAD[10] list 653: not found in the hash table, then insert it in HEAD[8] list 426: not found in the hash table, then insert it in HEAD[9] list 154: not found in the hash table, then insert it in HEAD[3] list 509: not found in the hash table, then insert it in HEAD[16] list 612: not found in the hash table, then insert it in HEAD[5] list 677: not found in the hash table, then insert it in HEAD[13] list 765: not found in the hash table, then insert it in HEAD[6] list 703: not found in the hash table, then insert it in HEAD[1] list Print the current Hash Table with 16 nodes: 1: 703 3: 154 5: 61 -> 897 -> 612 6: 765 8: 653 9: 426 10: 503 -> 275 12: 87 13: 677 16: 908 -> 509 19: 512 -> 170 Search Node 765 in current Hash Table: 765 found in the hash table: HEAD[6] 1: not found in the hash table, then insert it in HEAD[2] list 2: not found in the hash table, then insert it in HEAD[3] list 3: not found in the hash table, then insert it in HEAD[4] list 4: not found in the hash table, then insert it in HEAD[5] list 5: not found in the hash table, then insert it in HEAD[6] list
Reference
<< The art of computer programming: Sorting and Searching >> VOLUME 3, DONALD E. KNUTH相关文章推荐
- c语言实现hashmap(转载)
- 表格标签table深入了解
- Jquery实现的table最后一行添加样式的代码
- Search Engine XSS Worm
- Lua 学习笔记之C API 遍历 Table实现代码
- Lua中获取table长度问题探讨
- 获取Lua表结构(table)数据实例
- Lua教程之弱引用table
- Lua中释放table占用内存的方法
- Lua中table的一些辅助函数介绍
- Ruby中Hash的11个问题解答
- Lua中获取table长度的方法
- MySQL学习笔记5:修改表(alter table)
- table 行转列的sql详解
- Ruby简明教程之数组和Hash介绍
- 在C#中生成与PHP一样的MD5 Hash Code的方法
- asp 去掉html中的table正则代码函数
- js中hash和ico的关联分析
- Javascript遍历Html Table示例(包括内容和属性值)
- 用div实现像table一样的布局方法