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

📄 unionfindwithtrees.cpp

📁 datastucutre and algorithms, application, in C
💻 CPP
字号:
// unite/find using trees
// simple version

#include <iostream>

using namespace std;

int *parent;

void initialize(int numberOfElements)
{// Initialize numberOfElements trees, 1 element per set/class/tree.
   parent = new int[numberOfElements + 1];
   for (int e = 1; e <= numberOfElements; e++)
      parent[e] = 0;
}

int find(int theElement)
{// Return root of tree that contains theElement.
   while (parent[theElement] != 0)
      theElement = parent[theElement];  // move up one level
   return theElement;
}

void unite(int rootA, int rootB)
{// Combine trees with different roots rootA and rootB.
   parent[rootB] = rootA;
}

void main(void)
{
   initialize(10);
   unite(1,2);
   unite(3,4);
   unite(1,3);
   cout << "find(1) = " << find(1) << " find(2) = " << find(2) << endl;
   cout << "find(3) = " << find(3) << " find(4) = " << find(4) << endl;
   cout << "find(5) = " << find(5) << " find(6) = " << find(6) << endl;
}

⌨️ 快捷键说明

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