Information Technology is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Classification of data structures

Classification of data structures: clear definitions and categories (primitive/non-primitive, linear/non-linear, static/dynamic, homogeneous/heterogeneous, ADT vs implementation) and guidance on choosing the right structure for common problems.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Data structures determine how data is stored and accessed; choosing the right one affects program correctness, time and space efficiency, and maintainability. Engineers use this classification when designing software modules (e.g., compilers, databases, network routing, GUIs) to pick a suitable representation or ADT before implementing algorithms studied elsewhere in the course.

Key ideas

Start from first principles: a program manipulates values. How we organise these values in memory and what operations are allowed is the subject of data structures.

  • Data structure: a concrete way to store and organise data in memory so that operations (like read, insert, delete, traverse) can be performed efficiently. (Use: implementation-level concept: arrays, linked lists, trees, graphs, hash tables, etc.)

  • Abstract Data Type (ADT): a mathematical model of a data type that defines the set of values and operations on those values, together with the semantics (preconditions and postconditions), but not the implementation details. Example: Stack ADT specifies push, pop, isEmpty, top, and their effects; it does not say whether an array or linked list is used. Assumption: ADT specifies behaviour irrespective of physical storage.

  • Primitive data types: built-in, lowest-level types provided by a language or machine (examples: integer, real/float, character, boolean). Their sizes are defined by the language/ABI and may span one or more machine words; do not assume a single-word size across platforms.

  • Non-primitive data types: composed from primitive types (examples: array, record/struct, list, tree, graph). These require organisation rules and often pointers or multiple memory words.

Classification axes (each is defined, then consequences):

  1. By structure (logical shape):

    • Linear data structures: elements form a sequence where each element (except ends) has a unique predecessor and successor. Examples: array, linked list, stack, queue. Assumption: there exists a natural single ordering.
    • Non-linear data structures: elements relate in hierarchical or network ways; an element may have multiple predecessors or successors. Examples: tree, graph, heap.

    To draw for an exam: for a linear structure draw a horizontal sequence of boxes labelled with indexes or next pointers; for a non-linear (tree) draw a node at top labelled Root with two child branches down to left and right nodes; label parent-child edges.

  2. By homogeneity:

    • Homogeneous: all elements are of the same type (e.g., array of int).
    • Heterogeneous: elements may be of different types (e.g., record/struct).

    State assumption: languages like C++ allow both; in statically-typed languages the element type is declared.

  3. By storage allocation:

    • Static (fixed-size) data structures: size is fixed after creation and cannot grow without reallocation (typical example: fixed-size array). Assumption: often contiguous memory for arrays; 'static' here refers to size, not storage duration.
    • Dynamic data structures: allow growth and shrink at runtime using dynamic memory allocation (examples: linked list, dynamic arrays (vector), trees). Assumption: runtime allocator (heap) available.
  4. By implementation view:

    • Physical (internal) data structure: the actual memory layout (e.g., contiguous memory for arrays, nodes with pointers for linked lists).
    • Logical (abstract) data structure: the abstract organisation and allowed operations (e.g., list, set, map). The same logical structure can have different physical implementations. Terminology note: 'external-memory data structures' refers to disk/SSD-optimized structures (e.g., B-trees) and is unrelated to 'logical vs physical.' Common physical forms are 'sequential (contiguous) representation' and 'linked representation.'
  5. By access method:

    • Sequential access: elements accessed one after another (e.g., tape, linked list traversal).
    • Random access (direct access): any element can be accessed in O(1) time by index (assumption: contiguous memory), e.g., arrays.
  6. By mutability/persistence (short note):

    • Ephemeral (mutable) structures change in-place.
    • Persistent structures preserve previous versions after updates, typically via structural sharing; operations return new versions rather than overwriting. (This is important in functional programming and some concurrency contexts; 'immutable' often describes each version, but persistence does not forbid creating new versions.)

Comparison and roles:

  • An ADT specifies what operations must do; a data structure is one way to implement an ADT. For exams, state: "ADT ≠ implementation; ADT = interface + semantics; Data structure = concrete representation."

  • Common mappings: Stack ADT → array (static) or linked list (dynamic); Map ADT → hash table (average O(1) lookup) or balanced BST (O(log n) worst-case). Refer to other topics (Hash functions; Binary search trees; AVL trees) for performance tradeoffs.

Assumptions to state in answers: memory model (random-access machine), basic arithmetic and pointer costs O(1), and that primitive operations are constant-time unless otherwise noted.

Formulas

Data structure = logical organisation + physical representation — Data structure defined: logical rules (ordering/links) plus how stored in memory; no SI unit; applies when explaining differences between ADT and implementation.

