m2skills

dhash java

Apr 4th, 2017
4,762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. // Program to implement Double Hashing
  2. import java.util.Scanner;
  3.  
  4. class hashTable{
  5.    
  6.     private int size, elementCount, num;
  7.     private int[] table;
  8.     public Scanner sc = new Scanner(System.in);
  9.    
  10.     // initialize hash Table
  11.     hashTable(){
  12.         System.out.print("Enter the size of the Table : ");
  13.         this.size = sc.nextInt();
  14.        
  15.         this.table = new int[size];
  16.         this.num = 11;
  17.        
  18.         for(int i=0; i < size; i++){
  19.             this.table[i] = 0;
  20.         }
  21.         this.elementCount = 0;
  22.     }
  23.    
  24.    
  25.     // method that checks if the hash table is full or not
  26.     boolean isFull(){
  27.         if(this.elementCount == this.size){
  28.             return true;
  29.         }else{
  30.             return false;
  31.         }
  32.     }
  33.    
  34.    
  35.     // method that returns position for a given element
  36.     int h1(int element){
  37.         return element % this.size;
  38.     }
  39.    
  40.     int h2(int element){
  41.         return element % this.num;
  42.     }
  43.    
  44.    
  45.     // method that finds the next empty position using quadratic probing
  46.     int doubleHashing(int element){
  47.         boolean posFound = false;
  48.         int limit = 50, i = 2;
  49.         int newPosition = -1;
  50.         while(i <= 50){
  51.             newPosition = (i*this.h1(element) + h2(element)) % this.size;
  52.            
  53.             if(this.table[newPosition] == 0){
  54.                 posFound = true;
  55.                 break;
  56.             }
  57.             else{
  58.                 i++;
  59.             }
  60.         }
  61.         if(posFound){
  62.             return newPosition;
  63.         }else{
  64.             return -1;
  65.         }  
  66.     }
  67.    
  68.     // method that inserts element inside the hash table
  69.     void insert(int element){
  70.         int position;
  71.         // checking if the this.table is full
  72.         if(this.isFull()){
  73.             System.out.println("Hash Table Full");
  74.             return;
  75.         }  
  76.        
  77.         boolean isStored = false;
  78.        
  79.         position = this.h1(element);
  80.            
  81.         // checking if the position is empty
  82.         if(this.table[position] == 0){
  83.             this.table[position] = element;
  84.             System.out.println("Element " + element + " at position " + position);
  85.             isStored = true;
  86.             this.elementCount += 1;
  87.         }  
  88.        
  89.         // collision occured hence we do quadratic probing
  90.         else{
  91.             System.out.println("Collision occured for Element " + element + " at position " + position + "Finding new position.");
  92.             position = this.doubleHashing(element);
  93.             if(position != -1){
  94.                 this.table[position] = element;
  95.                 this.elementCount += 1;
  96.                 System.out.println("Element " + element + " stored at position " + position);
  97.             }
  98.         }  
  99.         return;
  100.     }  
  101.  
  102.     // method that searches for an element in the table
  103.     // returns position of element if found
  104.     // else returns false
  105.     int search(int element){
  106.         boolean found = false;
  107.        
  108.         int position = this.h1(element);
  109.        
  110.         if(this.table[position] == element){
  111.             found = true;
  112.             return position;
  113.         }  
  114.        
  115.         // if element is not found at position returned by hash function
  116.         // then we search element using quadratic probing
  117.         else{
  118.             int limit = 50, i = 1;
  119.             int newPosition = -1;
  120.             while(i <= 50){
  121.                 newPosition = (i*this.h1(element) + h2(element)) % this.size;
  122.                 if(this.table[newPosition] == element){
  123.                     found = true;
  124.                     break;
  125.                 }
  126.                 else{
  127.                     i++;
  128.                 }
  129.             }
  130.             if(found){
  131.                 return newPosition;
  132.             }
  133.             else{
  134.                 System.out.println("Element not found");
  135.                 return -1;
  136.             }
  137.         }
  138.     }  
  139.    
  140.    
  141.     // method to remove an element from the table      
  142.     void remove(int element){
  143.         int position = this.search(element);
  144.         if(position != -1){
  145.             this.table[position] = 0;
  146.             System.out.println("Element " + element + " is Deleted");
  147.             this.elementCount -= 1;
  148.         }else{
  149.             System.out.println("Element is not present in the Hash Table");
  150.         }
  151.         return;
  152.     }  
  153.    
  154.     // method to display the hash table
  155.     void display(){
  156.         for (int i = 0; i < this.size; i++){
  157.             System.out.println(i + " = " + this.table[i]);
  158.         }  
  159.         System.out.println("The number of element is the Table are : " + this.elementCount);
  160.     }  
  161. };     
  162.  
  163. public class hash{
  164.     public static void main(String arg[]){
  165.        
  166.         hashTable table1 = new hashTable();
  167.  
  168.         // storing elements in this.table
  169.         table1.insert(12);
  170.         table1.insert(26);
  171.         table1.insert(31);
  172.         table1.insert(17);
  173.         table1.insert(90);
  174.         table1.insert(28);
  175.         table1.insert(88);
  176.         table1.insert(40);
  177.         table1.insert(77);      // element that causes collision at position 0
  178.  
  179.         // displaying the Table
  180.         table1.display();
  181.  
  182.         // printing position of elements
  183.         System.out.println("The position of element 31 is : " + table1.search(31));
  184.         System.out.println("The position of element 28 is : " + table1.search(28));
  185.         System.out.println("The position of element 90 is : " + table1.search(90));
  186.         System.out.println("The position of element 77 is : " + table1.search(77));
  187.         System.out.println("The position of element 1 is : " + table1.search(1));
  188.  
  189.  
  190.         table1.remove(90);
  191.         table1.remove(12);
  192.  
  193.         table1.display();
  194.     }
  195. }
Add Comment
Please, Sign In to add comment