Single-file Kotlin library which can construct, search, output tree structure
? QNode
QNode is a single-file Kotlin library which can construct, search, output tree structure.
- Just copy and paste QNode.kt into your project
- You can freely modify, redistribute or fork ? MIT license
How to use
fun main() {
// first you have to make 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)
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]
// 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 QNodeExI for more complicated situations.
val yourDirectory = "./src-build"
val fileTree = Paths.get("./src-build").qTree(2, color = QShColor.YELLOW, style = QTreeStyle.UNICODE)
println(fileTree)
}
See QNodeTest.kt for more code examples.
This test file is also single-file, self-contained and has a main function. You can copy and paste to your codebase and run it.
Dependency
Refer to build.gradle.kts to directly check dependent libraries.
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect:1.7.10")
}
How did I create this library
In my own chaotic codebase, I created a program which automatically resolves dependencies at the method or property level, extracts necessary code elements, and automatically generates a compact self-contained single-file library.
It utilizes PSI to resolve dependencies for function calls and references to the classes.
My original repository is currently quite chaotic, so I decided to extract it little by little and publish some small libraries.
Also, I would like to prepare the original repository and publish it in the future.