Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursive Objects  an object that holds a reference to its own type is a recursive object  linked lists and trees are classic examples in computer science.

Similar presentations


Presentation on theme: "Recursive Objects  an object that holds a reference to its own type is a recursive object  linked lists and trees are classic examples in computer science."— Presentation transcript:

1 Recursive Objects  an object that holds a reference to its own type is a recursive object  linked lists and trees are classic examples in computer science of objects that can be implemented recursively 1

2 Linked Lists  a linked list is data structure made up of a sequence of data records where each record has s a field that contains a reference (a link) to the next record in the sequence  suppose we have a linked list that holds characters; a picture of our linked list would be: 2 'a''x''s' null 'r''a' links

3 UML Class Diagram 3 LinkedList - value : char - nextList : LinkedList...

4 LinkedList constructor 4

5 Creating a Linked List  to create the following linked list: 5 'a''x''s' null 'r''a'

6 Append  methods of recursive objects can often be implemented with a recursive algorithm  notice the word "can"; the recursive implementation is not necessarily the most efficient implementation  appending to the end of the list can be done recursively  base case: nextList is null (i.e. at the end of the list)  create new new list and append it to this link  recursive case: current link is not the last link  tell the next list to append 6

7 7

8 Getting an Element in the List  a client may wish to retrieve the ith element from a list  the ability to access arbitrary elements of a sequence in the same amount of time is called random access  arrays support random access; linked lists do not  to access the ith element in a linked list we need to sequentially follow the first (i - 1 ) links  takes O(n) time versus O( 1 ) for arrays 8 'a''x''s' null 'r''a' L.get(3) link 0link 1link 2

9  getting the ith element can be done recursively  base case: index == 0  return the value held by the current link  recursive case: current link is not the last link  tell the next list to get the element at index – 1  error case: index < 0  throw IndexOutOfBoundsException  error case: at the last link and index != 0  client has asked for an element past the end of the list  throw IndexOutOfBoundsException  setting the ith element is almost exactly the same 9

10 10

11 toString  finding the string representation of a list can be done recursively  the string is "a" + toString(the list['x', 'r', 'a', 's'])  base case: nextList is null  return the value of the link as a string  recursive case: current link is not the last link  return the value of the link as a string + the rest of the list as a string 11 'a''x''s' null 'r''a'

12 12

13 Counting Elements  suppose we want to count the number of times a value appears in the list  the number of times 'a' appears in the list is 1 + count('a' in the list['x', 'r', 'a', 's'])  note the similarity with toString 13 'a''x''s' null 'r''a'

14  base case: nextList is null  return 1 if the element matches the specified value; 0 otherwise  recursive case: current link is not the last link  element matches value: return ( 1 + number of times the value appears in the rest of the list)  element does not match value: return ( 0 + number of times the value appears in the rest of the list) 14

15 15

16 Non-recursive Implementations  the recursive structure of the linked list leads naturally to the recursive implementations of the various methods  but the methods can also be implemented using loops; for example toString 16


Download ppt "Recursive Objects  an object that holds a reference to its own type is a recursive object  linked lists and trees are classic examples in computer science."

Similar presentations


Ads by Google