scala with children

  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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import scala.collection.immutable.HashMap
class CritBitTrie[T](nodes: HashMap[String, CritBitTrie[T]], isExternalNode: Boolean /*, childrenSize: Int */, data: Option[T]) {
def this(isExternalNode: Boolean, data: Option[T]) = this(HashMap[String, CritBitTrie[T]](), isExternalNode, 0, data)
def add(word: String, value: T): CritBitTrie[T] = {
if (word.isEmpty) {
return new CritBitTrie[T](nodes, true, childrenSize, Option[T](value))
}
val maxPrefix = findMaxPrefix(word)
if (maxPrefix.isEmpty) {
val newLeaf = word -> new CritBitTrie[T](true, Option[T](value))
return new CritBitTrie[T](nodes + newLeaf, isExternalNode, childrenSize + 1, data)
}
val keyVal = nodes.find(kv => kv._1.startsWith(maxPrefix)).get
val edge = keyVal._1
val trie = keyVal._2
//добавление новой вершины
if (edge == maxPrefix) {
val newLeaf = edge -> trie.add(word.substring(edge.length), value)
val newNodes = nodes + newLeaf
val size = countWords(newNodes)
new CritBitTrie[T](newNodes, isExternalNode, size, data)
} else {
val end = edge.substring(maxPrefix.length)
val middleVertex = new CritBitTrie[T](HashMap(end -> trie), false, trie.size(), None)
val newLeaf = maxPrefix -> middleVertex.add(word.substring(maxPrefix.length), value)
val newNodes = (nodes - edge) + newLeaf
val size = countWords(newNodes)
new CritBitTrie[T](newNodes, isExternalNode, size, data)
}
}
def remove(word: String): CritBitTrie[T] = {
if (word.isEmpty) {
return new CritBitTrie(nodes, false, childrenSize, None)
}
val maxPrefix = findMaxPrefix(word)
if (maxPrefix.isEmpty) {
return this
}
val keyVal = nodes.find(kv => kv._1.startsWith(maxPrefix)).get
val edge = keyVal._1
val trie = keyVal._2
if (edge == maxPrefix) {
val newLeaf = edge -> trie.remove(word.substring(edge.length))
if (newLeaf._2.size() == 0) {
val newNodes = nodes - edge
val size = countWords(newNodes)
new CritBitTrie[T](newNodes, isExternalNode, size, data)
} else {
newLeaf._2.nodesNumber() match {
case 1 =>
val nextLeaf = newLeaf._2.getSingleLeaf()
if (newLeaf._2.size() == nextLeaf._2.size()) {
val leaf = (edge + nextLeaf._1) -> nextLeaf._2
val newNodes = (nodes - edge) + leaf
val size = countWords(newNodes)
new CritBitTrie[T](newNodes, isExternalNode, size, data)
} else {
val newNodes = nodes + newLeaf
val size = countWords(newNodes)
new CritBitTrie[T](newNodes, isExternalNode, size, data)
}
case _ =>
val newNodes = nodes + newLeaf
val size = countWords(newNodes)
new CritBitTrie[T](newNodes, isExternalNode, size, data)
}
}
} else {
this
}
}
def contains(word: String): Boolean = {
get(word) != None
}
def get(word: String): Option[T] = {
if (word.isEmpty) {
if (isExternalNode) {
return data
} else {
return None
}
}
val maxPrefix = findMaxPrefix(word)
if (maxPrefix.isEmpty) {
return None
}
val keyVal = nodes.find(kv => kv._1.startsWith(maxPrefix)).get
if (keyVal._1 != maxPrefix) {
return None
}
keyVal._2.get(word.substring(maxPrefix.length))
}
def size(): Int = {
if (isExternalNode) {
childrenSize + 1
} else {
childrenSize
}
}
def print(indent: Int = 0) {
if (isExternalNode) println(" " * indent + "$(" + data.get + ")$")
for (kv <- nodes) {
println(" " * indent + kv._1)
kv._2.print(indent + kv._1.length)
}
}
private def nodesNumber() = nodes.size
private def getSingleLeaf() = nodes.last
//сколько слов (вэлью?) хранится в дереве
private def countWords(nodes: HashMap[String, CritBitTrie[T]]) = nodes.foldLeft(0)(_ + _._2.size())
private def findMaxPrefix(word: String): String = {
for (edge <- nodes.keys) {
val prefix = (word, edge).zipped.takeWhile(Function.tupled(_ == _)).unzip._1.mkString
if (!prefix.isEmpty) {
return prefix
}
}
""
}
}
object CritBitTrie {
def apply[T](): CritBitTrie[T] = {
new CritBitTrie[T](false, None)
}
}