Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

Recursive Definitions and Structural Induction
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Termination Analysis Math Foundations of Computer Science.
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 Recitation 10. Induction Introduction. Induction is useful in proving properties of recursive algorithms, like their execution times. It is also the.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
General Info Check out gilgarn.org. QUIZ DO YOU: – Love CSC 171 ? – Want a job? – Like to exert power over others? – Want to improve CSC UR?
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Advanced Topics in Algorithms and Data Structures 1 An example.
Induction and recursion
Propositional Calculus CS 680: Formal Methods in Verification Computer Systems Jeremy Johnson.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
The ACL2 Proof Assistant Formal Methods Jeremy Johnson.
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
Compiled by: Dr. Mohammad Omar Alhawarat
5.5.3 Rooted tree and binary tree  Definition 25: A directed graph is a directed tree if the graph is a tree in the underlying undirected graph.  Definition.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Grammars and Parsing  Recursive Definitions of Properties.
Induction Proof. Well-ordering A set S is well ordered if every subset has a least element. [0, 1] is not well ordered since (0,1] has no least element.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
Classifications LanguageGrammarAutomaton Regular, right- linear Right-linear, left-linear DFA, NFA Context-free PDA Context- sensitive LBA Recursively.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Boolean Expression Evaluation CS 270: Math Foundations of CS Jeremy Johnson.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
1 CMSC 341 Introduction to Trees Textbook sections:
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
1 Proving Properties of Recursive Functions and Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
1 Recursive Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
CS 550 Programming Languages Jeremy Johnson
Binary Search Trees A binary search tree is a binary tree
CMSC 341 Introduction to Trees.
CS 270 Math Foundations of CS Jeremy Johnson
Discrete Structures for Computer Science
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Proving Properties of Recursive Functions and Data Structures
Proving Properties of Recursive List Functions
Binary Trees.
Simplification of Boolean Expressions
List and list operations (continue).
Presentation transcript:

Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive Algorithms for Manipulating and Traversing Data Structures  Structural induction  Examples  Lists  Trees  Expressions and Expression Trees

List Processing  Recursive Definition  List := nil | (cons All List)  List := nil (cons Type List )  Process lists using these two cases and use recursion to recursively process lists in cons  Use first and rest to access components of cons  Size is the number of conses (generally number of constructors)  Prove properties by inducting on size – assume property holds for smaller size objects and show that this implies it holds for object at hand 2

Member (defun member (x l) (cond ((equal l nil) nil) ((equal x (first l)) t) ((member x (rest l))))) 3

Append (defun append (x y) (if (equal x nil) y (cons (first x) (append (rest x) y))))  Properties 1.(append x nil) = x 2.(length (append x y)) = (+ (length x) (length y)) 3.(append x (append y z)) = (append (append x y) z) 4

Structural Induction  When using induction on recursively defined data structures like lists you can induct on the size of the data structure = to the number of calls to the constructors.  When trying to show a property for a data structure of a given size, you can assume that the property holds when making a recursive call on a smaller data structure. You must make sure that the property holds for all constructors including base cases.  With lists (rest …) will return a smaller data structure (at least one fewer cons)  Structural induction allows you to induct on the recursive data structure without being explicit about the size provided the IH is applied to smaller objects.

Proof of Property 1  Show (append x nil) = x using structural induction  Base case. x = nil. In this case, (append nil nil) returns nil = x.  By induction assume recursive call satisfies the property [note (rest x) is smaller than x]  I.E. (append (rest x) nil) = (rest x)  Thus (append x nil) returns (cons (first x) (rest x)) = x

