How can Implement a perfect hashed data structure using the four basic operations?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dseals22
    New Member
    • Jan 2017
    • 74

    How can Implement a perfect hashed data structure using the four basic operations?

    How to implement the perfect hashed data structure using the four basic operations (insert, fetch, delete, and update)? Will I have to use a hashtable to do this?

    This is my starting pseudocode below:
    Code:
    public class HashedClass{ 
    
    int ticketNumber; // keyfield 
    string purchaserName; 
    
    Hashtable hashtable = new Hashtable(); 
    
    insert(); 
    
    fetch();
    
    delete(); 
    
    update();
    
    }
    I started a pseudocode above, but I know it needs more work. Are there any resources that can help guide me to implementing my own java hashed perfect algorithm? I know that you have to use the direct hashed four basic operation for each four methods, right? But how can I do that based on pseudocode?

    I want my application to store nodes for a stadium ticket application where the ticket numbers range from 2000 to 100,000 for a 60,000 seat stadium. The ticket number will be the key field and the nodes will also store the purchaser's name.
    Last edited by Frinavale; Oct 17 '17, 04:29 PM. Reason: More detailed question
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Hashing takes the value of something and translates into a fixed-length key that represents the original value.

    You need to implement a hashing algorithm that translates the data you have into a a key.

    Once you have that algorithm you will need to use it to insert new items into your structure and for determining if an item exists in your structure (if you are fetching, updating, or deleting something and don't already know the hashed key for the item)

    That being said, you may also have to come up with an algorithm that resolves conflicts (or raise appropriate errors to state that an item already exists in that position).

    The fun of your exercise is really coming up with the hash algorithm and resolution algorithms. There are many ways to do this.

    Start researching the Hashing before you begin.
    You could even start with reading through the wikipedia entry for Hash function
    Last edited by Frinavale; Oct 17 '17, 04:45 PM.

    Comment

    • dseals22
      New Member
      • Jan 2017
      • 74

      #3
      @Frinavale, @chaarmann,

      Could I do something like this or do I need to break up my
      code into two different classes and then test the methods from the other class inside of another class's main method:
      Code:
      import java.util.Scanner; 
      
      public class StadiumTickets
      {
      
           int ticketNumber; // keyfield
      
           String purchaserName; 
      
      
        public void input()
        {
      
           Scanner input= new Scanner(System.in);
      
            // key values ranges between 2000 to 100,000 
      
      
            System.out.print("Please enter a ticket number between 2000 to 100,000: "); 
      
            // a variable to hold the answer
      
             ticketNumber= input.nextInt(); 
          
      
              // error checking to make sure the user doesn't enter a key outside of the lower or upper ranges
      
             if(ticketNumber < 2000 && ticketNumber > 100000) 
              System.out.println("This number is not a valid entry to input into the structure.");
           }
      
        public StadiumTickets(int ticketNumber, String purchaserName)
        {
           
             this.ticketNumber= ticketNumber; 
             this.purchaserName= purchaserName; 
         } 
      
      
       
         public StadiumTickets deepCopy()
         { 
      
            StadiumTickets clone= new StadiumTickets(ticketNumber, purchaserName); 
             return clone; 
         }  
      }

      Code:
      public class PerfectHash
      {
      
        private StadiumTickets[] data; 
        
      
        public boolean insert(StadiumTickets newStadiumTicket)
        {
            // the direct insert hashed algorithm 
      
             pseudoKey = preProcessing(targetKey); 
             ip = pseudoKey; // direct hashing function 
      
             // insert the new node
              data[ip] = newStadiumTicket.deepCopy(); 
        }
        
        public StadiumTickets fetch(String targetKey)
        {
         
          // the fetch algorithm 
          // access the primary storage area 
      
           pseudoKey = preprocessing(targetKey); 
           ip = pseudoKey; // direct hashing function 
          if(data[ip]== null)
           { return null; 
      
           } 
           else
           {
              return data[ip].deepCopy(); 
           }
         } 
      
         public boolean delete(String targetKey)
         { 
            // the delete direct hashed algorithm 
      
            // access the primary storage area 
               pseudoKey = preprocessing(targetKey); 
             ip = pseudoKey; // direct hashing function 
      
            if(data[ip]== null)
            { 
               return false; 
             } 
            else 
            { 
              data[ip]= null;                      
              return true; 
            }
          } 
          public boolean update(String targetKey, StadiumTickets newStadiumTicket)
          {
              // the update direct hashed algorithm 
      
             if(delete(targetKey) == false)
                 return false; 
             else 
              {
                 insert(newStadiumTicket) 
                   return true;
               }
           }
         
      }

      Comment

      Working...