Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between 'data structure' and 'data type'?

Two of the questions that often come up in the Uni exam, where I study, are:

  • Define data types. Classify and explain datatypes
  • Define data structures. Classify and explain data structures
  • Somehow, aren't they the same thing ?
    Consider that you are making a Tree<E> in Java. You'd declare your class for Tree<E>, add methods to it and somewhere you would do Tree<String> myTree = new Tree<>(); to make a tree object.

    Your data 'structure' is now a data 'type'.
    Say if you were asked a question: Of what type is the variable myTree? The answer would be, Tree<E>. Your data 'structure' is now a data 'type'.

    Now since they are the same, they will be classified in the same way depending on what basis you want to classify them on. Primitive or non primitive. Homogeneous or heterogeneous. Linear or hierarchical.

    That is my understanding. Is the understanding wrong ?

    like image 461
    An SO User Avatar asked Sep 22 '13 06:09

    An SO User


    1 Answers

    I would like to correct the following to start - you created a class called "Tree" and an object called "myTree", not a variable called "myTree" of datatype "Tree". These are different things.

    The following is the definition of a data type:

    A data type or simply type is a classification identifying one of various types of data, such as real-valued, integer or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of that type can be stored.

    Now, as per Wikipedia, there are various definitions for "type" in data type.

    The question you have asked is a good one. There are data types in today's modern languages, that are referred to as Abstract Data Types or ADT in short. The definition of an ADT is:

    An abstract data type (ADT) is a mathematical model for a certain class of data structures that have similar behavior; or for certain data types of one or more programming languages that have similar semantics. An abstract data type is defined indirectly, only by the operations that may be performed on it and by mathematical constraints on the effects (and possibly cost) of those operations.

    It is also written that:

    Abstract data types are purely theoretical entities, used (among other things) to simplify the description of abstract algorithms, to classify and evaluate data structures, and to formally describe the type systems of programming languages. However, an ADT may be implemented by specific data types or data structures, in many ways and in many programming languages; or described in a formal specification language.

    Meaning that ADT's can be implemented using either data types or data structures.

    As for data structures:

    A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

    Many textbooks, use these words interchangeably. With more complex types, that can lead to confusion.

    Take a small example: it's something of a standard to use b-tree for implementation of databases. Meaning, we know that this type of ADT is well suited for such type of a problem and can handle it more effectively. But in order to inject this effectiveness in an ADT you will need to create a data structure that will give you the desired output.

    Another example: there are so many trees like b-tree, binary-search tree, AA tree, etc. All these are essentially of the type of a tree, but each one is in it's own a data structure.

    Refer: List of data structures for a huge list of available structures.

    like image 172
    user2339071 Avatar answered Oct 05 '22 12:10

    user2339071