getStats

获取关于树的统计信息,如总节点数、叶子节点数、深度等。

getStats

getStats 方法遍历整个树,并返回一个包含其统计信息的对象。

用法

import { Tree } from '@movk/core'

const tree = [
  { id: 1, children: [
    { id: 2 },
    { id: 3, children: [{ id: 4 }] }
  ] }
]

const stats = Tree.getStats(tree)

/*
stats 将会是:
{
  total: 4,     // 总节点数
  leafCount: 2, // 叶子节点数
  maxDepth: 2,  // 最大深度
  minDepth: 1   // 最小深度(叶子节点中)
}
*/

API

getStats<T extends TreeNode>(tree: T[], config?: TreeConfig): { total: number; leafCount: number; maxDepth: number; minDepth: number }

参数

tree
T[] required
源树形结构数组。
config
TreeConfig
用于自定义树形结构中 id, pid, children 键名的配置对象。

返回值

object
返回一个包含 total, leafCount, maxDepth, minDepth 属性的统计对象。

Changelog

No recent changes
Copyright © 2024 - 2025 YiXuan - MIT License