Skip to content

Kotlin library that can construct a tree structure.

License

Notifications You must be signed in to change notification settings

nyabkun/qq-tree

Repository files navigation

🐕 qq-tree

qq-tree is a Kotlin library that can construct a tree structure.

How to use

  • Just copy and paste Single-File version QTreeNode.kt into your project.
  • Or you can use Jar version. See Maven Dependency Section.
  • Feel free to fork or copy to your own codebase 😍

Example

output

01-num-tree.png 01-num-tree.png 03-file-tree.png

02-tree-walk.png

03-file-tree.png

code example

Full Source : QTreeNodeExample.kt

fun intTree() {
    // First, you have to create the root node.
    val root = QTreeNode(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]
}

fun textTree() {
    // node can store anything
    val rootA = QTreeNode("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)
}

fun fileTree() {
    // You can implement QLazyNode for more complicated situations.
    class QFileNode(override val value: Path) : QLazyTreeNode<Path> {
        override fun hasChildNodesToFill(): Boolean {
            return value.isDirectory()
        }

        override fun fillChildNodes(): List<QFileNode> {
            return Files.walk(value, 1).asSequence().filter {
                it != value
            }.sortedBy {
                it.name
            }.sortedBy {
                !it.isDirectory()
            }.map {
                QFileNode(it)
            }.toList()
        }

        override fun toTreeNodeString(): String {
            return if (value.isDirectory()) "📂 " + value.name else value.name
        }
    }

    val rootDir = Paths.get("src-split").toAbsolutePath()

    val fileTree = QFileNode(rootDir).fillTree(maxDepth = 3).tree()

    println(fileTree)
}

Please see QTreeNodeTest.kt for more code examples. Single-File version src-test-single/QTreeNodeTest.kt is a self-contained source code that includes a runnable main function. You can easily copy and paste it into your codebase.

Public API

Single-File version Dependency

If you copy & paste QTreeNode.kt, fefer to build.gradle.kts to directly check project settings.

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-reflect:1.8.20")
    implementation("org.jetbrains.kotlin:kotlin-stdlib:1.8.20")
}

Jar version Maven Dependency

If you prefer a jar library, you can use jitpack.io repository.

build.gradle ( Groovy )

repositories {
    ...
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.nyabkun:qq-tree:v2023-06-02'
}

build.gradle.kts ( Kotlin )

repositories {
    ...
    maven("https://jitpack.io")
}

dependencies {
    implementation("com.github.nyabkun:qq-tree:v2023-06-02")
}

pom.xml

<repositories>
    ...
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependencies>
    ...
    <dependency>
        <groupId>com.github.nyabkun</groupId>
        <artifactId>qq-tree</artifactId>
        <version>v2023-06-02</version>
    </dependency>
</dependencies>

How did I create this library

  • This library was created using qq-compact-lib to generates a compact, self-contained library.
  • qq-compact-lib is a Kotlin library that can extract code elements from your codebase and make a compact library.
  • It utilizes PSI to resolve function calls and class references.
  • The original repository is currently being organized, and I'm gradually extracting and publishing smaller libraries.