Problem Statement
Which of the following is a non-linear data structure?
Explanation
A tree is a non-linear data structure because elements are arranged in a hierarchical manner, where each node can have multiple children. Data is not stored sequentially or in a linear order.
Linear data structures like arrays, linked lists, and queues store elements in a sequential manner where each element has a predecessor and successor, except for the first and last elements. Non-linear structures like trees and graphs allow for more complex relationships between elements.
Code Solution
SolutionRead Only
// Linear data structure - Array
int arr[] = {1, 2, 3, 4, 5};
// Non-linear data structure - Binary Tree
class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
}