MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / leaf_size

Method leaf_size

publication/code/chapter08/bst.rs:99–118  ·  view source on GitHub ↗

计算叶节点数

(&self)

Source from the content-addressed store, hash-verified

97
98 // 计算叶节点数
99 fn leaf_size(&self) -> usize {
100 // 都为空,当前节点就是叶节点,返回 1
101 if self.left.is_none() && self.right.is_none() {
102 return 1;
103 }
104
105 // 计算左右子树的叶节点数
106 let left_leaf = match &self.left {
107 Some(left) => left.leaf_size(),
108 None => 0,
109 };
110
111 let right_leaf = match &self.right {
112 Some(right) => right.leaf_size(),
113 None => 0,
114 };
115
116 // 左右子树的叶节点数之和就是总的叶节点数
117 left_leaf + right_leaf
118 }
119
120 // 计算非叶节点数
121 fn none_leaf_size(&self) -> usize {

Callers 1

none_leaf_sizeMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected