In this post, we will understand the difference between linear data structure and non-linear data structure.
Linear Data Structure
The elements of such a structure are arranged sequentially.
Every element can be accessed by traversing through the linear structure.
All the elements of a linear structure are at a single level, i.e there is no hierarchy.
They are easy to implement and use.
They utilize more memory, hence they are not very memory-friendly.
The time complexity of linear data structure usually increases when the size of the structure increases.
Examples include- list, array, stack
The below shows an example of a list in Python.
my_list = [45, 42, 12, 34, 56, 7] print(my_list)
Output
[45, 42, 12, 34, 56, 7]
Non-linear Data Structure
The elements are stored in a hierarchical fashion.
They are connected to each other using ‘nodes’.
The elements of this structure are present at different levels, not a single level.
Their implementation is not easy.
They can’t be traversed easily- it requires multiple iterations to completely walk through a non-linear data structure.
They are memory-friendly, i.e they use memory efficiently.
The time complexity of a non-linear data structure remains same even when the size of the data increases.
Examples include- Map, tree, graph
The below example shows how a graph is defined- it indicates that node interconnection also has to be defined.
Example
graph = {'A': ['B', 'C'], 'B': ['C'], 'C': ['D', 'E'], 'D': ['C'], 'E': ['F', 'G'], 'F': ['C']}