Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Functional Programming Lecture 7: Trees, Study notes of Computer Science

A part of the fall '05 cs457/557 functional programming course notes at psu, focusing on the topic of trees. Trees are essential data structures in computer science, characterized by their finite, unbounded size, polymorphism, and various branching factors. Different types of trees, such as integertree, simpletree, itree, and fancytree, and discusses functions on trees, including maptree, fringe, treesize, treeheight, and elemtree. It also introduces binary search trees and their implementation.

Typology: Study notes

Pre 2010

Uploaded on 08/17/2009

koofers-user-0ro
koofers-user-0ro 🇺🇸

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
10/13/05 PSU CS457/557 Fall '05 Tolmach 1
CS 457/557 Functional Programming
Lecture 7
Trees
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Functional Programming Lecture 7: Trees and more Study notes Computer Science in PDF only on Docsity!

CS 457/557 Functional Programming

Lecture 7

Trees

Trees

  • (^) Trees are important data structures in computer science.
  • Trees have interesting properties:
    • They usually are finite, but statically unbounded in size.
    • They often contain other non trivial 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)
    • shapes (see text)
    • programming language syntax trees
  • In a lazy language it is possible to have infinite trees.

Match up the Trees

  • IntegerTree
  • Tree
  • SimpleTree
  • List
  • ITree
  • FancyTree ‘a’ ‘b’ ‘a’ ‘b’ ‘c’ 2 6 9 ‘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 t
  • What kind of information is lost using fringe?

Binary Search Trees

  • InternalTrees (values at internal nodes) in sorted order.
  • Used for efficient implementation of sets, dictionaries, etc.
    • Logarithmic access, update in average case data ITree a = ILeaf | IBranch a (ITree a) (ITree a) elemTree:: Ord a => a -> ITree a -> Bool elemTree v ILeaf = False elemTree v (IBranch w l r) | v == w = True | v < w = elemTree v l | v > w = elemTree v r

Building Search Trees

insertTree::Ord a => a -> ITree a -> ITree a insertTree v ILeaf = IBranch v ILeaf ILeaf insertTree v (IBranch w l r) | v <= w = IBranch w (insertTree v l) r | v > w = IBranch w l (insertTree v r) listToTree xs = foldr insertTree ILeaf xs s = listToTree [1,4,3,5,2,9,8] == (IBranch 8 (IBranch 2 (IBranch 1 ILeaf ILeaf) (IBranch 5 (IBranch 3 ILeaf (IBranch 4 ILeaf ILeaf)) ILeaf)) (IBranch 9 ILeaf ILeaf))

Arithmetic Expressons

data Expr = C Float | Add Expr2 Expr | Sub Expr2 Expr | Mul Expr2 Expr | Div Expr2 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 e evaluate (e1 :- e2) = evaluate e1 - evaluate e evaluate (e1 :* e2) = evaluate e1 * evaluate e evaluate (e1 :/ e2) = evaluate e1 / evaluate e Main> evaluate e