hashing : You can quickly insert or delete data after the hash table is implemented. However, it is very inefficient to implement a lookup operation.
The bottom of the hash table is the array implementation, the length is pre-set, can be increased according to demand at any time. All elements are stored in a specific position based on the key corresponding to the element. When you use a hash table to store data, the key value is mapped to a number by a hash function, and the number range is 0-the length of the hash table.
Collision (collision): The hash function in the ideal state maps each key value into a unique array index. But the number of kin is infinite, and the length of the array is limited, and a more realistic goal is to have the hash function map the keys evenly to the array as much as possible. Even with an efficient hash function, there is still a possibility that two keys are mapped to the same value. This is called a collision. When it comes into being, we're going to solve it.
How big is the array in the hash table?
Common settings: Array length is a prime number.
functionHashTable () { This. Table =NewArray (137); This. Simplehash = Simplehash;//hash Function This. Betterhash = Betterhash;//Honahasi function This. buildchains = Buildchains;//Open Connection Method This. Showdistro = Showdistro;//displaying data in a hash table This. ShowDistro1 = ShowDistro1;//open hyphen to display data from a hash table This. put = put;//storing data in a list //this.newput = newput;//data is stored in the list This. get =get; } //hash Function: A simple method of removing remainder functionSimplehash (data) {varTotal = 0; for(vari = 0;i<data.length;i++) { Total+ = Data.charcodeat (i);//method to return the Unicode encoding of the character at the specified position. This return value is an integer between 0-65535. Add to get the hash value. } //Console.log (total+ "======"); returntotal% This. table.length;//returns a value in addition to the remainder method } function put (data) {var _pos = This.simplehash (data),//Index to array//var _pos = This.better Hash (data);//Index to array this.table[_pos] = data;//stores data based on the value returned as a key value. } functionShowdistro () {//var n = 0; for(vari = 0;i < This. table.length;i++) { if( This. table[i]! =undefined) {Console.log (i+": "+ This. Table[i]); //document.write (i+ ":" +this.table[i]+ "<br>"); } } } varhash =NewHashTable (); varBook = ["JavaScript", "JQuery", "PHP", "Java", "Go", "HTML5", "AI"]; for(varI =0;i < book.length;i++) {hash.put (book[i]); } //Hash.showdistro ();Console.log (book.length);
1:ai
7:java
54:php
56:javascript
77:go
79:html5
92:jquery
The above will be problematic:
1. Data rendering is distributed to both ends.
2. Sometimes there is a problem with incomplete content, (hash function processing with the same hash value, resulting in a collision)
hash function Design: collision resolution
Make sure that the hash table is used to store the data in an array whose size is a prime number (the remainder repetition rate is low when mathematical operations are taken to the remainder). The length of the array should be greater than 100, making it more evenly distributed.
Horner algorithm:
The new hash function still uses the ASCII value of the computed string first, but multiplies each time by a prime number when summing. Suggest a smaller, (31,37);
functionBetterhash (str) {varH = 37; varTotal = 0; for(vari = 0;i < str.length;i++) { Total+ = h*total+str.charcodeat (i); } Total= Total% This. Table.length; if(Total < 0) { Total+= This. table.length-1; } returnparseint (total); } varHASH2 =NewHashTable (); varBook2 = ["JavaScript", "JQuery", "PHP", "Java", "Go", "HTML5", "AI"]; for(vari = 0;i < book2.length;i++) {hash2.put (book2[i]); }
Hash integer key:
The above two examples are for manipulating the key values of the string type, and now study the data manipulation of the integer key.
Eg: Student's score query, randomly generate a 9-digit key (ID) to identify student identities and a score.
//generate student Scores: functionGetrandomgrade (Min,max) {returnMath.floor (Math.random () * (max-min+1)) +min; } //Build ID: functionGetstuid (arr) { for(vari = 0;i < arr.length;i++) { varnum = ""; for(varj = 0;j < 9;j++) {num+=math.floor (Math.random () *10); } num+ = Getrandomgrade (50,100); Arr[i]=num; } } varStunum = 10; varStudents =NewArray (Stunum); Getstuid (students); for(vari = 0;i < students.length;i++) { //Console.log (Students[i]);Console.log (students[i].substring (0,8) + "" +students[i].substring (9)); } varHtab =NewHashTable (); for(vari = 0;i < students.length;i++) {htab.put (students[i]); } htab.showdistro (); //if the same solution with the first hash function will result in collisions, a better hash function will not cause collisions.
//sort the hash list, take the value: //to redefine put (), get (); //Add method with key value functionNewput (key,data) {var_pos = This. Betterhash (key); This. table[_pos] =data; } //Get method: functionget (key) {return This. table[ This. Betterhash (key)]; } //test: Enter three person's name and phone number to query, three input after output quit to query varNumbers =NewHashTable (); varName,number; for(vari = 0;i < 3;i++) { //name = prompt ("Please enter Name:"); //number = prompt ("Please enter Phone:");Numbers.newput (Name,number); } name= ""; Prompt ("Enter quit end:"); while(Name! = "Quit") {Name= Prompt ("Inside Member:"); if(name = = "Quit") { Break; } Alert (Name+ "Member of the phone is:" +numbers.get (name)); Prompt ("Enter quit end:"); }
Collision handling: Open chain and linear detection
Open chain method: The key is also stored in the index position computed by the hash function, but it is not possible to store multiple copies of the data in an array cell. Each element in the underlying array that implements the hash table is a new data structure, even though the same values are stored in the same position after using the two key three columns, but they do not have the same position in the second array.
Implementation method: Create an empty array by calling a function when creating an array that stores the hash's key values, and then assign the array to each array in the hash table. This creates a two-dimensional array.
functionBuildchains () { for(vari = 0;i < This. table.length;i++) { This. table[i] =NewArray (); } } //the corresponding display elements also have to be changed functionShowDistro1 () {//var num = 0; for(vari = 0;i < This. table.length;i++) { if( This. table[i][0]! =undefined) {Console.log (i+ ":" + This. Table[i]); } } } //put functionput (key,data) {varpos = This. Simplehash (key); varindex = 0; if( This. table[pos][index] = =undefined) { This. table[pos][index] =data; }Else { while( This. table[pos][index]! =undefined) { ++index; } This. table[pos][index] =data; } } //Get functionget (key) {varindex = 0; varpos = This. Simplehash (key); if( This. table[pos][index] = =key) { return This. Table[pos][index]; }Else { while( This. table[pos][index]! =key) { ++index; } return This. Table[pos][index]; } returnundefined; } //Test: varHtable =NewHashTable (); Htable.buildchains (); varArrname = ["David", "Jennifer", "Donnie", "Raymond", "Cynthia", "Mike", "Clayton", "Danny", "Jonathan"]; for(vari = 0;i < arrname.length;i++) {htable.put (arrname[i]); } htable.showdistro1 ();
Hash of the js--data structure