简体   繁体   中英

What makes a data structure recursive?

I was reading about Recursive Data Type which has the following quote:

In computer programming languages, a recursive data type (also known as a recursively-defined, inductively-defined or inductive data type) is a data type for values that may contain other values of the same type

I understand that Linked List and Trees can be recursive data type because it contains smaller version of the same data structure, like a Tree can have a subtree.

However, it go me really confused because isn't fixed size array also contains sub-array? which is still the same type?

Can someone explain with examples that what makes a data structure recursive?

However, it go me really confused because isn't fixed size array also contains sub-array?

Conceptually, you could say that every array "contains" sub-arrays, but the arrays themselves aren't made up of smaller arrays in the code. An array is made up of a continuous chunk of elements, not other arrays.

A recursive structure (like, as you mentioned, a linked list), literally contains versions of itself:

class Node {
    Node head = null; // <-- Nodes can literally hold other Nodes
}

Whereas, if you think of an array represented as a class with fixed fields for elements, it contains elements , not other arrays:

class Array<E> {
   E elem1 = ...; // <-- In the code, an array isn't made up of other arrays,
   E elem2 = ...; //      it's made up of elements.
    ...
}

(This is a bad, inaccurate representation of an array, but it's the best I can communicate in simple code).

A structure is recursive if while navigating through it, you come across "smaller" versions of the whole structure. While navigating through an array, you'll only come across the elements that the array holds, not smaller arrays.

Note though, that this depends entirely on the implementation of the structure. In Clojure for example, "vectors" behave essentially identical to arrays, and can be thought of as arrays while using them, but internally, they're actually a tree (essentially a multi-child linked list).

A data type describes how data is stored (at least logically; on deeper layers there are only numbers, bits, transistors, etc. anyway). While an array (but only a non-empty one) can be considered as something plus another (sub-)array (such an approach is used commonly for lists in languages like Lisp and Prolog), it is usually stored element-wise.

For example, a list in Prolog is defined as either an empty list (a special case) or an element (called head ) concatenated with another list (called tail ). This makes a recursive data type.

On the other hand, a list in C can be defined as struct list { char[100] data; int length; } struct list { char[100] data; int length; } struct list { char[100] data; int length; } . Here list in not used in how list is defined (only char[] and int are used), so it's not a recursive data type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM