Searching: Chained Hash Table Search And Insertion-2

本文详细阐述了使用Java实现散列表的查找和插入操作,通过Node类和Main类展示了具体实现过程,包括哈希函数的应用和链式散列表的原理。

Separate chaining

Chained Hash Table Search And Insertion-2:Searching


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 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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值