? QCompactTreeNode
QCompactTreeNode is a Kotlin library that can construct a tree structure.
- Just copy and paste QNode.kt into your project
- You can modify and redistribute it under the MIT License.
How to use
output
example src code
fun main() {
// First, you have to create the root node.
val root = QNode(0)
val node1 = root add 1
val node2 = root add 2
val node3 = node2 add 3
val node4 = node2 add 4
val node5 = node4 add 5
val node6 = node4 add 6
val node7 = node2 add 7
val unicodeTree = root.tree(color = QShColor.GREEN, style = QTreeStyle.UNICODE)
println(unicodeTree)
val asciiTree = root.tree(color = QShColor.BLUE, style = QTreeStyle.ASCII)
println(asciiTree)
println()
val depthFirstResult = root.descendantsList(QSearchAlgo.DepthFirst).toString()
println("DepthFirst : $depthFirstResult") // [0, 1, 2, 3, 4, 5, 6, 7]
val breadthFirstResult = root.descendantsList(QSearchAlgo.BreadthFirst).toString()
println("BreadthFirst : $breadthFirstResult") // [0, 1, 2, 3, 4, 7, 5, 6]
println()
// node can store anything
val rootA = QNode("A")
val nodeB = rootA add "B"
val nodeC = nodeB add "C"
val nodeD = nodeB add "D"
val nodeE = nodeD add "E"
val nodeF = nodeE add "F"
val nodeG = nodeC add "G"
val textTree = rootA.tree(color = QShColor.CYAN, style = QTreeStyle.UNICODE)
println(textTree)
// You can implement QLazyNode for more complicated situations.
class QFileNode(override val value: Path) : QLazyNode<Path> {
override fun hasChildNodeToFill(): Boolean {
return value.isDirectory()
}
override fun fillChildren(): List<QFileNode> = Files.walk(value, 1).filter {
it != value
}.map {
QFileNode(it)
}.toList()
override fun toTreeNodeString(): String {
return value.name
}
}
val rootDir = Paths.get("rsc-test/root-dir").toAbsolutePath()
val fileTree = QFileNode(rootDir).fillTree(maxDepth = 2).tree()
println(fileTree)
}
Please see QNodeTest.kt for more code examples. This test file is a self-contained, single-file source code that includes a runnable main function. You can easily copy and paste it into your codebase and run it.
Dependency
Refer to build.gradle.kts to directly check project settings.
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect:1.8.20")
}
How did I create this library
I created this library by developing a program within my own codebase that automatically resolves dependencies at the method or property level, extracts necessary code elements, and generates a compact, self-contained, single-file library.
The program uses PSI to resolve dependencies for function calls and references to classes.
Although my original repository is currently disorganized, I have been gradually extracting and publishing small libraries. I also plan to prepare the original repository for publication in the future