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>(json).toTreeNode() * ``` */ @Serializable public data class TreeNodeDto( public val value: T, public val children: List> = emptyList(), ) /** Converts this subtree into a serializable [TreeNodeDto], preserving values and shape. */ public fun TreeNode.toDto(): TreeNodeDto = TreeNodeDto(value, children.map { it.toDto() }) /** Rebuilds a mutable [TreeNode] tree from this DTO. */ public fun TreeNodeDto.toTreeNode(): TreeNode { val node = TreeNode(value) children.forEach { node.addChild(it.toTreeNode()) } return node }