Data type for Tree in Haskell - haskell

For a data type of a binary tree you can write something like this:
data Tree a = Nil | Node a (Tree a) (Tree a)
So if I do want to include trees, with Nodes having more than just two children, how could the data type possibly look like?

A lesser known technique is Left-child right-sibling where you can use the exact same type to encode trees with more than two children per node:
data Tree a
= Nil
| Node a (Tree a) (Tree a) -- value, left child, right sibling
The alternative [Tree a] does not have a performance advantage, since Haskell lists are linked lists.

You can either have a fixed branching factor:
data BinaryTree a = BTNil
| BTNode a (BinaryTree a) (BinaryTree a)
data TernaryTree a = TTNil
| TTNode a (TernaryTree a) (TernaryTree a) (TernaryTree a)
data QuadTree a = QTNil
| QTNode a (QuadTree a) (QuadTree a) (QuadTree a) (QuadTree a)
-- etc
(Note that QuadTree isn't a great name for a general tree with a branching factor of 4, since there is a specific data structure with that name.)
or you can simply use a rose tree, which stores an arbitrary list of children at each node.
data RoseTree a = RTNil | RTNode a [RoseTree a]
There is some duplication here, as RTNil is only needed to store an explicit empty tree. Leaf nodes are just RTNode a []. (Consider what difference, if any, you would assign to the values RTNode 3 [], RTNode 3 [RTNil], RTNode 3 [RTNil, RTNil], etc.)

Related

Haskell trees: how to get binary tree with leaf as any type?

Created a binary tree structure, values can be either a node with 2 branches, a leaf or empty. I've not specifically defined leaf to be a certain type so I can create trees with all leaves as integers or all as string, but how can I get it so that it accepts a mix of the 2 types, so a tree with leaves as integers aswell as strings?
data Tree a = Empty | Leaf a | Node a (Tree a) (Tree a)
Example of what I want: a node with 2 leaves, leaf1 = "a string" leaf2 = 5
My current problem is that leaf1 and leaf2 need to be same type so both either integers or both strings. I get an error If I do a mix.
Practically you can't create trees in Haskell that can contain any type. However, you can define custom types with multiple constructors like this:
data Val a b = Int a | String b deriving (Show, Eq)
You then use the Val type to create trees containing two types:
data Tree a = Leaf a | Node a (Tree a) (Tree a) deriving (Show, Eq)
let v = Node (String "eh") (Leaf (Int 12)) (Leaf (Int 123))
:t v
v :: Num a => Tree (Val a String)

Haskell defining a tree

In haskell I can do things such as
a :: Int
a = 15
I have the datatype as below
data Btree a = ND | Data a | Branch (Btree a) (Btree a) deriving (Show, Eq)
How can I define a tree, not using a function, such as above.
I have tried;
tree :: BTree
tree = Branch Btree (Branch ND Data 1) (Branch ND (Branch ND ND))
I cannot seem to get it to work (I am new to haskell so if this is very basic I apologise in advance)
Keep in mind that to construct an instance of the data type you need to call one of its constructor functions. All these constructors, ND, Data, Branch, are just regular functions, so you should call them as such.
In your tree, you are mixing data types (Btree) with constructors, while you should only be using constructors. Also, keep in mind that the function call (the 'space operator' is left-associated) is very greedy, so Branch ND Data 1 calls the function Branch with 3 arguments, rather than what you want: Branch ND (Data 1).
So to create your tree of depth 2 considering the above you write:
tree = Branch (Branch ND (Data 1)) (Branch ND (Branch ND ND))
Note that Data 1 will be a Btree as well without any additional baggage
Prelude> :t (Data 1)
(Data 1) :: Num a => Btree a
if you want a simple branch with a right element only you can write
Branch ND (Data 1)
or two level deep as in the other answer. At this point convenient constructor functions will be handy to minimize noise and typing.

What is the difference between value constructors and tuples?

It's written that Haskell tuples are simply a different syntax for algebraic data types. Similarly, there are examples of how to redefine value constructors with tuples.
For example, a Tree data type in Haskell might be written as
data Tree a = EmptyTree | Node a (Tree a) (Tree a)
which could be converted to "tuple form" like this:
data Tree a = EmptyTree | Node (a, Tree a, Tree a)
What is the difference between the Node value constructor in the first example, and the actual tuple in the second example? i.e. Node a (Tree a) (Tree a) vs. (a, Tree a, Tree a) (aside from just the syntax)?
Under the hood, is Node a (Tree a) (Tree a) just a different syntax for a 3-tuple of the appropriate types at each position?
I know that you can partially apply a value constructor, such as Node 5 which will have type: (Node 5) :: Num a => Tree a -> Tree a -> Tree a
You sort of can partially apply a tuple too, using (,,) as a function ... but this doesn't know about the potential types for the un-bound entries, such as:
Prelude> :t (,,) 5
(,,) 5 :: Num a => b -> c -> (a, b, c)
unless, I guess, you explicitly declare a type with ::.
Aside from syntactical specialties like this, plus this last example of the type scoping, is there a material difference between whatever a "value constructor" thing actually is in Haskell, versus a tuple used to store positional values of the same types are the value constructor's arguments?
Well, coneptually there indeed is no difference and in fact other languages (OCaml, Elm) present tagged unions exactly that way - i.e., tags over tuples or first class records (which Haskell lacks). I personally consider this to be a design flaw in Haskell.
There are some practical differences though:
Laziness. Haskell's tuples are lazy and you can't change that. You can however mark constructor fields as strict:
data Tree a = EmptyTree | Node !a !(Tree a) !(Tree a)
Memory footprint and performance. Circumventing intermediate types reduces the footprint and raises the performance. You can read more about it in this fine answer.
You can also mark the strict fields with the the UNPACK pragma to reduce the footprint even further. Alternatively you can use the -funbox-strict-fields compiler option. Concerning the last one, I simply prefer to have it on by default in all my projects. See the Hasql's Cabal file for example.
Considering the stated above, if it's a lazy type that you're looking for, then the following snippets should compile to the same thing:
data Tree a = EmptyTree | Node a (Tree a) (Tree a)
data Tree a = EmptyTree | Node {-# UNPACK #-} !(a, Tree a, Tree a)
So I guess you can say that it's possible to use tuples to store lazy fields of a constructor without a penalty. Though it should be mentioned that this pattern is kinda unconventional in the Haskell's community.
If it's the strict type and footprint reduction that you're after, then there's no other way than to denormalize your tuples directly into constructor fields.
They're what's called isomorphic, meaning "to have the same shape". You can write something like
data Option a = None | Some a
And this is isomorphic to
data Maybe a = Nothing | Just a
meaning that you can write two functions
f :: Maybe a -> Option a
g :: Option a -> Maybe a
Such that f . g == id == g . f for all possible inputs. We can then say that (,,) is a data constructor isomorphic to the constructor
data Triple a b c = Triple a b c
Because you can write
f :: (a, b, c) -> Triple a b c
f (a, b, c) = Triple a b c
g :: Triple a b c -> (a, b, c)
g (Triple a b c) = (a, b, c)
And Node as a constructor is a special case of Triple, namely Triple a (Tree a) (Tree a). In fact, you could even go so far as to say that your definition of Tree could be written as
newtype Tree' a = Tree' (Maybe (a, Tree' a, Tree' a))
The newtype is required since you can't have a type alias be recursive. All you have to do is say that EmptyLeaf == Tree' Nothing and Node a l r = Tree' (Just (a, l, r)). You could pretty simply write functions that convert between the two.
Note that this is all from a mathematical point of view. The compiler can add extra metadata and other information to be able to identify a particular constructor making them behave slightly differently at runtime.

Help In Declaring Variable Number Of Arguments

High Guys,
I have to define a polymorphic datatype for a tree that can have multiple nodes. Each node can have any number of children and a vlaue. This type will always have at least one node. I am new in Haskell so am asking how can i declare the node to have variable number of arguments.
This is what i have now. This is a tree that can have a Node or a node with value (a) and two tree children. Instead of two tree children, i want them to be any number of tree children. (Analoog as java variable number of arguments "arg...")
data Tree a = Node a | Node a (Tree a) (Tree a) deriving (Show)
Thanks for your help
EDIT
A little question::: How can i declare this node with variable arguments in a functions
parameter(header/signature). I have to implement a function called
"contains" which will check if a Node contains a specific element.
contains :: Tree a -> b -> Bool
contains (Node val [(tree)]) = ......
Is the second line correct ?
it would be:
data Tree a = Node a | Node a [(Tree a)] deriving (Show)
but in addition there is a second problem that this should be
data Tree a = Leaf a | Branch a [(Tree a)] deriving (Show)
or such as the parts of a union must have different names as otherwise you couldn't use pattern matching
Leaf and Branch are data constructors so:
Branch 1 [Leaf 3, Branch 6 [Leaf 5]]
is an example of a Tree
contains :: Tree a -> a -> Boolean
contains (Leaf a) b = a == b
contains (Branch a c) b = a == b || any (map (\t -> contains t b) c)
or such

Converting rose trees to different binary tree types

I am completely lost on how to do some tree conversions in Haskell. I need to go from a rose tree defined as:
data Rose a = Node a [Rose a] deriving (Eq, Show, Ord)
to a binary tree which is defined as:
data Btree a = Empty | Fork a (Btree a) (Btree a) deriving (Eq, Show, Ord)
In my class I was given a function that is similar, but using a different definition of the binary tree. For that function the rose tree is defined the same and the binary tree is defined as:
Btree a = Leaf a | Fork (Btree a) (Btree a)
with the function from rose tree to binary tree defined as:
toB :: Rose a -> Btree a
toB (Node x xts) = foldl Fork (Leaf x) (map toB xts)
toB (Node x []) = foldl Fork (Leaf x) []
I have the answer but I don't know how to convert it so that it works with the new definition of Btree.
When I did something like this, I considered the "left" subtree to be the first child, and the "right" subtree to be the siblings of the node. This means that you convert the tree like so:
h h
/|\ /
/ | \ /
b d e ==> b->d->e
/ \ / \ / /
a c f g a->c f->g
h is still the root, but in the second diagram / is the left subtree and -> is the right. Leaves have no left subtree, but might have siblings (right subtrees). The root has no right subtree, but might have children (left subtree). Internal nodes have both.
Does that help?
Try to write a function converting from the first to the second definition of the binary tree. Then convB . toB is your new function! Now, systematically create a new function that acts directly as a fusion of the two, by inlining one into the other, and you'll get a straightforward and elegant solution.

Resources