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.P55

Description

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

Synopsis

Documentation

completelyBalancedTrees :: Int -> [Tree ()] Source #

In a completely balanced binary tree, the following property holds for every node: The number of nodes in its left subtree and the number of nodes in its right subtree are almost equal, which means their difference is not greater than one.

Write a function to construct completely balanced binary trees for a given number of nodes.

Examples

>>> printTreeList $ completelyBalancedTrees 4
[ Branch () (Branch () (Branch () Empty Empty) Empty) (Branch () Empty Empty)
, Branch () (Branch () Empty Empty) (Branch () (Branch () Empty Empty) Empty)
, Branch () (Branch () Empty Empty) (Branch () Empty (Branch () Empty Empty))
, Branch () (Branch () Empty (Branch () Empty Empty)) (Branch () Empty Empty) ]