...
Sign in
Computer Science

Recursive data types

Recursive data types

Share:

Recursive data types

Introduction / Definition

A recursive data type refers to itself in its definition. It naturally models lists, trees, and nested structures. A recursive definition normally contains a base case and a recursive case. Algorithms can then mirror this structure.

Syntax / Structure / Formula

List<T> = Empty | Node(T, List<T>)

Detailed Explanation

A list is either empty or a node containing a value and another list. Binary trees can similarly contain child trees.

Example

Example: Node(10, Node(20, Empty)).

Output / Result

The structure contains two values and terminates at Empty.

Important Points / Notes

  • Always define a base/empty case.
  • Lists and trees are common examples.

Mini Practice Question

Give a recursive definition of a binary tree.


CheckpointTest Yourself

In type theory and software design, why must every well-founded Recursive (Inductive) Data Type define at least one non-recursive Base Constructor?

Lesson Milestone

Finished Reading This Lesson?

Mark this lesson complete. A chapter counts as done once all its lessons are, and that adds to your course progress.

TH

Written by

ThinkWisely Team

Have a Question or Suggestion?

We'd love to hear from you. Drop your thoughts below to help us improve ThinkWisely!