Chapter 7 Trees.  Trees are important data structures in computer science.  Trees have interesting properties: They usually are finite, but unbounded.

Slides:



Advertisements
Similar presentations
Peter Fritzson 1 MetaModelica for Meta-Modeling and Model Transformations Peter Fritzson, Adrian Pop, Peter Aronsson OpenModelica Course at INRIA, 2006.
Advertisements

Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
© M. Winter COSC 4P41 – Functional Programming Testing vs Proving Testing –uses a set of “typical” examples, –symbolic testing, –may find errors,
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
String is a synonym for the type [Char].
NUMBER THEORY Chapter 1: The Integers. The Well-Ordering Property.
Recursive Data Types Koen Lindström Claessen. Modelling Arithmetic Expressions Imagine a program to help school-children learn arithmetic, which presents.
Domain Specific Embedded Languages Lecture 2, Designing and Using Combinators John Hughes.
Cse536 Functional Programming 1 6/12/2015 Lecture #7, Oct. 18, 2004 Today’s Topics – Trees – Kinds of trees - branching factor –functions over trees –patterns.
Cse536 Functional Programming 1 6/12/2015 Lecture #11, Nov. 01, 2004 Special Guest lecture by Tom Harke Today’s Topics –The Haskell Class system –Instance.
Trees Nature Lover’s View Of A Tree root branches leaves.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Advanced Programming Handout 12 Higher-Order Types (SOE Chapter 18)
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Arithmetic Expression Consider the expression arithmetic expression: (a – b) + ((c + d) + (e * f)) that can be represented as the following tree.
Advanced Programming Handout 5 Recursive Data Types (SOE Chapter 7)
Section 1-5 Algebra: Variables and Expressions. Vocabulary Algebra: Is a language of symbols, including variables Variable: Is a symbol, usually a letter,
Patterns and Sequences. Patterns refer to usual types of procedures or rules that can be followed. Patterns are useful to predict what came before or.
Presentation topic : LISP Amir Shahzad MCS 3 MCC
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
0 PROGRAMMING IN HASKELL Chapter 11 - Interactive Programs, Declaring Types and Classes.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Trees Nature Lover’s View Of A Tree root branches leaves.
© University of Auckland Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Trees. Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees. Nonlinear Containers root node leaf edge.
COMP Parsing 2 of 4 Lecture 22. How do we write programs to do this? The process of getting from the input string to the parse tree consists of.
Introduction to Functional Programming and ML CS 331 Principles of Programming Languages.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Chapter 9: Functional Programming in a Typed Language.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
COMP Parsing 3 of 4 Lectures 23. Using the Scanner Break input into tokens Use Scanner with delimiter: public void parse(String input ) { Scanner.
Com Functional Programming Regular Expressions and Abstract Data Types Marian Gheorghe Lecture 14 Module homepage Mole &
Overview of the Haskell 98 Programming Language
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
More Data Types CSCE 314 Spring CSCE 314 – Programming Studio Defining New Data Types Three ways to define types: 1.type – Define a synonym for.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
8.1 – Sequences and Series. Sequences Infinite sequence = a function whose domain is the set of positive integers a 1, a 2, …, a n are the terms of the.
Parsing 2 of 4: Scanner and Parsing
String is a synonym for the type [Char].
Conditional Expressions
Principles of programming languages 12: Functional programming
Koen Lindström Claessen
COMP261 Lecture 18 Parsing 3 of 4.
ML: a quasi-functional language with strong typing
Propositional Calculus: Boolean Functions and Expressions
RE-Tree: An Efficient Index Structure for Regular Expressions
CHAPTER 4 Trees.
Ch. 8 – Sequences, Series, and Probability
Pointers and References
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
PROGRAMMING IN HASKELL
CSE 311: Foundations of Computing
PROGRAMMING IN HASKELL
Types and Classes in Haskell
PROGRAMMING IN HASKELL
Sequences Overview.
Dr.Surasak Mungsing CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 07: การวิเคราะห์ขั้นตอนวิธีที่ใช้ในโครงสร้างข้อมูลแบบ.
Testing vs Proving Testing uses a set of “typical” examples,
การวิเคราะห์และออกแบบขั้นตอนวิธี
Trees Today’s Topics Trees Kinds of trees - branching factor
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Faculty of Computer Science and Information System
Presentation transcript:

Chapter 7 Trees

 Trees are important data structures in computer science.  Trees have interesting properties: They usually are finite, but unbounded in size. They sometimes contain other types within. They are often polymorphic. They may have differing “branching factors”. They may have different kinds of leaf and branching nodes.  Lots of interesting things can be modeled as trees lists (linear branching) arithmetic expressions (see text) parse trees (for languages)  In a lazy language it is possible to have infinite trees.

Examples data List a = Nil | MkList a (List a) data Tree a = Leaf a | Branch (Tree a) (Tree a) data IntegerTree = IntLeaf Integer | IntBranch IntegerTree IntegerTree data SimpleTree = SLeaf | SBranch SimpleTree SimpleTree data InternalTree a = ILeaf | IBranch a (InternalTree a) (InternalTree a) data FancyTree a b = FLeaf a | FBranch b (FancyTree a b) (FancyTree a b)

Match up the Trees  IntegerTree  Tree  SimpleTree  List  InternalTree  FancyTree ‘a’‘b’ ‘a’ ‘b’‘c’ 2 69 ‘a’ ‘b’ 1 2 ‘a’ ‘b’‘c’

Functions on Trees  Transforming one kind of tree into another: mapTree :: (a->b) -> Tree a -> Tree b mapTree f (Leaf x) = Leaf (f x) mapTree f (Branch t1 t2) = Branch (mapTree f t1) (mapTree f t2)  Collecting the items in a tree: fringe :: Tree a -> [a] fringe (Leaf x) = [x] fringe (Branch t1 t2) = fringe t1 ++ fringe t2  What kind of information is lost using fringe ?

More Functions on Trees treeSize :: Tree a -> Integer treeSize (Leaf x) = 1 treeSize (Branch t1 t2) = treeSize t1 + treeSize t2 treeHeight :: Tree a -> Integer treeHeight (Leaf x) = 0 treeHeight (Branch t1 t2) = 1 + max (treeHeight t1) (treeHeight t2)

Capture the Pattern of Recursion Many of our functions on trees have similar structure. Can we apply the abstraction principle? Yes we can: foldTree :: (a -> a -> a) -> (b -> a) -> Tree b -> a foldTree combine leafFn (Leaf x) = leafFn x foldTree combine leafFn (Branch t1 t2) = combine (foldTree combine leafFn t1) (foldTree combine leafFn t2)

Using foldTree With foldTree we can redefine the previous functions as: mapTree f = foldTree Branch fun where fun x = Leaf (f x) fringe = foldTree (++) fun where fun x = [x] treeSize = foldTree (+) (const 1) treeHeight = foldTree fun (const 0) where fun x y = 1 + max x y

Arithmetic Expressons data Expr = C Float | Add Expr Expr | Sub Expr Expr | Mul Expr Expr | Div Expr Expr Or, using infix constructor names: data Expr = C Float | Expr :+ Expr | Expr :- Expr | Expr :* Expr | Expr :/ Expr Infix constructors begin with a colon (:), whereas ordinary constructor functions begin with an upper-case character.

Example e1 = (C 10 :+ (C 8 :/ C 2)) :* (C 7 :- C 4) evaluate :: Expr -> Float evaluate (C x) = x evaluate (e1 :+ e2) = evaluate e1 + evaluate e2 evaluate (e1 :- e2) = evaluate e1 - evaluate e2 evaluate (e1 :* e2) = evaluate e1 * evaluate e2 evaluate (e1 :/ e2) = evaluate e1 / evaluate e2 Main> evaluate e1 42.0