JAVA data structure and algorithms (hash table) __java

Source: Internet
Author: User
What is a hash table.

A hash table is a data structure that provides fast insertion and lookup operations.

Advantages:

Inserts, finds, deletes the time level is O (1), the data item occupies the hash table long half, or two-thirds, the hash table performance is best.

disadvantage: based on an array, the array is difficult to expand after the creation of some of the hash table is basically filled with a very serious performance degradation; there is no simple way to traverse the entire data item in any order, such as from small to large;

Purpose: There is no need to traverse the data and predict the size of the data in advance, at which point the hash table is unmatched in speed and ease of use. Hashiha

is to convert the keyword into an array subscript, which is done through the hash function in the hash table. Hash functions are not required for hashing for some keywords, such as employee numbers.

hash Function: the effect is to convert a large, multiple-integer range into an array 's subscript range. Generally by taking the remainder of the completion. Key%arraysize.

conflicts: hash functions do not guarantee that each keyword is mapped to the location of an array of empty elements. The method of resolving conflicts is divided into: Open address method and chain address method. Open Address Law

If the data cannot be placed directly on a cell that is caused by an array of arrays computed by the hash function, it is necessary to look for other locations of the array . The method gathers, and the data items that are hashed in the aggregation are moved step-by-step, and inserted at the end of the aggregation, making the aggregation more and more large. The bigger the gathering, the faster it grows. Linear detection

In the linear detection, the linear search for blank units, such as 52 of the position is occupied, find 53, 54 ... Position until you find an empty space.

aggregation: A continuous series of filled cells is called a fill sequence. When you add more and more data items, the fill sequence becomes longer, which is called aggregation.

If you fill out a hash table too full, it will take a long time to fill in each of the data.

Package Cn.xyc.dataStructure.hash;

