What Abstract Data Structure is this?

Consider the following algebraic data type in Haskell:

1
data X a = Nil | Node a (X a) (X a)

What is the type of data strucutre that X entails?

List Associative Array Binary Tree Stack

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

What this definition means is that X X is either empty or contains some value of typeclass a a and two X X 's. This is what exactly a binary tree is!

Here is how someone would use this defintion to represent a tree like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
freeTree :: X Char  
freeTree =   
    Node 'P'  
        (Node 'O'  
            (Node 'L'  
                (Node 'N' Nil Nil)  
                (Node 'T' Nil Nil)  
            )  
            (Node 'Y'  
                (Node 'S' Nil Nil)  
                (Node 'A' Nil Nil)  
            )  
        )  
        (Node 'L'  
            (Node 'W'  
                (Node 'C' Nil Nil)  
                (Node 'R' Nil Nil)  
            )  
            (Node 'A'  
                (Node 'A' Nil Nil)  
                (Node 'C' Nil Nil)  
            )  
        )  

Graphics from: Learn You a Haskell for great good Graphics from: Learn You a Haskell for great good

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...