TreeNode

data class TreeNode<T>(val value: T, val children: List<TreeNode<T>> = emptyList())

Represents a node in a tree structure, capable of holding a value and a list of child nodes.

Parameters

T

The type of the value stored in the TreeNode.

Constructors

Link copied to clipboard
constructor(value: T, children: List<TreeNode<T>> = emptyList())

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

A list of child TreeNodes, defaulting to an empty list.

Link copied to clipboard
val value: T

The value contained in the TreeNode.

Functions

Link copied to clipboard
fun filter(predicate: (T) -> Boolean): TreeNode<T>?

Filters the tree based on a given predicate.

Link copied to clipboard
fun flattenTree(depth: Int = 0): Sequence<Pair<T, Int>>

Flattens the tree into a sequence of pairs containing each node's value and its depth.

Link copied to clipboard
open override fun toString(): String

Returns a string representation of the TreeNode.