public class List { int key; int value; List next; // This is a "reference" (= address) variable !! } |
A List object has the following structure:
The offset of the
field key from the
base address is
0
The offset of the
field value from the
base address is
4
The offset of the
field next from the
base address is
8
Sample list:
|
To understand how you can come up with a recursive algorithm, you must understand the recursive nature of a linked list:
Due to the recursive nature of a linked list, we can use divide and conquer to solve the original problem using the solution of a smaller problem:
|
When we write the recursive lookup algorithm for an (input) list:
int Lookup( List h, int searchKey ) { // We are developing this algorith right now... } |
we must be aware what the parameters represent !!!
The input parameter h will represent the original list:
So the Lookup( ) method will be like this:
int Lookup( List h, int searchKey )
{
if ( list h is empty )
return 0; // Not found is represented by 0
else if ( key in first list element == searchKey )
return (value in first list element);
else if ( key in 2nd list element == searchKey )
return (value in 2nd list element);
else if ( key in 3rd list element == searchKey )
return (value in 3rd list element);
and so on....
}
|
Notice that the work done in the highlighted area is the task:
|
So we can replace this code using a Lookup( ) call to search the sublist:
int Lookup( List h, int searchKey )
{
if ( list h is empty )
return 0; // Not found is represented by 0
else if ( key in first list element == searchKey )
return (value in first list element);
else
return Lookup( h.next, searchKey );
}
|
So in essence, we have implemented the following search algorithm:
The if-statement:
if ( key in first list element == searchKey ) |
is the (1) Search here part.
And the else part:
return Lookup( h.next, searchKey );
|
is the (2) Lookup in sub-list part.
(And we must handle the empty list as a special case or else the empty list will cause an error).
(You just need to use Java expressions to replace the pseudo code in the description)
The Lookup method in Java (without the class, we can add that later) is:
static int Lookup( List h, int searchKey ) { if ( h == null ) return 0; // Not found is represented by 0 else if ( h.key == searchKey ) return (h.value); else return Lookup( h.next, searchKey ); } |
In CS170 or CS171, you would have learn that the cases h == null and h.key == searchKey are called the base cases.
I hope with this approach, you get a feel on how the recursion was introduced:
|
How to run the program:
|
And you are lazy and refuse to traverse the linked list.
The only action that you want to do is:
|
Convince yourself that this search procedure will find the correct value:
|
/* --------------------------------------------------- Lookup( ) describes how a lazy guy will lookup a search key in a list that start at h --------------------------------------------------- */ int Lookup( List h, int searchKey ) { /* ----------------------------------------------------- I (the lazy guy) will only look at the first element if the list is not empty ----------------------------------------------------- */ if ( list h is empty ) return not found else if ( key of first element in list == searchKey ) return value in first element else { Hire someone to find "searchKey" in sublist (h.next); Take what he give me and return it } } |
Again: you should convince yourself that this lazy guy algorithm will achieve the desired result (and find the value) --- you must have faith that the hired searcher is competent and he will do his job correctly.
If you translates these actions into Java constructs, you will also get the same algorithm as discussed above:
static int Lookup( List h, int searchKey ) { /* ----------------------------------------------------- I (the lazy guy) will only look at the first element if the list is not empty ----------------------------------------------------- */ if ( h == null ) return 0; // Not found is represented by 0 else if ( h.key == searchKey ) return (h.value); else // Lookup(h.next, searchKey) = Hire someone to find "searchKey" // in sublist (h.next) return Lookup( h.next, searchKey ); } |