Overview of Linear and Non-Linear Data Structures
In this lesson, you will have “review time complexities” of all the data structures studied. You will also categorize them into linear and non-linear data structures.
Now that you have learned all the popular data structures, let’s see which of them are linear and which are non-linear. This information is useful when deciding the appropriate data structure for your algorithm.
Linear data structures
In linear data structures, each element is connected to either one (the next element) or two (the next and previous) more elements. Traversal in these structures is linear. This means that insertion, deletion, and search work is in O(n).
Arrays, linked lists, stacks, and queues are all examples of linear data structures.
Non-linear data structures
The exact opposite of linear data structures are non-linear data structures. In a non-linear data structure, each element can be connected to several other data elements. Traversal is not linear; hence, search, insertion, and deletion can work in O(log n) and even O(1) time.
Trees, graphs, and hash tables are all non-linear data structures.
Time and space complexity cheat table
Here’s a quick refresher of all the complexities for the data structures that you studied in this course. This will help you compare their performances in different scenarios.
Note: In the table, n is the total number of elements stored in the structure.
Data Structure | Insertion (Average/Worst) | Deletion (Average/Worst) | Search (Average/Worst) | Space Complexity (Worst) |
---|---|---|---|---|
Array | O(n) |
O(n) |
O(n) |
O(n) |
Singly Linked List | O(1) |
O(n) |
O(n) |
O(n) |
Doubly Linked List | O(1) |
O(n) |
O(n) |
O(n) |
Stack | O(1) |
O(1) |
O(n) |
O(n) |
Queue | O(1) |
O(1) |
O(n) |
O(n) |
Binary Heap | O(logn) |
O(logn) removeMax() or removeMin() |
O(n) |
O(n) |
Binary Tree | O(n) |
O(n) |
O(n) |
O(n) |
Binary Search Tree | O(logn) / O(n) |
O(logn) / O(n) |
O(logn) / O(n) |
O(n) |
Red-Black Tree | O(logn) |
O(logn) |
O(logn) |
O(n) |
AVL Tree | O(logn) |
O(logn) |
O(logn) |
O(n) |
2-3 Tree | O(logn) |
O(logn) |
O(logn) |
O(n) |
Hash Table | O(1) / O(n) |
O(1) / O(n) |
O(1) / O(n) |
O(n) |
Trie | O(n) |
O(n) |
O(n) |
O(n) |
Complexities of graph data structure
Operation | Adjacency List | Adjacency Matrix |
---|---|---|
Add Vertex | O(1) |
O(V2) |
Remove Vertex | O(V+E) |
O(V2) |
Add Edge | O(1) |
O(1) |
Remove Edge | O(E) |
O(1) |
Depth First Search | O(V+E) |
O(V2) |
Breadth First Search | O(V+E) |
O(V2) |
Space Complexity | O(V+E) |
O(V2) |
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.