A Datatype for Binary Trees Functions on Trees Amtoft from - - PowerPoint PPT Presentation

a datatype for binary trees
SMART_READER_LITE
LIVE PREVIEW

A Datatype for Binary Trees Functions on Trees Amtoft from - - PowerPoint PPT Presentation

A Datatype for Binary Trees Functions on Trees Amtoft from Hatcliff Binary Trees One of many possible definitions (no interior values) Examples General Trees datatype b i t r e e = Leaf i n t of | Node of b i t r e e b i t r e e


slide-1
SLIDE 1

Functions on Trees Amtoft from Hatcliff Binary Trees

Examples

General Trees

A Datatype for Binary Trees

One of many possible definitions (no interior values)

datatype b i t r e e = Leaf

  • f

i n t | Node of b i t r e e ∗ b i t r e e ;

Possible constructors:

− Leaf ; val i t = fn : i n t −> b i t r e e − Node ; val i t = fn : b i t r e e ∗ b i t r e e −> b i t r e e − Leaf ( 4 ) ; val i t = Leaf 4 : b i t r e e − Node ( Leaf (3) , Leaf ( 4 ) ) ; val i t = Node ( Leaf 3 , Leaf 4) : b i t r e e

slide-2
SLIDE 2

Functions on Trees Amtoft from Hatcliff Binary Trees

Examples

General Trees

Recursion Template

datatype b i t r e e = Leaf

  • f

i n t | Node of b i t r e e ∗ b i t r e e ;

Recall our guiding principle: A recursive function follows the structure

  • f inductively-defined data.

Recursion template:

fun t r e e r e c ( Leaf (n )) = . . . . | t r e e r e c ( Node ( bt1 , bt2 )) = . . . t r e e r e c ( bt1 ) . . . . . . t r e e r e c ( bt2 ) . . . ;

slide-3
SLIDE 3

Functions on Trees Amtoft from Hatcliff Binary Trees

Examples

General Trees

Sum of Tree Data

Sample tree:

− val t = Node ( Node ( Leaf (3) , Leaf ( 4 ) ) , Leaf ( 1 ) ) ; val t = Node ( Node ( Leaf #, Leaf #), Leaf 1) : b i t r e e

Desired behavior

− use ” t r e e l i b r a r y . sml” ; . . . val tree sum = fn : b i t r e e −> i n t − tree sum ( t ) ; val i t = 8 : i n t

Implementation: fun tree sum ( Leaf (n )) = n | tree sum ( Node ( bt1 , bt2 )) = tree sum ( bt1 ) + tree sum ( bt2 ) ;

slide-4
SLIDE 4

Functions on Trees Amtoft from Hatcliff Binary Trees

Examples

General Trees

Flipping a Tree

− val t = Node ( Node ( Leaf (3) , Leaf ( 4 ) ) , Leaf ( 1 ) ) ; val t = Node ( Node ( Leaf #, Leaf #), Leaf 1) : b i t r e e

Desired behavior − use ” t r e e l i b r a r y . sml” ; . . . val t r e e f l i p = fn : b i t r e e −> b i t r e e − t r e e f l i p ( t ) ; val i t = Node ( Leaf 1 , Node ( Leaf #,Leaf #)) : b i t r e e

Implementation: fun t r e e f l i p ( Leaf (n )) = Leaf (n) | t r e e f l i p ( Node ( bt1 , bt2 )) = Node ( t r e e f l i p ( bt2 ) , t r e e f l i p ( bt1 ) ) ;

slide-5
SLIDE 5

Functions on Trees Amtoft from Hatcliff Binary Trees

Examples

General Trees

Fringe of a Tree

− val t = Node ( Node ( Leaf (3) , Leaf ( 4 ) ) , Leaf ( 1 ) ) ; val t = Node ( Node ( Leaf #, Leaf #), Leaf 1) : b i t r e e

Desired behavior

− use ” t r e e l i b r a r y . sml” ; . . . val t r e e f r i n g e = fn : b i t r e e −> i n t l i s t − t r e e f r i n g e ( t ) ; val i t = [ 3 , 4 , 1 ] : i n t l i s t

Implementation: fun t r e e f r i n g e ( Leaf (n )) = [ n ] | t r e e f r i n g e ( Node ( bt1 , bt2 )) = t r e e f r i n g e ( bt1 ) @ t r e e f r i n g e ( bt2 ) ;

slide-6
SLIDE 6

Functions on Trees Amtoft from Hatcliff Binary Trees

Examples

General Trees

A Datatype for General Trees

Trees with arbitrary branching (still no interior values)

datatype ’ a g t r e e = Leaf

  • f

’ a | Node of ’ a branches and ’ a branches = Empty | Branch of ’ a g t r e e ∗ ’ a branches ;

A recursive function still follows the structure

  • f inductively-defined data.

Recursion template now involves mutual recursion:

fun g t r e e x ( Leaf (n )) = . . . | g t r e e x ( Node ( bs )) = . . . branches x ( bs ) . . . and branches x (Empty ) = . . . | branches x ( Branch ( gt , bs )) = . . . g t r e e x ( gt ) . . . . branches x ( bs ) . . . ;

slide-7
SLIDE 7

Functions on Trees Amtoft from Hatcliff Binary Trees

Examples

General Trees

Sum of Tree Data

datatype ’ a g t r e e = Leaf

  • f

’ a | Node of ’ a branches and ’ a branches = Empty | Branch of ’ a g t r e e ∗ ’ a branches ; fun gtree sum ( Leaf (n )) = n | gtree sum ( Node ( bs )) = branches sum ( bs ) and branches sum ( Empty ) = 0 | branches sum ( Branch ( gt , bs )) = gtree sum ( gt ) + branches sum ( bs ) ;

slide-8
SLIDE 8

Functions on Trees Amtoft from Hatcliff Binary Trees

Examples

General Trees

Lifting to Higher-Order Functions

datatype ’ a g t r e e = Leaf

  • f

’ a | Node of ’ a branches and ’ a branches = Empty | Branch of ’ a g t r e e ∗ ’ a branches ;

As for lists, we can write a map function

fun gtree map f ( Leaf (n )) = Leaf ( f (n )) | gtree map f ( Node ( bs )) = Node ( branches map f bs ) and branches map f (Empty ) = Empty | branches map f ( Branch ( gt , bs )) = Branch ( gtree map f gt , branches map f bs ) ; val gtree add1 = gtree map ( fn x = > x + 1 ) ;

What about filter or fold?