Which of the following principle does tree use?
(A). hierarchical data structure
(B). FIFO only
(C). Linear data structure
(D). None of these
Question’s Answer: hierarchical data structure
FIFO means first element added to the queue is the first one to be removed from queue.
Data Structure | Principle |
Linked List | linear data structure without an inherent ordering principle |
Array | linear data structure without an inherent ordering principle |
Stack | LIFO |
Queue | FIFO |
Tree | hierarchical data structure without an inherent ordering principle |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <iostream> using namespace std; class Node { public: int data; Node* left; Node* right; Node(int value) { data = value; left = NULL; right = NULL; } }; void inorderTraversal(Node* node) { if(node == NULL) { return; } inorderTraversal(node->left); cout << node->data << " "; inorderTraversal(node->right); } int main() { Node* root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(4); root->left->right = new Node(5); root->right->left = new Node(6); root->right->right = new Node(7); cout << "Binary tree elements: "; inorderTraversal(root); return 0; } |
Output
Binary tree elements: 4 2 5 1 6 3 7