Parallel query set of data structure (Java version)

Source: Internet
Author: User
From: http://blog.csdn.net/originalIntention/article/details/8233462
/*** Query the array of the data structure and query the Set * @ author sking */package and query the set. Public class arrayunionfind {static int [] equivclass; // array of elements, valid index from 1 to static int N; // check whether the set is large or small. *** initialize and query the set according to the specified array initial capacity * @ Param numberofelements array initial capacity */static void initialize (INT numberofelements) {n = numberofelements; equivclass = new int [n + 1]; // you can use the Union algorithm to merge for (int e = 1; E <= N; E ++) equivclass [e] = E ;} /*** merge the two specified "Classes" * @ Param classa "Class 1" * @ Param classb "Class 2" */public static void Union (INT classa, int classb) {for (int K = 1; k <= N; k ++) if (equivclass [k] = classb) equivclass [k] = classa ;} /*** return the "class" * @ Param theelement specified Element * @ return "class" */public static int find (INT theelement) of the specified Element) {return equivclass [theelement];}

/*** Query the linked list of the data structure and query the Set * @ author sking */package and query the set; public class initialize listunionfind {/*** linked list and query the node type of the Set * @ author sking */protected class equivnode {/** node [e]. equivclass is not only the value returned by find (E), but also an equivalent * class node [e]. the pointer of the first node in the equivclass chain. * Node [e]. size is defined only when e is the first node in the chain. Table * shows the number of nodes in a class. */INT equivclass; // class mark int size; // number of nodes of the class int next; /*** constructor that specifies the category and number of elements * @ Param theclass class * @ Param thesize number of elements */equivnode (INT theclass, int thesize) {equivclass = theclass; size = thesize ;}} Private Static equivnode [] node; // node array Private Static int N; // check the set size and size. *** initialize and query the Set * @ Param numberofelements and check the set capacity */Public void initialize (INT numberofelements) {n = numberofelements; node = new equivnode [n + 1]; // Initialization, which is a separate class. The number of elements of the class is 1for (int e = 1; E <= N; e ++) node [e] = new equivnode (E, 1);}/*** merge two specified "classes" * @ Param classa "Class 1" * @ Param classb "Class 2" */public static void Union (INT classa, int classb) {// always link a class with fewer elements to the IF (node [classa] in the chain corresponding to more element classes. size> node [classb]. size) {int T = classa; classa = classb; classb = T;} int K; For (k = classa; node [K]. next! = 0; k = node [K]. next) node [K]. equivclass = classb; node [K]. equivclass = classb; node [classb]. size + = node [classa]. size; node [K]. next = node [classb]. next; node [classb]. next = classa;}/*** find the class to which the specified element belongs * @ Param theelement specifies the element * @ return specifies the class to which the element belongs */public static int find (INT theelement) {return node [theelement]. equivclass ;}}

/** Use the tree structure to implement and query the Set * The Tree node contains two fields: parent. The root * parent field is used to indicate the parent node linked to this element. The root parent field is 0. * The root field indicates the root of the class. Only the root of the root node is true. * The array cable corresponding to the root node is marked as the class tag of this class. The array index of the root node * is the class tag to be returned by following the parent link from * element node to be queried. */Package and query set;/** and query set */public class treeunionfind {Private Static class node {int parent; Boolean root; private node () {parent = 1; root = true ;}} node [] node; Public treeunionfind (int n) {node = new node [n + 1]; for (int e = 0; e <= N; E ++) node [e] = new node ();} public int find (int e) {While (! Node [e]. root) E = node [e]. parent; Return e;} // A, B indicates the existing class mark public void Union (int A, int B) {node [A]. parent + = node [B]. parent; node [B]. root = false; node [B]. parent = ;}}

/** Improved query set, supports compression path */package and query set; public class treeunionfind2 {Private Static class node {int parent; Boolean root; private node () {parent = 1; root = true ;}} node [] node; Public treeunionfind2 (int n) {node = new node [n + 1]; for (int e = 0; E <= N; e ++) node [e] = new node ();} /* perform path compression while searching for Categories * to reduce the search time */Public int find (int e) {int current = E, P, GP; if (node [current]. root) Return Current; P = node [current]. parent; If (node [p]. root) return P; Gp = node [p]. parent; while (true) {node [current]. parent = gp; If (node [GP]. root) return gp; current = P; P = gp; Gp = node [p]. parent ;}} public void Union (int I, Int J) {If (node [I]. parent <node [J]. parent) {node [J]. parent + = node [I]. parent; node [I]. root = false; node [I]. parent = J;} else {node [I]. parent + = node [J]. parent; node [J]. root = false; node [J]. parent = I ;}}}
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.