mirror of
https://github.com/AdrianKuta/Tree-Data-Structure.git
synced 2026-06-20 03:10:14 +02:00
feat: Kotlin 2.x/K2, version catalog, serialization & coroutines modules, docs
v3.4 modernization (continued): - Migrate to Kotlin 2.x (K2); introduce gradle/libs.versions.toml version catalog; simplify the JS/Wasm/Node and iOS source-set wiring for the K2 hierarchy template. - Apply binary-compatibility-validator and Kover plugins. - New published module tree-structure-serialization: @Serializable TreeNodeDto with toDto()/toTreeNode() round-trip (kotlinx.serialization). - New published module tree-structure-coroutines: asFlow()/pre/post/levelOrderFlow() (kotlinx.coroutines Flow traversal). - Docs: README examples for Sequence/navigation/functional APIs, class-level KDoc (thread-safety/complexity), and a CHANGELOG.md. - Ignore subproject build/ directories. - Bump version to 3.4.0. All JVM tests green (core + both modules).
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package com.github.adriankuta.datastructure.tree.serialization
|
||||
|
||||
import com.github.adriankuta.datastructure.tree.TreeNode
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
* A serializable, acyclic view of a [TreeNode] subtree. [TreeNode] itself holds a back-reference to
|
||||
* its parent (a cycle), so it cannot be `@Serializable` directly — convert to/from this DTO instead.
|
||||
*
|
||||
* ```
|
||||
* val json = Json.encodeToString(tree.toDto())
|
||||
* val restored = Json.decodeFromString<TreeNodeDto<String>>(json).toTreeNode()
|
||||
* ```
|
||||
*/
|
||||
@Serializable
|
||||
public data class TreeNodeDto<T>(
|
||||
val value: T,
|
||||
val children: List<TreeNodeDto<T>> = emptyList(),
|
||||
)
|
||||
|
||||
/** Converts this subtree into a serializable [TreeNodeDto], preserving values and shape. */
|
||||
public fun <T> TreeNode<T>.toDto(): TreeNodeDto<T> =
|
||||
TreeNodeDto(value, children.map { it.toDto() })
|
||||
|
||||
/** Rebuilds a mutable [TreeNode] tree from this DTO. */
|
||||
public fun <T> TreeNodeDto<T>.toTreeNode(): TreeNode<T> {
|
||||
val node = TreeNode(value)
|
||||
children.forEach { node.addChild(it.toTreeNode()) }
|
||||
return node
|
||||
}
|
||||
Reference in New Issue
Block a user