ADT = {values, operations, semantics} — ADT components: set of possible values, allowed operations, and the semantics (pre/postconditions); no SI unit; use when defining ADT formally.

array declaration (C++) : type name[size] — e.g., int a[10]; type is element type, name identifier, size number of elements (compile-time constant in plain C++ arrays); applies when remembering static array syntax. In C/C++, array indices start at 0.

struct node { T data; node* next; }; — pattern to remember for singly linked list node in C++; T element type; next pointer; use when implementing lists.

std::vector<T> v; — C++ dynamic array (STL) declaration; T element type; use when you want automatic resizing.

Note: These are syntax reminders to memorise for exam programs. No SI units apply.

Worked examples

  1. Model answer: "Define ADT and explain difference from data structure with examples."

Answer (model):

  • Abstract Data Type (ADT): A mathematical model defined by the set of values it can hold and the operations permitted on those values with their semantics (pre/postconditions).
  • Data structure: A concrete implementation of an ADT describing the in-memory layout and algorithms for operations.
  • Example: Stack ADT: operations push(x), pop(), top(), isEmpty(); semantics: push(x) adds x, pop removes and returns top (precondition: stack not empty). Implementation options: array (fixed capacity), linked list (dynamic).
  • Key difference: ADT describes what and semantics; data structure describes how.
  1. Typical long question: "Classify the following and justify: array, linked list, binary search tree, graph, record/struct."

Answer (model):

  • Array: Non-primitive; linear; homogeneous; typically static (unless dynamic array used); random access. Draw: sequence of boxes indexed 0..n-1.
  • Linked list: Non-primitive; linear; homogeneous; dynamic; sequential access. Draw: nodes as boxes with data and pointer fields linked by arrows.
  • Binary Search Tree (BST): Non-primitive; non-linear (hierarchical); dynamic; homogeneity of key type; logical structure: each node with left/right child, BST property: left subtree keys < node key < right subtree keys.
  • Graph: Non-primitive; non-linear (network); dynamic or static; represented physically by adjacency list (list of neighbours) or adjacency matrix (2D array). Provide brief pros and cons: adjacency list memory O(V+E), adjacency matrix O(V^2).
  • Record/struct: Non-primitive; heterogeneous; static layout of named fields; often used to group related data.
  1. Design/choose DS: "You need to implement a symbol table for a compiler where average lookup must be fast; justify choice."

Model answer:

  • Candidate ADT: Map/Dictionary (key = identifier name, value = symbol info).
  • Implementation choices: hash table (with chaining or open addressing) gives average O(1) expected lookup, insert; balanced BST (e.g., AVL or red-black) gives O(log n) worst-case. For compiler where average case matters and keys are strings, choose hash table with a good string hash function and chaining to handle collisions; justify by expected performance and simpler average-case behaviour. Mention switching to balanced BST if adversarial inputs or strict worst-case guarantees needed.

Common mistakes

  • Saying arrays are always static — forget dynamic arrays/vectors exist.
  • Mixing up linear vs sequential access: arrays are linear and random-access; linked lists are linear but sequential-access.
  • Writing "graph is a tree" or assuming tree and graph are separate categories without noting that tree is a special graph.
  • Not stating assumptions (e.g., memory model, constant-time primitive operations) when comparing implementations.
  • Incorrectly claiming adjacency matrix uses O(V + E) space (it uses O(V^2)).

For your exam

  • Short notes: compare two or three structures (array vs linked list; adjacency list vs adjacency matrix). Full marks: table or bullet points comparing memory, access, typical operations and when to use each, with assumptions stated.
  • Long answers/derivations: explain conversion from ADT to implementation (e.g., implement Stack ADT by array and by linked list, showing operations and complexity). Full marks: include pseudocode or code skeleton, state pre/postconditions, and explain trade-offs.
  • Diagrams: draw array box sequence with indices; linked list nodes with data and next pointer arrows; small tree with root and children labelled. Full marks: clean diagram and labels, and brief explanation.
  • Programs: implement basic node struct or array declarations in C++ if asked. Full marks: correct compilable snippet and sample I/O if required.

Quick check

  1. Define ADT in one sentence.
  2. Is a binary tree linear or non-linear? Give a one-line reason.
  3. Which representation (adjacency list or adjacency matrix) is better for a sparse graph and why?

Answers: 1) ADT: A mathematical model defined by the set of values and the operations (with semantics) allowed on those values, independent of implementation. 2) Non-linear — because nodes can have multiple children and there is a hierarchical parent-child relation, not a single sequence. 3) Adjacency list — because it uses O(V + E) space which is much less than O(V^2) for sparse graphs (E << V^2).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?