ufs - int root1 = FIND(a); // Find root of node a int root2...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// ufs.h - no weighted union, no path compression class Gentree { // Gentree for UNION/FIND private: int* array; // Node array int size; // Size of node array int FIND(int) const; // Find root public: Gentree(int); // Constructor ~Gentree() { delete [] array; } // Destructor void UNION(int, int); // Merge equivalences bool differ(int, int); // TRUE if not in same tree int display(int) const; void breakwall(int) const; }; Gentree::Gentree(int sz) { // Constructor size = sz; array = new int[sz * 5]; // Create node array for(int i=0; i<sz; i++) array[i] = ROOT; // make all nodes roots for(int i=sz; i<5*sz; i++) array[i] = 1; // make all walls walled-off } // Return TRUE if nodes are in different trees bool Gentree::differ(int a, int b) {
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int root1 = FIND(a); // Find root of node a int root2 = FIND(b); // Find root of node b return root1 != root2; // Compare roots } void Gentree::UNION(int a, int b) { // Merge two subtrees int root1 = FIND(a); // Find root of node a int root2 = FIND(b); // Find root of node b if (root1 != root2) array[root2] = a; // Merge (removed weighted union) } int Gentree::FIND(int curr) const { // Find root findcalls++; while (array[curr] != ROOT) { curr = array[curr]; treetraversals++; } return curr; // At root } int Gentree::display(int curr) const { // for displaying maze return array[curr]; } void Gentree::breakwall(int wall) const { // for connecting maze array[wall] = 0; }...
View Full Document

This note was uploaded on 01/19/2010 for the course CSC 375 taught by Professor Turner during the Spring '09 term at University of Michigan-Dearborn.

Ask a homework question - tutors are online