Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays
- fun main(args: Array<String>) {
- println("Hello, dssfd!!!")
- val l = Node("L", null, setOf())
- val k = Node("K", null, setOf(l))
- l.parent = k
- val j = Node("J", null, setOf(k))
- val f = Node("F", null, setOf())
- val e = Node("E", null, setOf(j, f))
- j.parent = e
- f.parent = e
- val i = Node("I", null, setOf())
- val d = Node("D", null, setOf(e, i))
- i.parent = d
- val c = Node("C", null, setOf(d))
- val h = Node("H", null, setOf())
- val g = Node("G", null, setOf(h))
- val b = Node("B", null, setOf(g, c))
- h.parent = g
- c.parent = b
- g.parent = b
- val universalOrbitMap = Tree("COM", null, setOf(b))
- println("DfDdD")
- universalOrbitMap.traverse { previous, it -> println(previous?.value + " " + it.value.toString()) }
- println("x")
- }
- typealias Tree<T> = Node<T>
- fun <T> assignParents(nodes: Set<Node<T>>) {
- lateinit var currentRoot: Node<T>
- currentRoot = nodes.first()
- nodes -= currentRoot
- }
- data class Node<T>(val value: T, var parent: Node<T>?, val children: Set<Node<T>>) {
- fun traverse(block: (current: Node<T>) -> Unit) {
- block(this)
- for (child in children) {
- child.traverse(block)
- }
- }
- fun traverse(block: (previous: Node<T>?, current: Node<T>) -> Unit) {
- block(parent, this)
- for (child in children) {
- child.traverse(block)
- }
- }
- override fun toString() = "Node{$value, children=$children}"
- override fun hashCode() = Arrays.asList(value, children).hashCode()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement