-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisjointSet.cpp
More file actions
48 lines (42 loc) · 1.16 KB
/
DisjointSet.cpp
File metadata and controls
48 lines (42 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// DisjointSet.cpp
// MazesBetter
//
// Created by Kumin In on 2/19/16.
// Copyright © 2016 Kumin In. All rights reserved.
//
#include "DisjointSet.hpp"
DisjointSet::DisjointSet(): m_wall(255), m_rank(0), parent(this){}
bool DisjointSet::mergeSet(DisjointSet &other) {
DisjointSet *thisParent = this->findSet();
DisjointSet *otherParent = other.findSet();
if (thisParent == otherParent) {
return false;
}
if (thisParent->m_rank >= otherParent->m_rank) {
if(thisParent->m_rank == otherParent->m_rank) {
thisParent->m_rank += 1;
}
otherParent->parent = thisParent;
} else {
thisParent->parent = otherParent;
}
return true;
}
DisjointSet* DisjointSet::findSet() {
DisjointSet *child = this;
DisjointSet *parent = this->parent;
while (parent != parent->parent) {
parent = parent->parent;
}
while (child != parent) {
DisjointSet* tempParent = child->parent;
child->parent = parent;
child = tempParent;
}
return parent;
}
const std::string DisjointSet::getWall() {
std::bitset<8> bit_wall(m_wall);
return bit_wall.to_string();
}