Created
May 1, 2025 10:57
-
-
Save lamg/01eaa6b309807aa861a414503830801a to your computer and use it in GitHub Desktop.
print_tree.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed class Tree<out T> { | |
| data class Leaf<T>(val value: T) : Tree<T>() | |
| data class Branch<T>( | |
| val value: T, | |
| val children: List<Tree<T>>, | |
| ) : Tree<T>() | |
| } | |
| fun <T> printTree(tree: Tree<T>): List<String> { | |
| fun connectIndent(isLast: Boolean, child: String, grandChildren: List<String>): List<String> { | |
| val childConn = if (isLast) "└── " else "├── " | |
| val colConn = if (isLast) " " else "│ " | |
| val connected = childConn + child | |
| val indented = grandChildren.map { colConn + it } | |
| return listOf(connected) + indented | |
| } | |
| fun treeToLines(t: Tree<T>): Pair<String, List<String>> { | |
| return when (t) { | |
| is Tree.Leaf -> t.value.toString() to emptyList() | |
| is Tree.Branch -> { | |
| val root = t.value.toString() | |
| val children = t.children.mapIndexed { i, c -> | |
| val isLast = i == t.children.lastIndex | |
| val (childRoot, childLines) = treeToLines(c) | |
| connectIndent(isLast, childRoot, childLines) | |
| }.flatten() | |
| root to children | |
| } | |
| } | |
| } | |
| val (rootLine, childLines) = treeToLines(tree) | |
| return listOf(rootLine) + childLines | |
| } | |
| val myTree: Tree<Int> = Tree.Branch( | |
| 10, | |
| listOf(Tree.Branch(15, listOf(Tree.Leaf(12), Tree.Leaf(18)))) | |
| ) | |
| for (x in printTree(myTree)) { | |
| println(x) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment