Vous êtes sur la page 1sur 3

Length (size) of a sequence

Recursion
Examples

code
int size(list) {

call 1 call 2 call 3 call 4


(a,b,c) (b,c) (c) ()

if is_empty(list) return 0 else return 1 + size(tail(list)); }

false return 1+2

false return 1+1

false return 1+0

true return 0

25/09/2012

DFR- DSA Recursion Examples

25/09/2012

DFR- DSA Recursion Examples

Display list (forwards)


code
display(list) { if not is_empty(list) { display_el(head(list)); display(tail(list)); } }

Display list (backwards)


code
display(list) { if not is_empty(list) { display(tail(list)); display_el(head(list)); } }

call 1 call 2 call 3 call 4


(a,b,c) true
display a

call 1 call 2 call 3 call 4


(a,b,c) true (b,c)
display a

(b,c) true

(c) true

() false

(b,c) true (c)

(c) true ()

() false

display b display c

(b,c)

(c)

()

display b display c

25/09/2012

DFR- DSA Recursion Examples

25/09/2012

DFR- DSA Recursion Examples

Insert - beginning (sorted sequence)


code
insert(el, list) { if is_empty(list) return cons(el, list); if getval(el)<getval(head(list)) return cons(el, list);

Insert - middle (sorted sequence)


code
insert(el, list) { if is_empty(list) return cons(el, list); if getval(el)<getval(head(list)) return cons(el, list); return(cons(head(list), insert(el, tail(list))); head=a return (a,d,e,g) head=d return (d,e,g)

Call 1
(a, (c,d,g)) false true return (a,c,d,g)

Call 1 Call 2 Call 3


(e,(a,d,g) ) false false (e, (d,g)) false false (e, (g)) false true
return (e,g)

return(cons(head(list), insert(el, tail(list))); }

}
25/09/2012 DFR- DSA Recursion Examples 5 25/09/2012 DFR- DSA Recursion Examples 6

Insert - end (sorted sequence)


code
insert(el, list) { if is_empty(list) return cons(el, list); if getval(el)<getval(head(list)) return cons(el, list); return(cons(head(list), insert(el, tail(list))); head=a return (a,d,e) head=d return (d,e)

Delete - beginning (sorted sequence)


(e, ()) true
return (e)

Call 1 Call 2 Call 3


(e, (a,d)) false false (e, (d)) false false if getval(el)=getval(head(list)) return tail(list); true return (d,e)

code
delete(el, list) { if is_empty(list) return list;

Call 1
(a, (a,d,e)) false

return(cons(head(list), delete(el, tail(list))); }

}
25/09/2012 DFR- DSA Recursion Examples 7 25/09/2012 DFR- DSA Recursion Examples 8

Delete - middle (sorted sequence)


code
delete(el, list) { if is_empty(list) return list; if getval(el)=getval(head(list)) return tail(list); return(cons(head(list), delete(el, tail(list))); } head=a return (a,e)

Delete - end (sorted sequence)


code
delete(el, list) { if is_empty(list) return list; if getval(el)=getval(head(list)) return tail(list); return(cons(head(list), delete(el, tail(list))); head=a return (a,d) head=d return (d)

Call 1
(d, (a,d,e)) false false

Call 2
(d, (d,e)) false true return (e)

Call 1 Call 2
(e, (a,d,e)) false false (e, (d,e)) false false

Call 3
(e, (e)) false true
return ()

}
25/09/2012 DFR- DSA Recursion Examples 9 25/09/2012 DFR- DSA Recursion Examples 10

Vous aimerez peut-être aussi