Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tree Representation in F#

Tags:

tree

f#

I'm trying to implement a tree in F# using a list of tuples.
[a] where a = (string, [a])
Each node has a list of their children and leaf nodes would be (name, [])

I want to be able to recursively iterate through each level of the list like this.

    a
 b     e
c d   f g

They wont always be binary trees however.

let t2 = [("a", [("b", [("c", []), ("d", [])]), ("e", [("f", []), ("g", [])])])]

let rec checkstuff tple =
    match tple with
    | (_, []) -> true
    | (node, children) ->
        List.fold ( || ) false (List.map checkstuff children)

I get:

Type mismatch. Expecting a
    ('a * 'b list) list
but given a
    'b list
The resulting type would be infinite when unifying ''a' and ''b * 'a list'

Is there a way I can do something like this or is there not support for a recursive list of tuples like this?

like image 792
Stephen Olsen Avatar asked Jun 17 '11 20:06

Stephen Olsen


People also ask

What is the representation of tree?

Binary Tree Representation: A tree is represented by a pointer to the topmost node of the tree. If the tree is empty, then the value of the root is NULL.

What is degree of a tree?

The degree of a tree is the maximum degree of a node in the tree. Distance. The number of edges along the shortest path between two nodes. Level. The level of a node is the number of edges along the unique path between it and the root node.

What are 234 trees used for?

2-3-4 trees are self-balancing and usually are usually very efficient for finding, adding and deleting elements, so like all trees they can be used for storing and retrieving elements in non-linear order.

How do you represent a tree in memory?

Linked representation Binary trees in linked representation are stored in the memory as linked lists. These lists have nodes that aren't stored at adjacent or neighboring memory locations and are linked to each other through the parent-child relationship associated with trees.


2 Answers

Try changing your data structure a bit:

type Tree =
  | Branch of string * Tree list
  | Leaf of string

let t2 = Branch ("a", [Branch ("b", [Leaf "c"; Leaf "d"]); Branch ("e", [Leaf "f"; Leaf "g"])])

let rec checkstuff tree =
    match tree with
    | Leaf _ -> true
    | Branch (node, children) ->
        List.fold ( || ) false (List.map checkstuff children)
like image 105
Daniel Avatar answered Sep 20 '22 04:09

Daniel


There are a couple ways to approach this, and Daniel's is nice. But here is another way (also using discriminant unions) to define a recursive data structure, this one being a bit closer to your own approach (though I think I may actually prefer Daniel's since the cases are more explicit):

type tree<'a> =
    | Node of 'a * list<tree<'a>>

let t3 = Node("a", [Node("b", [Node("c",[]); Node("d",[])]); Node("e", [Node("f",[]); Node("g",[])])])

let rec checkstuff tple =
    match tple with
    | Node(_, []) -> true
    | Node(node, children) ->
        List.fold ( || ) false (List.map checkstuff children)
like image 22
Stephen Swensen Avatar answered Sep 20 '22 04:09

Stephen Swensen