Defining a rose tree (multiway tree) data type

A rose tree relaxes the limitation of at most two children per node. It can have an arbitrary number of elements. Rose trees are common when parsing HTML to represent the Document Object Model (DOM).

Getting ready

We will be representing the following tree in this recipe. The root node has three children:

Getting ready

How to do it...

We will not need any imports for this recipe:

  1. The rose tree data type is similar to that of the binary tree, except that instead of left and right children, it will store an arbitrary list of children:
    data Tree a = Node { value  :: a
                       , children  :: [Tree a] } 
                       deriving Show
  2. Construct the tree ...

Get Haskell Data Analysis Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.