ninetynine-1.3.0: Ninety-Nine Haskell Problems
CopyrightCopyright (C) 2021 Yoo Chung
LicenseGPL-3.0-or-later
Maintainerdev@chungyc.org
Safe HaskellSafe-Inferred
LanguageGHC2021

Problems.P61

Description

Part of Ninety-Nine Haskell Problems. Some solutions are in Solutions.P61.

Synopsis

Documentation

leaves :: Tree a -> [a] Source #

Collect the leaves of a binary tree in a list. A leaf is a node with no successors.

Examples

>>> sort $ leaves tree4
[2,4]

Notes

Expand

The original problem also included implementing a function which counts leaves. Instead, the internals function was moved from problem 62 to this one because it seemed a more natural grouping.

The examples sort the results to avoid order sensitivity. It is less of an issue for leaves, which has a sort of obvious natural order, but there is no single natural order for internals.

internals :: Tree a -> [a] Source #

Collect the internal nodes of a binary tree in a list.

An internal node of a binary tree has either one or two non-empty successors.

Examples

>>> sort $ internals tree4
[1,2]