Introduction to the algorithm of the ten (11 chapters Hash list 11.1-4 large Array for direct addressing method of the dictionary operation)

Source: Internet
Author: User

11.1-4 topics:


We want to implement a dictionary on a very large array by using direct addressing. Initially, the array may contain some useless information, but it is not practical to initialize the entire array because the array is too large in size. Give a way to implement a direct addressing dictionary on a large array. Each storage object occupies an O (1) space, the SEARCH, Inseart, delete operations are all O (1), and the time to initialize the data structure is O (1). (Tip: You can take advantage of an additional array, which is treated like a stack, whose size equals the number of keywords actually stored in the dictionary to help determine whether a given item in a large array is valid).



idea:


Because large arrays are too large to initialize, we do not know exactly where there is real data, so the data cannot be stored in a large array, because you do not know exactly where the data is.


Here the way is: to store the data on the stack, the stack of additions and deletions can be achieved O (1), and then on the large array, corresponding to the position of key elements, the corresponding subscript on the stack, so that the key to the large array to find the subscript, and then according to the subscript of the stack can find the key value corresponding to the data


Then, it is also necessary to solve the problem of how to determine whether the data is valid, this is also very simple, after the above search process, it is not difficult to find that if the data is valid, the following conditions need to be met:


1, the key value corresponds to the value of the position in the large array, must be located in the [0, the stack top position], otherwise it is certainly not the data2, to meet the 1th, we go to the corresponding position on the stack, find the element data, its key value is in turn equal to our original key value, otherwise it means that the data does not exist. You can see the Isexist function in the code below .



Data element type: _node.h


Class Node {public:int key;node (int key): Key (Key) {}node (): Key (-1) {}};



Stack class, storing real data: _stack.h


#include <iostream> #include "_node.h" using namespace std;/* * is used to hold data stacks, using the singular group, where the data is a node type, containing a key value */class Stack {int size;public:int top;node* array; Stack (int size): size (size), Top ( -1) {array = new node[size];} ~stack () {//Do some memory recovery work delete[] array;} Enter stack void push (int* hash, int key) {if (top = = size-1) {cout << "error:stackoverflow" << Endl;return;} Top++;array[top].key = key;//The element in the hash array corresponding to the key position is linked to the top position element on the stack hash[key] = top;} Out stack int pop (int* hash, int key) {if (top = =-1) {cout << "error:stackunderflow" << endl;return-1;} int tmp = array[top].key;top--;//Update the hash table so that it equals-1, because the stack array subscript cannot be-1, convenient to judge hash[key] = -1;return tmp;} void Travel () {if (Top < 0) {return;} int tmp = top;while (tmp >= 0) {cout << array[tmp--].key << ';} cout << Endl;} /* * Swap the value of the POS position with the value at the top of the stack */void swaptop (int* hash, int key) {int pos = (hash) [key];//update hash list Hash[array[top].key] = Pos;hash[arra Y[pos].key] = top;//interchange operation, update stack node TMP = array[top];array[top] = Array[pos];array[pos] = tmp;}}; 



Demo.cpp, contains the hash class:


#include <iostream> #include "_stack.h" using namespace Std;class Hash {public:int* hasharray;//For storing an array of positions in the stack, The array subscript corresponds to the key value stack* s; Stack/construct hash (int hashsize, int stackSize) that holds the real data: Hasharray (), S () {Hasharray = new int[hashsize];s = new stack (stackSize); }//destructor ~hash () {delete[] hasharray;delete s;} function bool Isexist (int* hash, int key) {if (Hash[key] <= s->top && hash[key] >= 0&& ke to determine if the key value already exists y = = S->array[hash[key]].key) {return true;} cout << "key does not exist!" << Endl;return false;} Insert a data void insert (int key) {S->push (This->hasharray, key);} Delete a data void Delete_ (int key) {//To determine if there is an if (!isexist (This->hasharray, key)) {return;} The data of the position on the corresponding stack is exchanged with the stack top data, and the values in the hash array are flushed to point to the correct stack array element s->swaptop (This->hasharray, key);//stack, flush the values in the hash array s-> Pop (This->hasharray, key);} Find out if the key value is already included node* search (int key) {if (!isexist (This->hasharray, key)) {return NULL;} else {return S->array + hash Array[key];}} Traversal of the contained element void travel () {int TMP = s>top;while (tmp >= 0) {cout << s->array[tmp--].key << ';} cout << Endl;}}; the int main () {///test uses a hash array size of 1000, and the stack size for the data is 100hash* hash = new Hash (+ +); cout << Hash->search (555) << Endl;hash->insert (555); Hash->insert (444); Hash->insert (333); Hash->travel (); Hash->delete_ (555); Hash->travel (); cout << Hash->search (333)->key << Endl;return 0;}







Introduction to the algorithm of the ten (11 chapters Hash list 11.1-4 large Array for direct addressing method of the dictionary operation)

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.