/**
 * 
 Description: Data item stored in hash table * * 
 <pre>
 * HISTORY *
 ****************************************
 *  ID   DATE           person          REASON
 *  1    October 2, 2016        80002253         Create
 * *******************************************************************
 * </pre> * * 
 @author Mntze D. *
 @since 1.0/
 public
class DataItem {
	private int item;//data item key public

	DataItem (int item) {
		This.item = Item;
	}

	public int Getkey () {return
		item;
	}
}


Package Cn.xyc.dataStructure.hash; /** * Description: Using linear detection method to implement hash table * * <pre> * HISTORY * ************************************************************** * ID DATE Person REASON * 1 October 2, 2016 80002253 Create * 
 * </pre> * * @author Mntze D. * @since 1.0
	*/public class Hash {//Save data in hash table private dataitem[] Hasharray;
	The length of the hash table is private int arraysize;

	Hash items that can be deleted private DataItem nonitem;
		/** * Initialize HASH table * * @param size */public hash (int size) {arraysize = size;
		Hasharray = new Dataitem[arraysize];
	 Nonitem = new DataItem (-1);//Deleted entry is-1}////////////////////////////////////////////////////////////** * traversal hash table
		*/public void displaytable () {System.out.print ("Table:"); for (int i = 0; i < arraysize i++) {if (hasharray[i]!= null) {System.out.print (Hasharray[i].getkey () + "\ T")
			; } else {
				System.out.print ("**\t");
	} System.out.println (""); }////////////////////////////////////////////////////////** * Hash function * @param key * @return/public in
	T Hashfunc (int key) {return key% ArraySize; }////////////////////////////////////////////////////////public void Insert (DataItem item) {int key = Item.getkey
		();

		int hashval = Hashfunc (key); while (Hasharray[hashval]!= null && hasharray[hashval].getkey ()!=-1) {++hashval;//until a blank element is found Hashval%= a
	rraysize;//guaranteed not to cross over}//Insert data hasharray[hashval] = Item;
		}////////////////////////////////////////////////public DataItem Delete (int key) {int hashval = Hashfunc (key); 
				while (Hasharray[hashval]!= null) {if (Hasharray[hashval].getkey () = = key) {DataItem temp = Hasharray[hashval];
				Hasharray[hashval] = Nonitem;
			return temp;
			} ++hashval;
		Hashval%= arraysize;
	return null; }//////////////////////////////////////////////public DataItem find (int key) {int hashval = Hashfunc (key);
			while (Hasharray[hashval]!= null) {if (Hasharray[hashval].getkey () = = key) {return hasharray[hashval];
				else {++hashval;
			Hashval%= arraysize;
	} return null; 
	}/////////////////////////////////////////////////////////** * Get a length of the array length of prime number * * @param min * @return * * @SuppressWarnings ("unused") private int getprime (int min) {for (int i = 0; true; i++) {if (IsPrime (i)) {RET
			Urn i; }}//////////////////////////////////////** * To determine whether a number is prime * * @param n * @return/private Boolean
			IsPrime (int n) {for (int i = 2; I * i < n; i++) {if (n% i = = 0) {return false;
	} return true;
 }

}





Extended Array
Hash functions calculate the size of a given data item based on the size of the array, so it is not easy to copy data from one array to another. You need to insert each data item into the new array by iterating through the old array, using the Insert method.

two times Detection

Two attempts to prevent aggregation, the thought history of the detection of a distant cell, rather than the original position adjacent to the unit .

The step is the square of the number of steps:x+1^2, x+2^2, x+3^2, not x+1, x+2.

But when the search for two probes becomes longer, it seems as if it is becoming more and more hopeless, because the step-square growth may fly out of the space. result in two times of aggregation.


again Hashifa

is a sequence of probes that rely on keywords, not each keyword.

The reason for the two-time aggregation is that the detection sequence of the two-detection algorithm is always fixed 1,4,9,16.

The method is to hash the keyword in a different hash function, using this as the step, and the length of the specified keyword is invariant throughout the probe, although the different keywords use different step lengths.

The second hash function is characterized by a difference from the first hash function. Cannot output 0 (otherwise there will be no step size: Each probe is in situ, the algorithm will fall into a dead loop).

Experts found that the hash function in the following form works very well:

stepsize=constant-(key%constant), where constant is a prime number and is less than the array capacity. such as stepsize=5-(key%5);

Package Cn.xyc.dataStructure.hash;
 /** * * Description: Use the hash method to achieve the hash table <br/> * Requirements: The capacity of the table is a prime number, otherwise the hash may be taken to 0 resulting in the algorithm crash.            * * <pre> * HISTORY * **************************************************************************** * ID DATE Person REASON * 1 October 2, 2016 80002253 Create * **********************************  * </pre> * * @author Mntze D. * @since 1.0/public class hashdouble
	{//Save data in hash table private dataitem[] Hasharray;
	The length of the hash table is private int arraysize;

	Hash items that can be deleted private DataItem nonitem;
		/** * Initialization Hash Table * * @param size */public hashdouble (int size) {arraysize = size;
		Hasharray = new Dataitem[arraysize];
	 Nonitem = new DataItem (-1);//Deleted entry is-1}////////////////////////////////////////////////////////////** * traversal hash table
		*/public void displaytable () {System.out.print ("Table:");
				for (int i = 0; i < arraysize i++) {if (hasharray[i]!= null) {System.out.print (Hasharray[i].getkey () + "T");
			else {System.out.print ("**\t");
	} System.out.println (""); }////////////////////////////////////////////////////////public void Insert (DataItem item) {int key = Item.getkey
		();
		int hashval = HASHFUNC1 (key);
		int stepsize = HASHFUNC2 (key); while (Hasharray[hashval]!= null && hasharray[hashval].getkey ()!=-1) {hashval = stepsize;//until a blank element ha is found
	Shval%= arraysize;//guaranteed not to cross over}//Insert data hasharray[hashval] = Item;
		}////////////////////////////////////////////////public DataItem Delete (int key) {int hashval = HASHFUNC1 (key);
		int stepsize = HASHFUNC2 (key);
				while (Hasharray[hashval]!= null) {if (Hasharray[hashval].getkey () = = key) {DataItem temp = Hasharray[hashval];
				Hasharray[hashval] = Nonitem;
			return temp;
			} Hashval + = stepsize;
		Hashval%= arraysize;
	return null; }//////////////////////////////////////////////public DataItem find (int key) {
		int hashval = HASHFUNC1 (key);
		int stepsize = HASHFUNC2 (key);
			while (Hasharray[hashval]!= null) {if (Hasharray[hashval].getkey () = = key) {return hasharray[hashval];
				else {hashval + = stepsize;
			Hashval%= arraysize;
	} return null; 
	}/////////////////////////////////////////////////////////** * Get a length of the array length of prime number * * @param min * @return * * @SuppressWarnings ("unused") private int getprime (int min) {for (int i = 0; true; i++) {if (IsPrime (i)) {RET
			Urn i; }}////////////////////////////////////////////////////////** * Hash function * @param key * @return/PU
	Blic int hashFunc1 (int key) {return key% ArraySize; }////////////////////////////////////////////////////////** * Hash function: Generate Step Size * * @param key * @return/Pub
	LIC int HASHFUNC2 (int key) {//The remainder of the hash must be less than the length of the array, not the same as hF1//cannot be 0//is a prime number return 5-(key% 5); 
	}//////////////////////////////////////** * To determine whether a number is prime * * @param n * @return/private Boolean isprime (int n) {for (int i = 2; I * i < n; i++) {if (n% i = = 0) {
			return false;
	} return true;
 }

}


Link Address method

Set the list in each cell in the hash table. The keyword of a data item is still a cell that maps to a hash table as usual, and the data item itself is inserted into the list of the cells. Other data items that map to this location only need to be added to the list and do not need to look for empty spaces in the original array.

pros and Cons:

Ordered lists do not speed up successful searches, but can reduce half of the time in unsuccessful searches.

The deletion time level is also reduced by half

The insertion time is prolonged because the data item cannot be inserted only in the header, and the correct position in the ordered table must be found before inserting.

Package Cn.xyc.dataStructure.hash;

/**
 * 
 Description: Nodes in the list
 * 
 * <pre>
 * HISTORY *
 ********************************************
 *  ID   DATE           person          REASON
 *  1    October 2, 2016        80002253         Create
 * ****************************************************************************
 * </pre>
 * 
 * @author Mntze · D.
 * @since 1.0/
 public
class Linkitem {
	private int data;//List of data items public
	Linkitem nextlink;//next node public

	linkitem (int data) {
		this.data = data;
	}

	public int Getkey () {return
		data;
	}

	public void Dispalylink () {
		System.out.println (data + "\ T");
	}


Package Cn.xyc.dataStructure.hash; /** * Description: From small to large ordered list * * <pre> * HISTORY * ***************************************************************** * ID DATE Person REASON * 1 October 2, 2016 80002253 Create * ********* * </pre> * * @author Mntze D. * @since 1.0 *

	/public class Sortlink {//The first element of the list is private linkitem;
	Public Sortlink () {i = null;
		}///////////////////////////////////////public void Insert (Linkitem thelink) {int key = Thelink.getkey ();
		Linkitem previousitem = null;
		Linkitem CurrentItem = First from the very beginning;
			while (CurrentItem!= null && key > Currentitem.getkey ()) {previousitem = CurrentItem;
		CurrentItem = Currentitem.nextlink;
		} if (Previousitem = = null) {a = Thelink;
			else {previousitem.nextlink = Thelink; Thelink.nextlink = CUrrentitem;
		}///////////////////////////////////////////public void Delete (int key) {Linkitem previous = null;
		Linkitem current = i;
			While (the current!= null && current.getkey ()!= key) {previous = current;
		current = Current.nextlink;
			} if (previous = = null) {//description is the first if (!= null) {A/First.nextlink;
		} else {previous.nextlink = Current.nextlink; }//////////////////////////////////////////////////public linkitem find (int key) {Linkitem CurrentItem = i
		; while (CurrentItem!= null && currentitem.getkey () <= key) {if (Currentitem.getkey () = = key) {return C
			Urrentitem;
		} CurrentItem = Currentitem.nextlink;
	return null;
		}///////////////////////////////////////////////public void Displaylist () {Linkitem currentitem=first;
			while (Currentitem!=null) {currentitem.dispalylink ();
		Currentitem=currentitem.nextlink;
	} System.out.println ();
 }

}

Package Cn.xyc.dataStructure.hash; /** * Description: Use chain address method to implement hash table * * <pre> * HISTORY * *************************************************************** * ID DATE Person REASON * 1 October 2, 2016 80002253 Create * *******
 * </pre> * * @author Mntze D. * @since 1.0
	*/public class Hashlink {//Save the data in the hash table private sortlink[] Hasharray;
	The length of the hash table is private int arraysize;

	Hash items that can be deleted private Sortlink nonitem;
		/** * Initialization Hash Table * * @param size */public hashlink (int size) {arraysize = size;
		Hasharray = new Sortlink[arraysize];
		Populate array for (int i = 0; i < hasharray.length i++) {Hasharray[i] = new Sortlink (); }/////////////////////////////////////////////////////////////** * traversal HASH table */public void displaytable () {Sy
		Stem.out.print ("Table:"); for (int i = 0; i < arraysize i++) {if (hasharray[i]!= null) {
				Hasharray[i].displaylist ();
			else {System.out.print ("**\t");
	} System.out.println (""); }////////////////////////////////////////////////////////** * Hash function * @param key * @return/public in
	T Hashfunc (int key) {return key% ArraySize; }////////////////////////////////////////////////////////public void Insert (Linkitem item) {int key = Item.getkey
		();
		int hashval = Hashfunc (key);
	Insert Data Hasharray[hashval].insert (item);
		}////////////////////////////////////////////////public void Delete (int key) {int hashval = Hashfunc (key);
	Hasharray[hashval].delete (key);
		}//////////////////////////////////////////////public linkitem find (int key) {int hashval = Hashfunc (key);
		Linkitem item = hasharray[hashval].find (key);
	return item; 
	}/////////////////////////////////////////////////////////** * Get a length of the array length of prime number * * @param min * @return * * @SuppressWarnings ("unused") private int getprime (int min) {
		for (int i = 0; true; i++) {if (IsPrime (i)) {return i; }}//////////////////////////////////////** * To determine whether a number is prime * * @param n * @return/private Boolean
			IsPrime (int n) {for (int i = 2; I * i < n; i++) {if (n% i = = 0) {return false;
	} return true;
 }

}






Barrel

Another approach is similar to the link-address method, which uses an array in each cell of the hash table, not a linked list. Such an array is called a bucket.

disadvantage: bucket capacity is too small will overflow, too much waste of space.



the comparison between open address method and chain address method:

unknown number of items: use chain address method.







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.