Page 1
Self-Reference - RecursionSelf-Reference - Recursion
Cmput 115 - Lecture 6
Department of Computing Science
University of Alberta©Duane Szafron 1999
Some code in this lecture is based on code from the book:Java Structures by Duane A. Bailey or the companion structure package
Revised 1/2/00
Page 2
©Duane Szafron 1999
2
About This LectureAbout This Lecture
In this lecture we will learn about self-reference.
This powerful idea allows us to:– Write self-referencing algorithms.– Construct self-referencing objects.– Prove properties about algorithms using
mathematical induction.
In this lecture we will focus on self-referencing Algorithms
Page 3
©Duane Szafron 1999
3
OutlineOutline
Self-reference Recursive methods Computing the sum 1 + 2 + … n
recursively Inserting an element in a vector
recursively Stack frames A trace of recursion
Page 4
©Duane Szafron 1999
4
Self-ReferenceSelf-Reference
Self-reference occurs when object refers to itself or more generally to another object from the same class.
Self-reference also occurs when a method (or algorithm) calls itself. Such a method is called a recursive method.
Self-reference also occurs when the proof of a theorem relies on the application of the same theorem to a simpler case. This situation is called mathematical induction.
Page 5
©Duane Szafron 1999
5
Recursive MethodsRecursive Methods
Recursion occurs when a method calls itself, either directly or indirectly.
For recursion to terminate, two conditions must be met:– the recursive call must somehow be simpler the recursive call must somehow be simpler
than the original call.than the original call.– there must be one or more simple cases that there must be one or more simple cases that
do not make recursive calls (basis steps).do not make recursive calls (basis steps).
Page 6
©Duane Szafron 1999
6
Recursive Sum ExampleRecursive Sum Example Write a method that computes the sum of the
integers from 1 to n. Note that:
1 + 2 + … + n = (1 + 2 + … + n-1) + n
public static int sum(int n) {// post: return the sum of ints from 1 to the given value
if (n < 1)return 0;
elsereturn sum(n - 1) + n;
}code based on Bailey pg. 59
Page 7
©Duane Szafron 1999
7
Vector Element InsertionVector Element Insertion Recall the iterative implementation:
public void insertElementAt(Object object, int index) {//pre: 0 <= index <= size()//post: inserts the given object at the given index,// moving elements from index to size()-1 to the right
int i;this.ensureCapacity(this.elementCount + 1);for (i = this.elementCount; i > index; i--)
this.elementData[i] = this.elementData[i - 1];
this.elementData[index] = object;
this.elementCount++; }code based on Bailey pg. 39
index
Page 8
©Duane Szafron 1999
8
Recursive Element InsertionRecursive Element Insertion Here is a recursive implementation:
public void insertElementAt(Object object, int index) {
//pre: 0 <= index <= size()//post: inserts the given object at the given index,// moving elements from index to size()-1 to the right
Object previous;if (index >= this.size())
this.addElement(object);else {
previous = this.elementAt(index);this.insertElementAt(previous, index + 1);setElementAt(object, index);
} }code based on Bailey pg. 60
Basis step
Recurring step
Page 9
©Duane Szafron 1999
9
Trace of Recursive InsertTrace of Recursive Insert
1 2 3 4 5index=3ins 2.5
this
1 2 3 4 5index=4ins 3
this
ins 41 2 3 4 5
index=5this
(index=size(this)=5)add 5
1 2 3 4 5index=5
this 5
set 4 index=5
1 2 3 4 4this 5
index=4set 3
1 2 3 3 4this’ 5
set 2.5
1 2 2.5 3 4this 5index=3
Page 10
©Duane Szafron 1999
10
Direct References in MethodsDirect References in Methods
When a method is executing it can access some objects and some values.
The receiver object can be referenced directly using the pseudo-variable this.
Other objects and values can be referenced directly using method parameters and local variables.
Still other objects and values can only be accessed indirectly by sending messages that return references to them.
Page 11
©Duane Szafron 1999
11
Method Activations and FramesMethod Activations and Frames
A method can only access objects while it is executing or active.
The collection of all direct references in a method is called the frame or stack frame of a method.
The frame is created when the method is invoked, and destroyed when the method finishes.
If a method is invoked again, a new frame is created for it.
Page 12
©Duane Szafron 1999
12
Multiple Activations of a MethodMultiple Activations of a Method
When we invoke a recursive method on an object, the method becomes active.
Before it is finished, it makes a recursive call to the same method.
This means that when recursion is used, there is more than one copy of the same method active at once.
Therefore, each active method has its own frame which contains independent copies of its direct references.
Page 13
©Duane Szafron 1999
13
Stack Frames for Stack Frames for InsertElementAtInsertElementAt
Each frame has its own pseudo-variable, this, bound to a different receiver object.
Each frame has two parameters, object and index.
Each frame has its local variable, previous, bound to a different object.
These frames all exist at the same time.
Page 14
©Duane Szafron 1999
14
Recursive Vector Insertion ExampleRecursive Vector Insertion Example
For example, insert “Wilma” at index 1.
“Wilma”
aVectorelementData
elementCount
anArray0 1 2 3
“Fred” “Barney” “Betty” null
3
Page 15
©Duane Szafron 1999
15
Calling Calling insertElementAtinsertElementAt(“Wilma”, 1)(“Wilma”, 1)
if (index >= this.size())this.addElement(object);
else {previous = this.elementAt(index);this.insertElementAt(previous, index + 1); . . . .}
thisobjectindex
previous
aVectorelementData
elementCount
anArray0 1 2 3
“Fred” “Barney” “Betty” null
3
“Wilma”
1
code based on Bailey pg. 60
Page 16
©Duane Szafron 1999
16
Calling Calling insertElementAtinsertElementAt(“Barney”, 2)(“Barney”, 2)
thisobjectindex
previousaVector
elementData
elementCount
anArray0 1 2 3
“Fred” “Barney” “Betty” null
3
“Wilma”
1
thisobjectindex
previous
2
code based on Bailey pg. 60
if (index >= this.size())this.addElement(object);
else {previous = this.elementAt(index);this.insertElementAt(previous, index + 1); . . . .}
Page 17
©Duane Szafron 1999
17
CallingCalling insertElementAt insertElementAt(“Betty”, 3)(“Betty”, 3)
thisobjectindex
previousaVector
elementData
elementCount
anArray0 1 2 3
“Fred” “Barney” “Betty” null
3
“Wilma”
1
2
thisobjectindex
previous
thisobjectindex
previous
3
4
code based on Bailey pg. 60
if (index >= this.size())this.addElement(object);
else {previous = this.elementAt(index);this.insertElementAt(previous, index + 1); . . . .}
Page 18
©Duane Szafron 1999
18
Returning insertElementAt(“Betty”, 3)Returning insertElementAt(“Betty”, 3)
if (index >= this.size())this.addElement(object);
else {previous = this.elementAt(index);this.insertElementAt(previous, index + 1);
thisobjectindex
previousaVector
elementData
elementCount
anArray0 1 2 3
“Fred” “Barney” “Betty”
4
“Wilma”
1
2
thisobjectindex
previous
thisobjectindex
previous
3
code based on Bailey pg. 60
Page 19
©Duane Szafron 1999
19
Returning insertElementAt(“Barney”, 2)Returning insertElementAt(“Barney”, 2)
if (index >= this.size())this.addElement(object);
else {previous = this.elementAt(index);this.insertElementAt(previous, index + 1);setElementAt(object, index); }
thisobjectindex
previousaVector
elementData
elementCount
anArray0 1 2 3
“Fred” “Barney” “Betty”
4
“Wilma”
1
thisobjectindex
previous
2
code based on Bailey pg. 60
Page 20
©Duane Szafron 1999
20
Returning insertElementAt(“Wilma”, 1)Returning insertElementAt(“Wilma”, 1)
thisobjectindex
previous
aVectorelementData
elementCount
anArray0 1 2 3
“Fred” “Barney” “Betty”
4
“Wilma”
1
code based on Bailey pg. 60
if (index >= this.size())this.addElement(object);
else {previous = this.elementAt(index);this.insertElementAt(previous, index + 1);setElementAt(object, index); }
Page 21
©Duane Szafron 1999
21
Recursive Vector Insertion DoneRecursive Vector Insertion Done
After, inserting “Wilma” at index 1.
“Wilma”
aVectorelementData
elementCount
anArray0 1 2 3
“Fred” “Barney” “Betty”
4
“Wilma”
Page 22
©Duane Szafron 1999
22
Some Principles from the TextbookSome Principles from the Textbook
7. Recursive structures must make “progress” towards a
principles from Bailey ch. 4
“base case”.