Hash data Structure C + + describes __ garbled problem

Source: Internet
Author: User

In this paper, two kinds of hashing are implemented--linear open addressable hashing, linked list hashing

Hash list-A dynamic collection structure that supports only the basic rules for Insert,search and delete operations: Keyword Key,index=fun (key), T[index] satellite data, function fun that is, hash function.
The expected time to find an element is O (1)

Direct addressing table: The keyword whole domain is relatively small, uses the array to store completely, corresponds each. Insert,search and delete operations are all O (1)
Hash list: The keyword is larger in whole domain and may collide.
How to solve the collision:
1 Find the appropriate hash function. The hash function is carefully selected, and the possible collisions are still resolved, and the method is not quite thorough.
2) Link method (Implementation)
Given a hash-list t that holds n elements and has m slots, defines the load factor a=n/m of T, that is, the average number of elements stored in a chain.
The probability of any element being hashed into an M slot is the same, and independent of where other elements have been hashed out.
This is called a simple consistent hash. A successful lookup is O (1+a), a=n/m, insert delete O (1), note A (in the book is Alpha)
3 Open addressing methods (for example, linear open-line addressable hashing)
All elements are stored in a hash table, no linked list (that is, no elements are stored outside the hash list), and the load factor is not greater than 1.
To insert an element, you can continuously examine the items in the hash table until you find an empty slot to place the keyword to be inserted.


Design hash function (algorithm design slightly)
Heuristic method: Multiplication Hash and division hash
Randomization method: Full-domain hashing

The realization of linear open addressable hashing
It is difficult to perform a delete operation on a hash table element, but it can be resolved by:
When removing slot I, set a specific value deleted in slot I instead of nil. Of course, the corresponding Hash-search and Hash-insert must cooperate with the modification.
The lookup time does not depend on Mount factor A. Therefore, in the application that must delete the keyword, the link method is often used to solve the collision.

The following implementation of the deletion method, is written in the next write, is not correct.

