Algorithm introduction 11.2-4

Source: Internet
Author: User

For more details, see click to open the link. Here, I will only talk about my views.

This is based on slots. I think it is very helpful to establish a good mathematical model in your mind.

Each slot is defined as a node {key, flag, pre, next}

You need a free linked list and a single-chain table. The header is free = 0;

Define two functions that operate the free linked list. This is easy to think of: removing a node from the free linked list removefromfree (slot object), or) add the slot object to the free linked list freetofree.

The principle of the removefromfree function is that the node to be removed from the free table is a [H], and its Pre and next nodes are located. A [H]. pre next points to next of a [H... A [H]. the PRE of next points to the PRE of a [H. in this way, a [H] can be isolated. return the index H of a [H. (It is a small detail to judge whether next and pre exist in the middle. You can adjust them slowly)

Freefromfree: how to release a node to a free linked list: Insert a [H] to the beginning of the linked list, and set the key of a [H] to-1 and pre =-1.

Search x maps X to the slot H through hash. If the slot is empty or the keyword that originally occupies this slot does not belong to this slot, an error is found. Why? This is hard to understand. It needs to be combined with insert,

Because the insert operation is performed, if the slot to be inserted is null, it will be inserted directly. If the slot to be inserted is not empty and the keyword occupying this slot also belongs to this slot, that is, two keywords with the same priority conflict with each other in the same slot. The processing policy is that the old keyword is used to randomly find a slot and insert it to the new value, then the new keywords are linked to the linked list. If the slot to be inserted is not empty and the keyword occupying this slot does not belong to this slot, that is, the priority of the new keyword is higher than that of the previous keyword, therefore, the previous keyword is randomly inserted into an empty slot, and the slot is left to the New Keyword with a higher priority. Its next and pre are set to-1. that is, it is the first one.

There is a rule in it. for search, if X reaches a slot through hash, if X exists, then the original keyword y of this slot must belong to this slot, and X belongs to the queue starting with Y. Therefore, we can determine whether the original keyword of this slot belongs to this slot. If it does not belong to this slot, errors will inevitably occur.

Extension: If X reaches a slot through hash, it reaches the beginning of a queue. The keyword at the beginning of this queue must belong to this slot.

The next step is to search for this queue and output the slot where the keyword X is located.

Insert X: Search X first. If yes, it will be returned and cannot be inserted again. Otherwise, continue. It is relatively simple to use hash to set X to H slot. If the slot is empty, take h directly from the free table, and set pre and next to-1, insert X into the key. If the slot is not empty and the keyword that occupies the slot has the same priority as X (that is, they all belong to this slot), the old key will stay in the slot, leave a slot for the new value. If the slot is not empty and X has a higher priority than the original keyword (that is, the original keyword occupies the site of X), you can find a new slot, move the original keyword to the new slot to free up the original slot for X.

Delete X: Similarly, search X first. If X cannot be found, delete cannot be returned.

Search the slots occupied by X first. If the slots of X are different from those occupied by X, this indicates that the slot of array X is occupied by keywords with the same priority. In addition, according to the first come, first served principle above, in the queue, elements in the front of X already occupy the slot of Hash (x), that is, the PRE of X is inevitable. Therefore, you can directly release the slot occupied by X to free.

If the slot location of X is the same as the slot location, and the queue has only one element, that is, next =-1, it can be directly released to free.

If the slot location of X is the same as that of X, and there are more than one element in the queue, it is ideal to release the slot of X to free, A keyword behind X can be used to top the gap of X.

For specific implementation, see the code:

# Include <iostream> # include <time. h ># include <string> using namespace STD; int free = 0; struct node {int key; int flag; int pre; int next ;}; int removfromfree (node *, int h) {A [H]. flag = 1; if (a [H]. PRE> = 0) A [A [H]. pre]. next = A [H]. next; // adjust the Middle Value of the pointer to separate else free = A [H]. next; if (a [H]. next> = 0) A [A [H]. next]. pre = A [H]. pre; return h;} void freetofree (node * a, int h) {A [H]. flag = 0; A [H]. next = free; // insert a [H] to the free table header free = H; A [H]. pre =-1; A [H]. Key =-1;} int Hash (int x) {return X % 20;} int search (node * a, int X) {int H = hash (X ); if (A [H]. flag = 0 | Hash (A [H]. key )! = H) // If the slot is empty, it fails. Keywords in the same queue should have the same priority, all of which are hashed to the same slot, return-1; // If Hash (A [H]. Key )! = H indicates that the key priority of the entire queue is different from that of X, and X cannot appear in this queue int P = h; while (P> = 0 & A [p]. key! = X) P = A [p]. next; if (a [p]. key = x) return P; else return-1;} void insert (node * a, int X) {int ret; ret = search (A, X ); if (Ret> = 0) return; // if the same value exists, int H = hash (x) cannot be inserted. If (A [H]. flag = 0) // If the slot is empty, insert {int T = removfromfree (A, H); A [T]. key = x; A [T]. pre =-1; A [T]. next =-1;} else if (H = hash (A [H]. key) // If the slot value and the value to be inserted have the same priority (both belong to the same slot), the old slot will be placed in another slot, the principle {int T = removfromfree (A, free); A [T] = A [H]; if (a [H]. next> = 0) A [A [H]. next]. pre = T; A [H]. key = X; A [H]. Next = T; // It is easy to draw a [T]. Pre = H;} else if (H! = Hash (A [H]. key) // A [H]. ke occupies the slot location of X, that is, the latecomer belongs to this slot. If the original slot does not belong to this slot, the original value is moved to another slot {int T = removfromfree (, free); A [T] = A [H]; A [A [T]. pre]. next = T; if (a [T]. next> = 0) A [A [T]. next]. pre = T; A [H]. key = x; A [H]. pre =-1; A [H]. next =-1 ;}} void Delete (node * a, int X) {int ret; ret = search (A, x); If (Ret <0) return; // If (ret = H & A [RET]. next =-1) // If the slot queue has only one element, free it directly to the free table {freetofree (A, H );} else if (H = RET & A [RET]. next! =-1) // If the slot queue has more than one element, and the queue header is the element to be deleted. Then, after deleting the element, you need to fill in the subsequent element with this vacancy {// H is the position where X should be, and RET is the actual position where X is int next = A [RET]. next; A [RET] = A [next]; A [A [RET]. next]. pre = ret; freetofree (A, next);} else if (H! = RET) // if the location h of X is different from the actual location ret of X, it means that the location h of X is occupied by another element with the same priority, RET will not be the queue header, that is, pre exists {// you can directly delete a [A [RET]. pre]. next = A [RET]. next; if (a [RET]. next> = 0) A [A [RET]. next]. pre = A [RET]. pre; freetofree (A, RET) ;}} void print (node * A) {cout <"index pre flag key next" <Endl; For (INT I = 0; I <20; I ++) {cout <I <'\ t' <A [I]. pre <'\ t' <A [I]. flag <'\ t' <A [I]. key <'\ t' <A [I]. next <Endl ;}} void main () {node A [20]; for (INT I = 0; I <20; I ++) {A [I]. pre = I-1; A [I]. flag = 0; A [I]. key =-1; A [I]. next = I + 1; if (I = 19) A [I]. next =-1;} string STR; int s; while (true) {CIN> STR; If (STR = "S") {CIN> S; cout <search (A, S) <Endl;} else if (STR = "I") {CIN> S; insert (a, S );} else if (STR = "p") {print (a);} else if (STR = "D") {CIN> S; Delete (, s );}}}

The running result is as follows:

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.