Scala последняя лаба (косая)

 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class Node[T](val value: T) {
private var depth: Int = 0
private var parent: Node[T] = this
def this (value: T, p: Node[T], d: Int) = {
this(value)
this.depth = d
this.parent = p
}
def Parent() = parent
def Depth() = depth
def isRoot() = this == this.parent
def printTree(n: Node[T] = this) {
println(n)
if (n != n.parent) printTree(n.parent)
}
override def toString():String = {
"value: " + value
}
}
import scala.collection.mutable.HashMap
class DSU[T](private var matchNode:HashMap[T, Node[T]] = new HashMap[T, Node[T]]) {
//var realRealationship = HashMap[T, Node[T]]()
private var implicitRelationship = HashMap[Node[T], Node[T]]()
def makeSet(value: T): DSU[T] = {
new DSU[T](matchNode += (value -> new Node[T](value)))
}
def MatchNode(value: T) = matchNode.apply(value)
def findSet(value: T): T = matchNode apply value Parent match {
case (parent) if (matchNode.apply(value) == parent && !implicitRelationship.contains(matchNode apply value) ) => value
case (parent) if (matchNode.apply(value) == parent && implicitRelationship.contains(matchNode apply value) ) => {
matchNode += (value -> new Node[T](value, implicitRelationship.apply(matchNode.apply(value)), matchNode.apply(value).Depth))
implicitRelationship -= matchNode.apply(value)
matchNode.apply(value).Parent.value
}
case _ => {
val result: T = findSet(matchNode.apply(value).Parent.value)
matchNode += (value -> new Node[T](value, matchNode.apply(result), matchNode.apply(value).Depth))
result
}
}
/*def findSet(value: T): T = phantomRealationship.contains(value) match {
case true => findCicle(value, phantomRealationship)
case false => findCicle(value, realRealationship)
}*/
def unionSets(a: T, b: T): DSU[T] = findSet(a) match{
case (z) if (z == findSet(b)) => this
case (z) if ((matchNode apply a isRoot) && MatchNode(a).Depth < MatchNode(b).Depth) => {
implicitRelationship += (MatchNode(b) -> MatchNode(a))
this
//new DSU (matchNode += (a -> new Node[T](a, matchNode apply b)))
}
case (z) if (matchNode apply a isRoot) => {
implicitRelationship += (MatchNode(a) -> MatchNode(b))
this
}
case _ if (MatchNode(findSet(a)).Depth < MatchNode(findSet(b)).Depth) => {
matchNode += (b -> new Node[T](b, matchNode apply b, MatchNode(findSet(a)).Depth + MatchNode(findSet(b)).Depth ))
new DSU (matchNode += (a -> new Node[T](a, matchNode apply b, MatchNode(a).Depth)))
}
case _ => {
matchNode += (b -> new Node[T](a, matchNode apply a, MatchNode(b).Depth + MatchNode(a).Depth ))
new DSU (matchNode += (b -> new Node[T](b, matchNode apply a, MatchNode(b).Depth)))
}
}
}
object Main extends App {
override def main(args: Array[String]) = {
var dsu = new DSU[Int]
dsu = dsu.makeSet(2)
println(dsu.findSet(2))
dsu = dsu.makeSet(3)
dsu = dsu.makeSet(4)
println(dsu.findSet(3) + " " + dsu.findSet(2))
var dsu1 = dsu.unionSets(2, 3)
dsu1 = dsu1.unionSets(4, 2)
println(dsu1.findSet(3))
println(dsu1.findSet(2))
println(dsu1.findSet(4))
}
}