00:00

Linear Vs Non-Linear Data Structures

Last updated:

Every data structure answers one basic question: how are the elements connected to each other? The answer puts it into one of two groups, and that group tells you a lot about how the structure is stored, searched and traversed.

The two groups are:

  1. Linear Data Structure
  2. Non-Linear Data Structure
Classification of data structures into linear (static and dynamic) and non-linear (tree and graph)

What is Linear data structure?

A linear data structure keeps its elements in one ordered line. Apart from the two ends, each element has exactly one neighbour before it and one after it, much like people standing in a queue. Because of this, a single loop from the first element to the last visits everything.

Common linear data structures are arrays, linked lists, stacks and queues.

Linear data structures can also be described as static or dynamic:

  • Static Data Structure: the capacity is fixed when the structure is created. A Java array such as new int[5] can never hold a sixth element; you would have to create a bigger array and copy the values across.

  • Dynamic Data Structure: the size changes as elements are added and removed, so memory is used only for the data that actually exists. Linked lists are dynamic, and so are stacks and queues built on a linked list or on a growable array such as ArrayList or ArrayDeque.

What is Non-Linear Data Structure?

In a non-linear data structure one element can be linked to many others, so the data branches instead of forming a line. A tree branches from a single root into parent and child nodes; a graph can connect any node to any other node, even in cycles.

These structures are the right tool when the relationships between items matter as much as the items themselves: file systems, company hierarchies, route maps and recommendation networks.

Trees and graphs are the two main non-linear data structures.

Difference between Linear and Non-linear Data Structures

CharacteristicLinear Data StructureNon-Linear Data Structure
Arrangement of Data ElementsElements form one sequence; each has at most one predecessor and one successor.Elements branch out: a hierarchy (tree) or a network (graph).
ApplicationsSimple data storage, task scheduling, function call management.Complex data representation like organizational hierarchies, social networks, network routing.
Memory UtilizationDepends on the structure: an array is very compact; a linked list adds one reference per node.Usually needs more memory per element, because each node stores several references (children or edges).
TraversalOne pass from the first element to the last visits every element.Needs a traversal strategy (depth-first or breadth-first, using recursion, a stack or a queue) to reach every element.
Connections per ElementEach element (except the first and last) has exactly one predecessor and one successor.In a tree, each node has one parent (except the root) and any number of children. In a graph, a node can connect to any number of other nodes; there is no parent or child.
AdvantagesSimple implementation makes it easier to understand and work with.Efficient for hierarchical and networked data and supports complex relationships.
DisadvantagesSome operations are slow (for example, searching an unsorted list is O(n)), and it cannot model branching relationships directly.More complex to implement, can require more memory and management.
ExamplesArrays, linked lists, stacks and queues.Trees (including heaps) and graphs.
Example Use Cases
  • Arrays for indexing
  • Stacks for reversing strings
  • Queues for task scheduling.
  • Trees for database indexing
  • Graphs for network analysis
  • Heaps for priority queues