Download presentation
Presentation is loading. Please wait.
Published byJonas Harrell Modified over 8 years ago
1
1 Recursive Data Structures CS 270 Math Foundations of CS Jeremy Johnson
2
2 Objective To provide recursive definitions for other, in addition to lists, important data structures and to systematically derive recursive algorithms to traverse and process them. To practice using recursive data structures and writing recursive functions in Racket
3
Outline Lists Recursive definition Predicate Properties Trees Recursive definition Predicate Properties Expressions and Expression Trees Recursive definition Predicate Properties
4
List Processing Recursive Definition List := null| (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 [accessors] Size is the number of conses Termination – size of recursive calls must decrease 4
5
List Predicates (define (list? l) (cond [(null? l) #t] [(cons? l) (list? (rest l))] [else #f])) (define (intlist? l) (cond [(null? l) #t] [(cons? l) (and (integer? (first l)) (intlist? (rest l))] [else #f])) 5
6
Size (define (atom? x) (not (cons? x))) ; Input: x is a list ; Output: a non-negative integer = number of cons cells in x (define (size x) (cond [(atom? x) 0] [(null? x) 0] [(cons? x) (+ 1 (size (first x)) (size (rest x)))])) 6
7
Binary Trees Recursive Definition Either empty or consists of value, and recursively a left and a right binary tree BinTree := null | (All BinTree BinTree) Constructor (define (BinTree value left right) (list value left right)) (BinTree 1 (BinTree 2 null null) (BinTree 3 (BinTree 4 null null) (BinTree 5 null null))) '(1 (2 null null) (3 (4 null null) (5 null null)))
8
BinTree Predicate (define (BinTree? T) (cond [(null? T) #t] [(list? l) (if (= (length T) 3) (and (BinTree? (left T)) (BinTree? (right T))) #f)] [else #f]))
9
Number of Nodes Access functions (define (left BT) (second BT)) (define (right BT) (third BT)) ; Input: T a binary tree ; Output: a non-negative integer = number of nodes in T (define (Nnodes BT) (if (null? BT) 0 (+ (Nnodes (left BT)) (Nnodes (right BT)) 1)))
10
Height of Binary Trees Height of tree T, H(T), is the max length of the paths from the root all of the leaves ; Input: T a binary tree ; Output: a non-negative integer = height of T (define (Height BT) (if (null? BT) (+ (max (Height (left BT)) (Height (right BT))) 1)))
11
Internal Path Length (IPL) The sum of the length of the paths from the root to every node in the tree Recursive definition ; Input: T a binary tree ; Output: a non-negative integer = internal path length of T (define (IPL (BT) (if (null? nil) 0 (+ (IPL (left BT)) (IPL (right BT)) (- (Nnodes BT) 1))))
12
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) 1 2 3 45 IPL = 6 = 0 + 2 + 4 Nnodes = 5 IPL((left BT)) = 0 IPL((right BT)) = 2
13
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
14
General Trees Recursive Definition Either empty or consists of value, and recursively a list of trees Tree := null | (All List ) Constructor (define (Tree value children) (list value children)) (Tree 1 (list (Tree 2 null) (Tree 3 null) (Tree 4 null))) '(1 (2 ()) (3 ()) (4 ())))
15
Tree Predicate (define (Tree? T) (cond [(null? T) #t] [(list? l) (if (= (length T) 2) (if (list? (second T)) (foldr and #t (map Tree? (second T))) #f) #f)] [else #f]))
16
Number of Nodes Access functions (define (value T) (first T)) (define (children T) (second T)) ; Input: T a general tree ; Output: a non-negative integer = number of nodes in T (define (Nnodes T) (if (null? T) 0 (foldr + 1 (map Nnodes (children T)))))
17
Expression Trees Basic arithmetic expressions can be represented by a binary tree Internal nodes are operators Leaf nodes are operands Consider 2 * ( 3 + 5 ) * 2+ 35
18
Expression Tree Definition ExpTree := (Number Integer) | (Add ExprTree ExprTree) | (Mult ExprTree ExprTree) (define (NumNode val) (list 'Number val)) (define (AddNode E1 E2) (list 'Add E1 E2)) (define (MultNode E1 E2) (list 'Mult E1 E2)) (MultNode (NumNode 2) (AddNode (NumNode 3) (NumNode 5)))
19
Expression Tree Predicate (define (ExpTree? T) (cond [(Number? T) #t] [(Add? T) #t] [(Mult? T) #t] [else #f])) (define (Add? T) (if (list? T) (if (= (length T) 3) (if (equal? (first T ‘Add)) (and (ExpTree? (op1 T)) (ExpTree? (op2 T))) #f) #f) #f))
20
Expression Tree Evaluation Check each case in the recursive definition, recursively evaluate subexpressions and apply appropriate operator ; Input: E is an arithmetic expression ; Output: a number = to the value of E (define (EvalExpr E) (cond [(Number? E) (Value E)] [(Add? E) (+ (EvalExpr (Op1 E)) (EvalExpr (Op2 E)))] [(Mult? E) (* (EvalExpr (Op1 E)) (EvalExpr (Op2 E)))])) (check= (let ((E (MultNode (NumNode 2) (AddNode (NumNode 3) (NumNode 5))))) (EvalExpr E)) 16)
21
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
22
Expression Tree Predicate (define (ExpTree? E) (cond [(Number? E) #t] [(Variable? E) #t] [(Add? E) #t] [(Mult? E) #t] [else #f]))
23
Expression Tree Size ; Inputs: E is an arithmetic expression ; Output: a non-negative integer equal to the number of ; constructors in E. (define (ExpTreeSize E) (cond [(Number? E) 1] [(Variable? E) 1] [(Add? E) (+ (ExpTreeSize (op1 E)) (ExpTreesize (op2 E)))] [(Mult? E) (+ (ExpTreeSize (op1 E)) (ExpTreesize (op2 E)))] [else #f]))
24
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
25
Expression Tree Evaluation Modify EvalExpr to include variables – Need second argument containing an environment and must handle variable case ; Inputs: E is an arithmetic expression and env is an environment ; Assume all variables in E are defined in env ; Output: a number = to the value of E (define (EvalExpr E env) (cond [(Number? E) (Value E)] [(Variable? E) (lookup E env)] [(Add? E) (+ (EvalExpr (Op1 E) env) (EvalExpr (Op2 E) env))] [(Mult? E) (* (EvalExpr (Op1 E) env) (EvalExpr (Op2 E) env))]))
26
26 Boolean Expressions BExpr := Constant: T|F [#t | #f] Variable [symbol] Negation: BExpr [(not BExpr)] And: BExpr BExpr [(and BExpr BExpr)] Or: BExpr BExpr [(or BExpr BExpr)]
27
27 Predicate for Boolean Expressions (define (BExpr? expr) (cond [ (constant? expr) #t ] [ (variable? expr) #t ] [ (not? expr) (Bexpr? (op1 expr)) ] [ (or? expr) (and (Bexpr? (op1 expr)) (Bexpr? (op2 expr))) ] [ (and? expr) (and (Bexpr? (op1 expr)) (Bexpr? (op2 expr))) ] [ else #f ]))
28
Evaluation ; Inputs: expr is a Boolean expression and env is an environment ; Assume that all variables in expr are defined in env ; Ouptut: a Boolean = to the value of expr (define (BExprEval expr env) (cond [ (constant? expr) expr ] [ (variable? expr) (lookup expr env) ] [ (not? expr) (not (BExprEval (op expr) env)) ] [ (or? expr) (or (BExprEval (op1 expr) env) (BEexprEval (op2 expr) env)) ] [ (and? expr) (and (BExprEval (op1 expr) env) (BExprEval (op2 expr) env)) ] ))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.