⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fastunionfind.java

📁 《算法设计与分析》王晓东编著
💻 JAVA
字号:
public class FastUnionFind {
   private Tree[] unionTree;
   private Tree[] root; 
   private int n;
   
   public FastUnionFind(int n)
   {
	   unionTree=new Tree[n+1];	 
	   root=new Tree[n+1];
	   for (int i=1;i<=n;i++)
	   {
		   unionTree[i]=new Tree();
		   unionTree[i].node=i;
		   root[i]=unionTree[i];
	   }
	   this.n=n;
   }
   
   public int find(int nodeindex)
   {	   
	   if (nodeindex>n || nodeindex<1) return -1;
	   Tree temp=unionTree[nodeindex];
	   while (temp.upper!=null) temp=temp.upper;
	   return temp.node;
   } 
   
   public void union(int a,int b)
   {
	   Tree newtree=new Tree();	   
	   newtree.node=root[a].node;
	   root[a].upper=newtree;
	   root[a]=newtree;
	   root[b].upper=newtree;
	   root[b]=null;	   
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -