The list as a recursive data structure

• Recursive data structure

• Recursive data structure:

 Recursive data structure (also known as a recursively-defined data structure) = a data structure that contains a smaller version of itself.

• A recursive data structure looks like a Russian doll:

• List is a recursive data structure

• Why is a list a recursive data structure:

• A list contains a smaller list:

• In fact, we can define a list using the following recursive definition:

 ``` List = [ ] (empty list) or Node -> List ```

• A list can be:

 An empty list,       or An node followed by a list.

• Example:

• Why is this a list:

• Because it satisfies the recursive definition of list as follows:

• Read the diagram as follows:

 [4 9 3 7] is a list because: 4 is a node and [9 3 7] is a list [9 3 7] is a list because: 9 is a node and [3 7] is a list [3 7] is a list because: 3 is a node and [7] is a list [7] is a list because: 7 is a node and [] is a list [] is a list because: the empty list is a list

• Recursive data structure and recursion

• Fact:

• The recursive problem solving technique is ideally suited to maniplulate recursive data structures because:

 A recursive data structure contains a smaller copy of itself The smaller copy can naturally processed by another copy of the recursive method.