Check the java source code of the set data structure.

Source: Internet
Author: User

Check the java source code of the set data structure.

You can see a question on the Internet:

A set of strings. The format is as follows:. Merging a set whose intersection is not empty requires that there be no intersection between the merged sets. For example, the preceding example should be output.

(1) Describe your solution to this problem;

(2) provides the main processing procedures, algorithms, and complexity of algorithms;

(3) describe possible improvements.

One of the solutions is to use and check the Set (the data structure is in, but you have forgotten to do so). Therefore, Baidu has made a reference to a blogger's

Article http://blog.csdn.net/dm_vincent/article/details/7655764, the idea is very clear, but in the code implementation is not general application, in order to review, with java generic Implementation of A and query set structure, can be used for integer, string and other collection and custom data types.


Union_Find class:

Package com. algorithms; import java. util. hashMap;/*** and check the set. The data structure * adopts generic programming * and is implemented by pointing to the parent node, that is, map. the get (key) value is the key of the parent node. The key and value of the root node are equal, it is also an entry for querying sets * @ author "zhshl" * @ date2014-10-20 **/public class Union_Find
 
  
{Private HashMap
  
   
UnionMap = new HashMap
   
    
(); // Record the relationship between nodes private HashMap
    
     
UnionSize = new HashMap
     
      
(); // The size of a certain tree. The size of a specific query set is private int count = 0; /// check the number of sets/*** obtain and query the total number of sets * @ return */public int getCount () {return count ;} /*** return the value of the root of the query set where the key belongs. If the key does not exist, NULL * @ param key * @ return */public T find (T key) is returned) {if (! UnionMap. containsKey (key) {return null;} T value = unionMap. get (key); while (! Value. equals (key) {// if the key and value are not equal, it is not the root node // path compression, and the parent node of the node points to its grandfather node unionMap. put (key, unionMap. get (unionMap. get (key); key = value; if (! UnionMap. containsKey (key) {return null;} value = unionMap. get (key);} return value;}/*** add key1 and key2 to the query set * @ param key1 * @ param key2 */public void union (T key1, T key2) {// if (! UnionMap. containsKey (key1) {unionMap. put (key1, key1); unionSize. put (key1, 1); count ++;} if (! UnionMap. containsKey (key2) {unionMap. put (key2, key2); unionSize. put (key2, 1); count ++;} T root1 = find (key1); T root2 = find (key2); if (root1.equals (root2 )) {// if it already belongs to the same query set, return;} if (unionSize. get (root1)> unionSize. get (root2) {// when the first node contains many nodes, point the second root node to the first root node, modify the corresponding node, and check the large and small unionMap. put (root2, root1); int size = unionSize. get (root1) + unionSize. get (root2); unionSize. put (root1, size);} else {unionMap. put (root1, root2); int size = unionSize. get (root1) + unionSize. get (root2); unionSize. put (root2, size);} count --; // reduce the total number of queried sets by one }}
     
    
   
  
 


Test code:

package com.algorithm.test;import com.algorithms.Union_Find;public class Union_Find_Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubUnion_Find
 
   uf=new Union_Find
  
   ();uf.union("s1", "s2");uf.union("s3", "s4");uf.union("s1", "s3");uf.union("s5", "s6");uf.union("s6", "s7");String str1=uf.find("s5");String str2=uf.find("s2");/*Union_Find
   
     uf2=new Union_Find
    
     ();uf2.union(1, 2);uf2.union(3, 2);uf2.union(4, 2);uf2.union(5, 2);uf2.union(3, 5);uf2.union(2, 4);*/System.out.println(str1+":::::"+str2);}}
    
   
  
 







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.

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.