Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. import java.util.Arrays
  2.  
  3. fun main(args: Array<String>) {
  4. println("Hello, dssfd!!!")
  5. val l = Node("L", null, setOf())
  6. val k = Node("K", null, setOf(l))
  7. l.parent = k
  8. val j = Node("J", null, setOf(k))
  9. val f = Node("F", null, setOf())
  10. val e = Node("E", null, setOf(j, f))
  11. j.parent = e
  12. f.parent = e
  13. val i = Node("I", null, setOf())
  14. val d = Node("D", null, setOf(e, i))
  15. i.parent = d
  16. val c = Node("C", null, setOf(d))
  17. val h = Node("H", null, setOf())
  18. val g = Node("G", null, setOf(h))
  19. val b = Node("B", null, setOf(g, c))
  20. h.parent = g
  21. c.parent = b
  22. g.parent = b
  23. val universalOrbitMap = Tree("COM", null, setOf(b))
  24. println("DfDdD")
  25. universalOrbitMap.traverse { previous, it -> println(previous?.value + " " + it.value.toString()) }
  26.  
  27. println("x")
  28. }
  29.  
  30. typealias Tree<T> = Node<T>
  31.  
  32. fun <T> assignParents(nodes: Set<Node<T>>) {
  33. lateinit var currentRoot: Node<T>
  34. currentRoot = nodes.first()
  35. nodes -= currentRoot
  36. }
  37.  
  38. data class Node<T>(val value: T, var parent: Node<T>?, val children: Set<Node<T>>) {
  39. fun traverse(block: (current: Node<T>) -> Unit) {
  40. block(this)
  41. for (child in children) {
  42. child.traverse(block)
  43. }
  44. }
  45.  
  46. fun traverse(block: (previous: Node<T>?, current: Node<T>) -> Unit) {
  47. block(parent, this)
  48. for (child in children) {
  49. child.traverse(block)
  50. }
  51. }
  52. override fun toString() = "Node{$value, children=$children}"
  53. override fun hashCode() = Arrays.asList(value, children).hashCode()
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement