Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package równoważenie_wysokości;
- import java.util.ArrayList;
- import java.util.List;
- public class Element {
- int value;
- Element parent = null;
- int height;
- static ArrayList lista = new ArrayList();
- public Element(int value){
- this.value = value;
- }
- static void MAKE_SET(Element x){
- x.parent = x;
- x.height=0;
- lista.add(x);
- }
- static void UNION (Element x , Element y){
- LINK(FIND(x),FIND(y));
- }
- static void LINK (Element x, Element y){
- if (x.height > y.height)
- y.parent=x;
- else {
- x.parent=y;
- if (x.height == y.height)
- y.height++;
- }
- }
- static Element FIND(Element x ){
- return x == x.parent ? x : FIND(x.parent) ;
- }
- static void display(Element x){
- if (x.parent.value == x.value){
- System.out.print(x.parent.value);
- }
- else {
- while(x.parent.value != x.value){
- System.out.print(x.value + "==>" + x.parent.value+ "==>");
- display2(x.parent);
- break;
- }
- }
- }
- static void display2(Element x){
- if (x.parent.value == x.value){
- System.out.print(x.parent.value);
- }
- else {
- while(x.parent.value != x.value){
- System.out.print(x.parent.value+ "==>");
- display2(x.parent);
- break;
- }
- }
- }
- static void wyswietlListeElementow(){
- for (int i =0; i<lista.size();i++){
- System.out.println(lista.get(i));
- }
- }
- public static void main(String []args){
- Element a = new Element(10);
- Element b = new Element(11);
- Element c = new Element(12);
- Element d = new Element(13);
- Element e = new Element(100);
- wyswietlListeElementow();
- System.out.println("Lista elementów: "+a.value+" "+b.value+" "+c.value+" "+d.value+ " "+e.value);
- System.out.println();
- MAKE_SET(a);
- MAKE_SET(b);
- MAKE_SET(c);
- MAKE_SET(d);
- MAKE_SET(e);
- LINK(a,b);
- System.out.println("Łączę 10 z 11:");
- display(a);
- System.out.println("\n");
- LINK(b,e);
- System.out.println("Łączę 100 z 11:");
- display(e);
- System.out.println("\n");
- LINK(c,d);
- System.out.println("Łączę 12 z 13:");
- display(c);
- System.out.println("\n");
- LINK(d,b);
- System.out.println("Łączę 12 z 11:");
- display(c);
- System.out.println("\n");
- System.out.println("Całe drzewo");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment