---
title: "getStats"
description: "获取关于树的统计信息，如总节点数、叶子节点数、深度等。"
seo_title: "getStats"
seo_description: "Compute statistics for a tree, including total nodes, leaves, branches, and maximum depth, to summarize its size and shape."
canonical_url: "https://core.mhaibaraai.cn/docs/transformers/tree/get-stats"
---
# getStats

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

## 用法

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

```ts [example.ts]
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 键名的配置对象。:可选。指定节点唯一标识符的键名。默认为 "id"。可选。指定节点父级标识符的键名。默认为 "pid"。可选。指定子节点数组的键名。默认为 "children"。

### 返回值

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

## Changelog

See commit history for [src/transformers/tree/validate.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/transformers/tree/validate.ts).


## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