Proof of Property 2  Show (length (append x y) = (+ (length x) (length y)) using structural induction on x  Base case. x = nil. (append nil y) = y and (length y) = (+ (length nil) (length y))  By induction assume recursive call satisfies the property  (length (append (rest x) y) = (+ (length (rest x)) (length y))  Thus (length (append x y)) = (length (cons (first x) (append (rest x) y)) = (+ 1 (length (rest x)) + (length y)) = (+ (length x) (length y))

Proof of Property 3  Show (append x (append y z)) = (append (append x y) z)  Base case. x = nil. (append nil (append y z)) = (append y z) = (append (append nil y) z)  Assume property holds for (rest x)  (append (append x y) z)  (append (cons (first x) (append (rest x) y)) z) [by def]  (cons (first x) (append (append (rest x) y) z)) [by def]  (cons (first x) (append (rest x) (append y z))) [by IH]  (append (cons (first x) (rest x)) (append y z)) [by def]  (append x (append y z)) [by property of cons]

Reverse (defun reverse (l) (if (equal l nil) nil (append (reverse (rest l)) (cons (first l) nil))))  Properties  (length (reverse x)) = (length x)  (reverse (append x y)) = (append (reverse y) (reverse x))  (reverse (reverse x)) = x 9

Proof of Property 2  Show (rev (append x y)) = (append (rev y) (rev x))  Base case. x = nil. (rev (append nil y)) = (rev y) = (append (rev y) nil) = (append (rev y) (rev nil))  Assume property holds for (rest x)  (rev (append x y))  (rev (cons (first x) (append (rest x) y)) [def apppend]  (append (rev (append (rest x) y)) (cons (first x) nil)) [def rev]  (append (append (rev y) (rev (rest x))) (cons (first x) nil)) [IH]  (append (rev y) (append (rev (rest x)) (cons (first x) nil))) [prop app]  (append (rev y) (rev x)) [def of rev]

Proof of Property 3  Show (rev (rev x)) = x  Base case. x = nil. (rev (rev nil)) = (rev nil) = nil  Assume property holds for (rest x)  (rev (rev x))  (rev (append (rev (rest x)) (cons (first x) nil))) [def rev]  (append (rev (cons (first x) nil)) (rev (rev (rest x)))) [property 2 of rev]  (append (cons (first x) nil) (rev (rev (rest x)))) [def of rev]  (append (cons (first x) nil) (rest x)) [IH]  (cons (first x) (append nil (rest x))) [def of app]  (cons (first x) (rest x)) = x [def of app and prop of cons]

n-Trees  Finite collection of nodes  Each node has exactly one parent, but for the root node  Each node may have up to n children  A leaf node has no children  An interior node is a node that is not a leaf  Each subtree is a tree rooted at a given node  Tree := nil | (All List )  E.G. (1 ((2 nil) (3 nil) (4 nil)))

Binary Trees  A binary tree is a 2-tree  A binary tree is  Empty, or  Consists of a node with 3 attributes:  value  left, which is a binary tree  right, which is a binary tree BinTree := (Type BinTree BinTree )  E.G. (1 (2 nil nil) (3 (4 nil nil) (5 nil nil))) (BinTree 1 (BinTree 2 nil nil) (BinTree 3 (BinTree 4 nil nil) (BinTree 5 nil nil)))

Number of Nodes of a Binary Trees  Nnodes(T) = 0 if T is empty  Nnodes(T.left) + Nnodes(T.right) + 1 (defun Nnodes (BT) (if (equal BT nil) 0 (+ (Nnodes (left BT)) (Nnodes (right BT)) 1))) (defun left (BT) (second BT)) (defun right (BT) (third BT))

Height of Binary Trees  Height of tree T, H(T), is the max length of the paths from the root all of the leaves  H(T) = -1 if T is empty  max( H(left(T)), H(right(T)) ) + 1 (defun Height (BT) (if (equal BT nil) (+ (max (Height (left BT)) (Height (right BT))) 1)))

Correctness of Height  Show (Height BT) = length of the longest path in BT  Prove by structural induction  Base case. -1 for nil is needed to obtain the correct height of a tree with a single node.  Assume (Height (left BT)) and (Height (right BT)) compute the lengths of the longest paths in the left and right subtrees  The longest path in BT is one more than the longest path in either the left or right subtrees which by induction is one more than the maximum of (Height (left BT)) and (Height (right BT))

Internal Path Length (IPL)  The sum of the length of the paths from the root to every node in the tree  Recursively:  IPL( T ) = 0 if T is empty  IPL( T ) = IPL( left( T )) + IPL( right( T )) + num_nodes( T ) – 1 (defun IPL (BT) (if (equal BT nil) 0 (+ (IPL (left BT)) (IPL (right BT)) (- (Nnodes BT) 1))))

IPL Verification (check= (IPL nil) 0) (check= (let ((BT (consBinTree 1 (consBinTree 2 nil nil) (consBinTree 3 (consBinTree 4 nil nil) (consBinTree 5 nil nil))))) (IPL BT)) 6) IPL = 6 = Nnodes = 5 IPL((left BT)) = 0 IPL((right BT)) = 2

Correctness of IPL  Show (ipl BT) = sum of the lengths of the paths from the root to all nodes in the tree.  Prove by structural induction  Base case. 0 is needed to get the proper value for a tree with a single node.  Assume (ipl (left BT)) and (ipl (right BT)) compute the sum of the lengths of paths in the left and right subtrees  The length of each path in BT is one more than the lengths of the paths in the left and right subtrees. Thus one has to be added for each node in the left and right subtrees and since there are (- (Nnodes BT) 1) such nodes this must be added to the sum of (ipl (left BT)) and (ipl (right BT))

Recurrence for the Number of Binary Trees  Let T n be the number of binary trees with n nodes.  T 0 = 1, T 1 = 1, T 2 = 2, T 3 = 5

Expression Trees  Basic arithmetic expressions can be represented by a binary tree  Internal nodes are operators  Leaf nodes are operands  Consider 2 * ( ) : * 2+ 35

Expression Tree Definition  ExpTree := (Number Integer) | (Add ExprTree ExprTree) | (Mult ExprTree ExprTree) (defun NumNode (val) (list 'Number val)) (defun AddNode (E1 E2) (list 'Add E1 E2)) (defun MultNode (E1 E2) (list 'Mult E1 E2)) (MultNode (NumNode 2) (AddNode (NumNode 3) (NumNode 5)))

Expression Tree Evaluation  Check each case in the recursive definition, recursively evaluate subexpressions and apply appropriate operator (defun EvalExpr (E) (cond ((isNumber E) (Val E)) ((isAdd E) (+ (EvalExpr (Op1 E)) (EvalExpr (Op2 E)))) ((isMult E) (* (EvalExpr (Op1 E)) (EvalExpr (Op2 E)))))) (check= (let ((E (MultNode (NumNode 2) (AddNode (NumNode 3) (NumNode 5))))) (EvalExpr E)) 16)

Expression Trees with Variables  What if we allow variables in expression trees?  The value depends on the values of the variables  Consider x * ( z + 5 )  When x = 2 & z = 5 the value is 20  When x = 0 & z =5 the value is 0  When x = 2 & z = -1the value is 8  … * x+ z5

Environments  Need to look up the values of all variables occurring in the expression  Environment contains a list of bindings where a binding is a pair (name value) that binds a value to the name of the variable  E.G. env = ((x 2) (z 5))  lookup returns the value associated with a given variable in an environment  (lookup ‘x env)  2

Expression Tree Evaluation  Modify EvalExpr to include variables – need second argument containing an environment and must handle variable case (defun EvalExpr (E env) (cond ((isNumber E) (Val E)) ((isVariable E) (lookup E env)) ((isAdd E) (+ (EvalExpr (Op1 E) env) (EvalExpr (Op2 E) env))) ((isMult E) (* (EvalExpr (Op1 E) env) (EvalExpr (Op2 E) env)))))