and check the data structure Java source code

Source: Internet
Author: User

See a topic on the Internet:

Given a collection of strings, in the formof:. Requires merging a collection where the intersection is not empty, requiring no overlap between the completed collections, for example, the above example should be output.

(1) Please describe your idea of solving this problem;

(2) give the main processing flow, algorithm, and the complexity of the algorithm;

(3) Please describe the possible improvements.

One solution is to use and check the set, (the data structure has, but has forgotten the embarrassment) so, Baidu a bit, the main reference to a blogger's

The article http://blog.csdn.net/dm_vincent/article/details/7655764, the idea speaks very clearly, but does not have the general application in the code realization, in order to review again, uses the Java generic type to implement a and check the set structure, can be used for integers, strings, and so on collections and custom data types.


Union_find class:

Package Com.algorithms;import java.util.hashmap;/** * and check set, data structure * using generic programming * with the way to the parent node, that is, the value of Map.get (key) is the parent node key, The key and value of the root node are equal, and are also the portals of a set-up * @author "ZHSHL" * @date2014 -10-20 * */public class Union_find<t> {private hashmap<t ,t> unionmap=new hashmap<t, t> (); Record the relationship between nodes private hashmap<t,integer> unionsize=new hashmap<t, integer> ();////the size of a tree, the size of a certain and check set private int count=0;///number of/** * Gets and sets the total number of * @return */public int GetCount () {return count;} /** * Returns the value of the root of the set that the key belongs to and returns null if no key exists * @param key * @return */public t find (t key) {if (!unionmap.containskey (key)) {RE Turn null;} T Value=unionmap.get (key); while (!value.equals (key)) {/////If key and value are not equal, then the root node///path compression, 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,key2 to the collection * @param key1 * @param key2 */public void Union (T key1,t Key2) {/////For first time accession is deposited in map if (!unionmap.containske Y (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 is already part of the same and checked, return directly;} if (Unionsize.get (ROOT1) >unionsize.get (Root2)) {////The first and the collection contains more nodes, let the second tree root node point to the first tree root node, and modify the corresponding and check set size 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--;////Total and the number of check sets decreased by one}}


Test the Code class:

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<string> uf=new union_find<string& gt; (); 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<integer> uf2=new union_find<integer> (); 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);}}







and check the data structure Java source code

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.