File hash.h #ifndef hashtable_ #define Hashtable_ #include <iostream> #include <stdlib.h> #include "xcept.
H "using namespace std;
      Template<class E, Class k> class HashTable {public:hashtable (int divisor = 11);
      ~hashtable () {delete [] HT; delete [] empty;}
      BOOL Search (const k& K, e& E) const;
      hashtable<e,k>& Insert (const e& E);
      hashtable<e,k>& Delete (e& E);
      void output (),//Output the hash table Private:int hsearch (const k& K) const; int D; hash function divisor E *ht; Hash table array bool *empty;

1D array};
   Template<class E, class k> hashtable<e,k>::hashtable (int divisor) {//constructor.

   D = divisor;
   Allocate hash table Arrays HT = new E [D];

   empty = new bool [D];
Set all buckets to empty for (int i = 0; i < D; i++) empty[i] = true; Template<class E, class k> int Hashtable<e,k>::hseArch (const k& K) Const {//Search an open addressed table.
 Return location of K if present.
   Otherwise return inserts point if there are space. int i = k% D;     Home bucket Int j = i;
      Start at home bucket do {if (Empty[j] | | | ht[j] = = k) return J;  j = (j + 1)% D; Next Bucket} while (J!= i);

   Returned to home?  Return J; Table Full} Template<class E, class k> bool Hashtable<e,k>::search (const k& K, e& e) const {//Pu
 t element that matches K in E.
   return False if no match.
   int B = Hsearch (k);
   if (Empty[b] | | | ht[b]!= k) return false;
   e = Ht[b];
return true; } template<class E, class k> hashtable<e,k>& hashtable<e,k>::insert (const E& E) {//Hash table
   Insert. K k = e;

   Extract key int b = Hsearch (k);
                  Check if insert is to being done if (Empty[b]) {empty[b] = false;
                  HT[B] = e;

   return *this;} No INSERT, check IF Duplicate or full if (ht[b] = k) throw badinput (); Duplicate throw Nomem (); Table Full} Template<class E, class k> hashtable<e,k>& hashtable<e,k>::D elete (e& E) {//has
   H table Insert. K k = e;

   Extract key int b = Hsearch (k); The check if insert is to being done if (Empty[b]) {cout << does not have this element.
       "<< Endl;
   return *this;
   } Empty[b] = true;
   int count = 0;
   int index = b+1;
       while (index!= b) {if (index >= d) Index-= D;
       K key;
           if (!empty[index]) {key = Ht[index];
       if (key% D)!= index) count++;
   } index++;
   } cout << "count=" << count << Endl;
   E *temp = new E[count];
   index = b+1;
   int i = 0;
       while (index!= b) {if (index >= d) Index-= D;
       K key;
    if (!empty[index] && i < count) {key = Ht[index];       if ((key% D)!= index) {temp[i] = Ht[index];
               Empty[index] = true;
           i++;
   }} index++;
   for (i = 0;i < count;i++) Insert (Temp[i]);
   Delete[] temp;
return *this; Template<class E, class k> void Hashtable<e,k>::output () {for (int i = 0; i< D; i++) {if (empt
      Y[i]) cout << "Empty" << Endl;
else cout << Ht[i] << Endl;}
 } #endif
Xcept.h file
Exception classes for various error types

#ifndef xcept_
#define XCEPT_

#include <new>

Itializers
class Badinitializers {public
   :
      badinitializers () {}
};

Insufficient memory
class Nomem {public
   :
      Nomem () {}
};

Change new to throw Nomem instead of xalloc
void My_new_handler ()
{throw
   nomem ();

New_handler old_handler_ = Set_new_handler (My_new_handler);
Set_new_handler (My_new_handler);
Improper array, find, insert, or delete index
//or deletion from empty structure
class Outofbounds {
   pu Blic:
      outofbounds () {}
};

Use when operands should have matching the size
class Sizemismatch {public
   :
      Sizemismatch () {}
};
  //use when zero is expected
class Mustbezero {public
   :
      Mustbezero () {}
};
Use when zero is expected
class Badinput {public
   :
      badinput () {}
};

#endif
Hash.cpp Test Example
Test hash table with linear open addressing #include <iostream> #include "hash.h" class element {friend int
   Main (void);
      Public:operator Long () const {return key;}//private:g++ has a problem with main a friend int data;
Long key;

};
   int main (void) {hashtable<element, long> h (11);
   Element e;
   E.key = 80;
   H.insert (e);
   E.key = 40;
   H.insert (e);
   E.key = 65;
   H.insert (e);
   H.output ();
   E.key = 58;
   H.insert (e);
   E.key = 24;
   H.insert (e);
   cout << ' << Endl;
   H.output ();
   E.key = 2;
   H.insert (e);
   E.key = 13;
   H.insert (e);
   E.key = 46;
   H.insert (e);
   E.key = 16;
   H.insert (e);
   E.key = 7;
   H.insert (e);
   E.key = 21;
   H.insert (e);
   cout << ' << Endl;
   H.output ();
 E.key = 99;
   try {H.insert (e);}
   catch (Nomem) {cout << "No memory for" << Endl;}
   cout << "Delete" << Endl;
   E.key = 24;
   H.delete (e); H.output ();
   cout << "Delete 15,there is not the element" << Endl;
   E.key = 15;
   H.delete (e);
   H.output ();
   E.key = 2;
   H.delete (e);
   H.output ();
return 0;
              }
Makefile file
HASH:HASH.O hash.o xcept.h
        g++-o hash hash.o hash.o:hash.cpp hash.h xcept.h
g++-
        c hash.cpp hash.h xcept.h
  g++-C hash.h
        g++-C xcept.h Clean
:
        rm-f Hash *.O *.gch


Output results:
Empty
empty
empty
empty
empty
empty
 
-Empty empty Empty
empty
empty
empty empty
empty
 
7
2
 memory No
-for-delete
count=7
empty
2 km/
7
The delete 15,there
is not the element does not have it.
Empty
2/
7
count=6
21
empty
7
empty
65



Link Method implementation














Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: