This project is read-only.
Composite pattern

Category:
Design patterns
Description: For more information about composite pattern, please go to http://en.wikipedia.org/wiki/Composite_pattern
Code:
type CompositeNode<'T> = 
| Node of 'T
| Tree of 'T * CompositeNode<'T> * CompositeNode<'T>
with
member this.InOrder f =
match this with
| Tree(n, left, right) ->
left.InOrder(f)
f(n)
right.InOrder(f)
| Node(n) -> f(n)
member this.PreOrder f =
match this with
| Tree(n, left, right) ->
f(n)
left.PreOrder(f)
right.PreOrder(f)
| Node(n) -> f(n)
member this.PostOrder f =
match this with
| Tree(n, left, right) ->
left.PostOrder(f)
right.PostOrder(f)
f(n)
| Node(n) -> f(n)

let OtherCompositePatternSample() =
let tree = Tree(1, Tree(11, Node(12), Node(13)), Node(2))
let nodeAccessFunc = printf "%A,"

printf "in order process: "
tree.InOrder nodeAccessFunc
printfn ""

printf "pre order process: "
tree.PreOrder nodeAccessFunc
printfn ""

printf "post order process: "
tree.PostOrder nodeAccessFunc
printfn ""

Execution Result:
in order process: 12,11,13,1,2,
pre order process: 1,11,12,13,2,
post order process: 12,13,11,2,1,

Last edited Sep 14, 2011 at 4:25 AM by ttliu2000, version 2

Comments

No comments yet.