For a school assignment, I made a binary tree implementation in Haskell as such:
data BinTree = L | N BinTree BinTree deriving (Eq, Show)
-- this function creates the full binary tree of size 2^(n+1) -1
makeBinTree 0 = L
makeBinTree n = N (makeBinTree (n-1)) (makeBinTree (n-1))
Which creates a binary tree in which each parent node has two children. So, makeBinTree 3 has the following output: N (N (N L L) (N L L)) (N (N L L) (N L L))
For my own understanding, I was hoping to make a tree such that each parent node has an arbitrary number of children. I've been stuck for a while on how to proceed.
So the input would be:
makeBinTree 2 3
and the output would be:
N (N L L L) (N L L L) (N L L L)
Any hints of how to do it would be greatly appreciated.
data BinTree = L | N BinTree BinTreehas 2 branches.data BinTree = L | N BinTree BinTree BinTreehas 3 branches. But what if you wanted a tree that can have a mixed number of branches? What stays the same between these two definitions and what changes?