a datatype for binary trees
play

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


  1. 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 ; 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

  2. Recursion Template Functions on Trees Amtoft from Hatcliff Binary Trees b i t r e e = datatype Examples Leaf i n t of General Trees | Node of b i t r e e ∗ b i t r e e ; Recall our guiding principle: A recursive function follows the structure of inductively-defined data. Recursion template: t r e e r e c ( Leaf (n )) = . . . . fun | 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 ) . . . ;

  3. Sum of Tree Data Functions on Trees Amtoft from Hatcliff Sample tree: Binary Trees Examples t = Node ( Node ( Leaf (3) , Leaf ( 4 ) ) , Leaf ( 1 ) ) ; − val General Trees t = Node ( Node ( Leaf #, Leaf #), Leaf 1) val : b i t r e e Desired behavior − use ” t r e e l i b r a r y . sml” ; . . . tree sum = fn : b i t r e e − > i n t val − tree sum ( t ) ; i t = 8 : i n t val Implementation: tree sum ( Leaf (n )) = n fun | tree sum ( Node ( bt1 , bt2 )) = tree sum ( bt1 ) + tree sum ( bt2 ) ;

  4. Flipping a Tree Functions on Trees Amtoft from Hatcliff t = Node ( Node ( Leaf (3) , Leaf ( 4 ) ) , Leaf ( 1 ) ) ; − val Binary Trees t = Node ( Node ( Leaf #, Leaf #), Leaf 1) val Examples : b i t r e e General Trees Desired behavior − use ” t r e e l i b r a r y . sml” ; . . . t r e e f l i p = fn : b i t r e e − > b i t r e e val − t r e e f l i p ( t ) ; i t = Node val ( 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 ) ) ;

  5. Fringe of a Tree Functions on Trees Amtoft from Hatcliff Binary Trees t = Node ( Node ( Leaf (3) , Leaf ( 4 ) ) , Leaf ( 1 ) ) ; − val Examples t = Node ( Node ( Leaf #, Leaf #), Leaf 1) val General Trees : b i t r e e Desired behavior − use ” t r e e l i b r a r y . sml” ; . . . t r e e f r i n g e = fn : b i t r e e − > i n t l i s t val − t r e e f r i n g e ( t ) ; i t = [ 3 , 4 , 1 ] : i n t l i s t val Implementation: t r e e f r i n g e ( Leaf (n )) = [ n ] fun | 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 ) ;

  6. A Datatype for General Trees Functions on Trees Amtoft from Hatcliff Trees with arbitrary branching (still no interior values) Binary Trees ’ a g t r e e = datatype Examples Leaf ’ a of General Trees | Node of ’ a branches ’ a branches = and Empty | Branch of ’ a g t r e e ∗ ’ a branches ; A recursive function still follows the structure of inductively-defined data. Recursion template now involves mutual recursion: g t r e e x ( Leaf (n )) = . . . fun | 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 ) . . . ;

  7. Sum of Tree Data Functions on Trees Amtoft from Hatcliff Binary Trees Examples ’ a g t r e e = datatype General Trees Leaf ’ a of | Node of ’ a branches ’ a branches = and 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 ) ;

  8. Lifting to Higher-Order Functions Functions on Trees Amtoft from Hatcliff ’ a g t r e e = datatype Binary Trees Leaf ’ a of Examples | Node of ’ a branches General Trees ’ a branches = and Empty | Branch of ’ a g t r e e ∗ ’ a branches ; As for lists, we can write a map function gtree map f ( Leaf (n )) = Leaf ( f (n )) fun | 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 ) ; gtree add1 = gtree map ( fn x = > x + 1 ) ; val What about filter or fold?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend