making Yaml and BFS of QNDTree work

This commit is contained in:
minjaesong
2018-12-09 01:52:57 +09:00
parent 11f7fa5c9a
commit 59c6876849
4 changed files with 153 additions and 27 deletions

View File

@@ -1,6 +1,7 @@
package net.torvald.terrarum
import java.util.ArrayList
import java.util.*
import kotlin.collections.ArrayList
class QNDTreeNode<T>(var data: T? = null, var parent: QNDTreeNode<T>? = null) {
var children = ArrayList<QNDTreeNode<T>>()
@@ -18,11 +19,54 @@ class QNDTreeNode<T>(var data: T? = null, var parent: QNDTreeNode<T>? = null) {
action(node, depth)
}
/**
* (QNDTreeNode, Int) is Node-depth pair, starting from zero.
*/
fun traversePreorder(action: (QNDTreeNode<T>, Int) -> Unit) {
this.traverse1(this, action)
}
/**
* (QNDTreeNode, Int) is Node-depth pair, starting from zero.
*/
fun traversePostorder(action: (QNDTreeNode<T>, Int) -> Unit) {
this.traverse2(this, action)
}
/**
* (QNDTreeNode, Int) is Node-depth pair, starting from zero.
*/
fun traverseLevelorder(action: (QNDTreeNode<T>, Int) -> Unit) {
val q = ArrayList<Pair<QNDTreeNode<T>, Int>>() // node, depth
q.add(this to 0)
while (q.isNotEmpty()) {
val node = q.removeAt(0)
action(node.first, node.second)
node.first.children.forEach {
q.add(it to node.second + 1)
}
}
}
/**
* Retrieves data in the node in a specific depth (level).
* Probably only useful for level = 1
*/
fun getLevelData(level: Int): List<T?> {
val list = ArrayList<T?>()
traversePreorder { node, i ->
if (i == level) {
list.add(node.data)
}
}
return list
}
override fun toString() = data.toString()
fun print() {
TODO()
}
}