Open In App

Difference between an array and a tree

Last Updated : 02 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Array:

An array is a collection of homogeneous(same type) data items stored in contiguous memory locations. For example, if an array is of type “int”, it can only store integer elements and cannot allow the elements of other types such as double, float, char, etc.

  • The array is a linear data structure in which elements are stored at contiguous memory locations.
  • In array, we store elements of the same datatype together.
  • It has index-based addressing as elements are stored at contiguous memory locations.
  • Index starts from 0 and goes up to (N - 1) where N is the number of elements in the array.
  • As the arrays allow random access of elements in O(1). It makes accessing elements by position faster.
  • The biggest disadvantage of an array is that its size cannot be increased.

Below is the general representation of the array:

Tree:

The tree represents the nodes connected by edges. The binary tree or binary search tree specifically. A binary tree is a special data structure used for data storage purposes. A binary tree has a special condition that each node can have a maximum of two children. A binary tree has the benefits of both an ordered array and a linked list as search is as quick as in a sorted array and insertion or deletion operations are as fast as in a linked list.

  • A tree is a group of nodes starting from the root node.
  • Every node has a specific parent and may or may not have multiple child nodes.
  • Each node contains a value and references to the children.
  • It's a kind of graph data structure but does not have cycles and is fully connected.
  • The best example to visualize the tree data structure is to visualize a natural rooted tree.

Tabular difference between array and tree:

ParameterArrayTree
NatureIt is a linear data structureIt is a linear non-linear data structure
Base Notion0th Index of the arrayThe root of the Tree
SuccessorElement at reference_index + 1Child nodes of the current node.
PredecessorElement at reference_index - 1Parent of the current node.
Natural IntuitionStaircase model with the base staircase as the ith indexThe best example to visualize the tree data structure is to visualize a natural rooted tree.
Order of InsertionUsually an element inserted at current_index + 1Depends on the type of tree.
Order of DeletionAt any index but after deletion elements are rearrangedDepends on the type of tree.
Insertion Complexity

O(1)->Insertion at end.

O(N)->Insertion at random index.

Depends on the type for example AVL- O(log2N).
Deletion Complexity

O(1)->Deletion from end.

O(N)->Deletion from a random index.

Depends on the type for example AVL- O(log2N).
SearchingO(N)Depends on the type for example AVL- O(log2N).
Finding MinO(N)Depends on the type for example Min Heap- O(log2N).
Finding MaxO(N)Depends on the type for example Max Heap- O(log2N).
IsEmptyO(1)Mostly O(1)
Random AccessO(1)Mostly O(N)
ApplicationArrays are used to implement other data structures, such as lists, heaps, hash tables, deques, queues and stacks.,Fast search, insert, delete, etc.

Next Article

Similar Reads