{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lect15B_lists_recursion

# Lect15B_lists_recursion - ITI 1121 Introduction to...

This preview shows pages 1–13. Sign up to view the full content.

ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of March 27, 2010 Abstract Recursive list processing (part II) * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
“Head + tail” if ( ... ) { // base case calculate results } else { // general case // pre-processing s = method( p.next ); // recursion // post-processing }
E get( int index ) Let’s build the recursive method, this requires augmenting the signature of the method E get( int index ) with an additional parameter representing the current element ( p ). private E get( Node<E> p, int index ); If index represents the position of the element with respect to the list starting at p , what is the position of the element in the list starting at p.next ? Yes, it’s index-1 ! What is the base case? If the index is zero then simply returns p.value .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
private E get( Node < E > p, int index ) private E get( Node<E> p, int index ) { if ( index == 0 ) { return p.value; } return get( p.next, index-1 ); } What would occur if the initial value of index was larger than the total number of elements in list? index > 0 and p == null .
private E get( Node < E > p, int index ) private E get( Node<E> p, int index ) { if ( p == null ) { throw new IndexOutOfBoundsException(); } if ( index == 0 ) { return p.value; } return get( p.next, index - 1 ); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
public E get( int index ) public E get( int index ) { if ( index < 0 ) { throw new IndexOutOfBoundsException(); } return get( first, index ); } private E get( Node<E> p, int index ) { if ( p == null ) { throw new IndexOutOfBoundsException(); } if ( index == 0 ) { return p.value; } return get( p.next, index-1 ); }
int indexOf( E obj ) The methods indexOf returns the position of the first (left most) occurrence of obj in the list, the first element of the list is the position 0, the method returns -1 if the element is not found in the list. Following the “head + tail” strategy, the general case will involve a recursive call for the tail of the list: int s = indexOf( p.next, o ); What does s represent? It’s the position of o in the list starting at p.next . What is the position of o with respect to the list starting at p ? 1 + s .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
int indexOf( Node < E > p, E obj ) Combining s (the result of the recursive call) and the current node. if ( p.value.equals( o ) ) { result = 0; } else if ( result == -1 ) { result = s; } else { result = 1 + s; }
int indexOf( Node < E > p, E obj ) What is the base case? The smallest list is the empty list, it does not contain the value we’re looking for, we should return the special value -1, to indicate that a match could not be found. if ( p == null ) { return -1; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
int indexOf( Node < E > p, E obj ) private int indexOf( Node<E> p, E o ) { if ( p == null ) { return -1; } int result = indexOf( p.next, o); if ( p.value.equals( o ) ) { return 0; } if ( result == -1 ) { return result; } return result + 1; }
int indexOf( Node < E > p, E obj ) Is this working? Yes, but it’s inefficient. Why?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
int indexOf( Node < E > p, E obj ) The recursion should stop as soon as the first occurrence has been found. Okay, but how?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 54

Lect15B_lists_recursion - ITI 1121 Introduction to...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online