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

Solutions.P54

Description

Some solutions to Problems.P54 of Ninety-Nine Haskell Problems.

Synopsis

Documentation

data Tree a Source #

A binary tree.

A Tree of type a consists of either an Empty node, or a Branch containing one value of type a with exactly two subtrees of type a.

Notes

Expand

This is not the problem 54A from the original Ninety-Nine Haskell Problems. As it also mentions, there is nothing to do here except making sure code compiles correctly, thanks to Haskell's strict typing and the way Tree is defined.

Instead, the problem was replaced by the simple problems of implementing given binary trees as Haskell values. I.e., turn the examples from the original problem into simple problems to solve.

Instances

Instances details
Generic (Tree a) Source # 
Instance details

Defined in Problems.BinaryTrees

Associated Types

type Rep (Tree a) :: Type -> Type #

Methods

from :: Tree a -> Rep (Tree a) x #

to :: Rep (Tree a) x -> Tree a #

Show a => Show (Tree a) Source # 
Instance details

Defined in Problems.BinaryTrees

Methods

showsPrec :: Int -> Tree a -> ShowS #

show :: Tree a -> String #

showList :: [Tree a] -> ShowS #

NFData a => NFData (Tree a) Source # 
Instance details

Defined in Problems.BinaryTrees

Methods

rnf :: Tree a -> () #

Eq a => Eq (Tree a) Source # 
Instance details

Defined in Problems.BinaryTrees

Methods

(==) :: Tree a -> Tree a -> Bool #

(/=) :: Tree a -> Tree a -> Bool #

Ord a => Ord (Tree a) Source #

An arbitrary total ordering for Tree.

Defines an order for a set of Trees. Not intended to support solving problems.

Instance details

Defined in Problems.BinaryTrees

Methods

compare :: Tree a -> Tree a -> Ordering #

(<) :: Tree a -> Tree a -> Bool #

(<=) :: Tree a -> Tree a -> Bool #

(>) :: Tree a -> Tree a -> Bool #

(>=) :: Tree a -> Tree a -> Bool #

max :: Tree a -> Tree a -> Tree a #

min :: Tree a -> Tree a -> Tree a #

type Rep (Tree a) Source # 
Instance details

Defined in Problems.BinaryTrees

type Rep (Tree a) = D1 ('MetaData "Tree" "Problems.BinaryTrees" "ninetynine-1.3.0-4Xxr3hBGtJH9Ff8qb2Invo" 'False) (C1 ('MetaCons "Empty" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Branch" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Tree a)) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Tree a)))))

leaf :: a -> Tree a Source #

Define a shorthand function for constructing a leaf node.

A leaf node in Tree is a branch with two empty subtrees.

tree1 :: Tree Char Source #

Define as the tree as shown in the following.

tree2 :: Tree Char Source #

Define as a binary tree consisting of only a single root node with value 'a'.

tree3 :: Tree Char Source #

Define as an empty binary tree.

tree4 :: Tree Int Source #

Define as the following tree with integer values.