The Wayback Machine - https://web.archive.org/web/20240915122650/https://www.geeksforgeeks.org/difference-between-array-and-tree/
Open In App

Difference between an array and a tree

Last Updated : 02 Nov, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Medium
47.34%
57.2K

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:

Parameter Array Tree
Nature It is a linear data structure It is a linear non-linear data structure
Base Notion 0th Index of the array The root of the Tree
Successor Element at reference_index + 1 Child nodes of the current node.
Predecessor Element at reference_index – 1 Parent of the current node.
Natural Intuition Staircase model with the base staircase as the ith index The best example to visualize the tree data structure is to visualize a natural rooted tree.
Order of Insertion Usually an element inserted at current_index + 1 Depends on the type of tree.
Order of Deletion At any index but after deletion elements are rearranged Depends 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).
Searching O(N) Depends on the type for example AVL- O(log2N).
Finding Min O(N) Depends on the type for example Min Heap- O(log2N).
Finding Max O(N) Depends on the type for example Max Heap- O(log2N).
IsEmpty O(1) Mostly O(1)
Random Access O(1) Mostly O(N)
Application Arrays are used to implement other data structures, such as lists, heaps, hash tables, deques, queues and stacks., Fast search, insert, delete, etc.


Previous Article
Next Article

Similar Reads

Convert a Generic Tree(N-array Tree) to Binary Tree
Prerequisite: Generic Trees(N-array Trees) In this article, we will discuss the conversion of the Generic Tree to a Binary Tree. Following are the rules to convert a Generic(N-array Tree) to a Binary Tree: The root of the Binary Tree is the Root of the Generic Tree.The left child of a node in the Generic Tree is the Left child of that node in the B
13 min read
Difference between General tree and Binary tree
General Tree: In the data structure, General tree is a tree in which each node can have either zero or many child nodes. It can not be empty. In general tree, there is no limitation on the degree of a node. The topmost node of a general tree is called the root node. There are many subtrees in a general tree. The subtree of a general tree is unorder
2 min read
Difference between B tree and B+ tree
B-Tree: B-Tree is known as a self-balancing tree as its nodes are sorted in the inorder traversal. In B-tree, a node can have more than two children. B-tree has a height of logM N (Where ‘M’ is the order of tree and N is the number of nodes). And the height is adjusted automatically at each update. In the B-tree data is sorted in a specific order,
3 min read
Difference between Binary Tree and Binary Search Tree
Binary Tree Data Structure:A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right children.  Binary Search Tree Data Structure (BST):A binary search tree is a hierarchical data structure where each node has at most two children, w
2 min read
Difference between Binary tree and B-tree
B-Tree : B-Tree is known as a self-balancing tree as its nodes are sorted in the inorder traversal. Unlike the binary trees, in B-tree, a node can have more than two children. B-tree has a height of logM N (Where ‘M’ is the order of tree and N is the number of nodes). And the height is adjusts automatically at each update. In the B-tree data is sor
7 min read
Difference between Binary Search Tree and AVL Tree
Binary Search Tree:A binary Search Tree is a node-based binary tree data structure that has the following properties: The left subtree of a node contains only nodes with keys lesser than the node’s key.The right subtree of a node contains only nodes with keys greater than the node’s key.The left and right subtree each must also be a binary search t
2 min read
Difference between Spanning Tree Protocol (STP) and Rapid Spanning Tree Protocol (RSTP)
Spanning Tree Protocol (STP) and Rapid Spanning Tree Protocol (RSTP), are designed to avoid network loops in Ethernet. It provide loop-free logical topology. They were both designed to provide network reliability and particularly avoid broadcast radiation, but the major differences are on the rate, functionality and versatility. In this article we
5 min read
Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
In this article, we will discuss the complexity of different operations in binary trees including BST and AVL trees. Before understanding this article, you should have a basic idea about Binary Tree, Binary Search Tree, and AVL Tree. The main operations in a binary tree are: search, insert and delete. We will see the worst-case time complexity of t
4 min read
Minimum difference between any two weighted nodes in Sum Tree of the given Tree
Given a tree of N nodes, the task is to convert the given tree to its Sum Tree(including its own weight) and find the minimum difference between any two node's weight of the sum tree. Note: The N nodes of the given tree are given in the form of top to bottom with N-1 line where each line describes two nodes that are connected. Examples: Input: Outp
11 min read
Difference between Suffix Array and Suffix Tree
Suffix Array and Suffix Tree are data structures used for the efficient string processing and pattern matching. They provide the different ways to the store and query substrings each with the unique characteristics and use cases. Understanding the differences between them helps in the choosing the right data structure for the specific applications.
3 min read
Comparison between Height Balanced Tree and Weight Balanced Tree
What is Height Balanced Tree? Self-Balancing binary search trees are the height-balanced binary tree is one for which at every node, the absolute value of the difference in heights of the left and right subtree is no larger than one. An empty tree is height-balanced. A non-empty binary tree T is balanced if: The left subtree of T is balanced.The ri
4 min read
Comparison between Fenwick Tree vs Segment Tree - with Similarities and Differences
Fenwick Tree (Binary Indexed Tree) and Segment Tree are both data structures used for efficient range query and update operations on an array. Here's a tabular comparison of these two data structures. Similarities between the Fenwick tree and Segment treeHere are some of the areas where Fenwick Tree is similar to Segment Tree: Array type: Both Fenw
2 min read
What’s difference between “array” and “&array” for “int array[5]” ?
If someone has defined an array such as “int array[5]”, what’s the meaning of “array” or “&array”? Are they both same or are they different? You might be tempted to think that they both would point to the very first element of the array i.e. they both will have same address. Let us find out! To check this, the very first thing that comes to min
4 min read
Sub-tree with minimum color difference in a 2-coloured tree
A tree with N nodes and N-1 edges is given with 2 different colours for its nodes. Find the sub-tree with minimum colour difference i.e. abs(1-colour nodes - 2-colour nodes) is minimum. Example: Input : Edges : 1 2 1 3 2 4 3 5 Colours : 1 1 2 2 1 [1-based indexing where index denotes the node] Output : 2 Explanation : The sub-tree {1-2} and {1-2-3-
10 min read
Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
Given a Complete Binary Tree as an array, the task is to print all of its levels in sorted order.Examples: Input: arr[] = {7, 6, 5, 4, 3, 2, 1} The given tree looks like 7 / \ 6 5 / \ / \ 4 3 2 1 Output: 7 5 6 1 2 3 4 Input: arr[] = {5, 6, 4, 9, 2, 1} The given tree looks like 5 / \ 6 4 / \ / 9 2 1 Output: 5 4 6 1 2 9 Approach: A similar problem is
6 min read
Generate an N-length array having length of non-decreasing subarrays maximized and minimum difference between first and last array elements
Given an array arr[ ] of size N, the task is to print an N-length array whose sum of lengths of all non-decreasing subarrays is maximum and the difference between the first and last elements is minimum. Examples: Input: N = 5, arr = {4, 3, 5, 3, 2}Output: {3, 4, 5, 2, 3}Explanation: Difference between the first and last element is minimum, i.e. 3 -
6 min read
Difference between Tree Topology and Bus Topology
1. Tree Topology: Tree topology is a type of network topology in which the nodes are arranged in the design of a tree. In tree topology, branching cable starts from the node at the head top known as the root node. The branching cable having no loops connect the root with all other nodes in hierarchical manner for communication. Tree topology is mor
2 min read
Difference between sums of odd position and even position nodes for each level of a Binary Tree
Given a Binary Tree, the task is to find the absolute difference between the sums of odd and even positioned nodes. A node is said to be odd and even positioned if its position in the current level is odd and even respectively. Note that the first element of each row is considered as odd positioned.Examples: Input: 5 / \ 2 6 / \ \ 1 4 8 / / \ 3 7 9
8 min read
Difference between Star Topology and Tree Topology
1. Star Topology: Star topology is a type of network topology in which all the nodes are connected to the central hub or router. In star topology, connections are in the way that for n nodes, n number of links are needed. Star topology is less complex due to its simplicity. Star topology is less expensive than tree topology. 2. Tree Topology: Tree
3 min read
Difference between Decision Table and Decision Tree
1. Decision Table: Decision Table is just a tabular representation of all conditions and actions. Decision Table are always used whenever the processing logic is very complicated and involves multiple conditions. The main components used for the formation of the Data Table are Conditions Stubs, Action Stubs, and rules. Types of decision tables: Ext
3 min read
Difference between sum of even and odd valued nodes in a Binary Tree
Given a binary tree, the task is to find the absolute difference between the even valued and the odd valued nodes in a binary tree. Examples: Input: 5 / \ 2 6 / \ \ 1 4 8 / / \ 3 7 9 Output: 5 Explanation: Sum of the odd value nodes is: 5 + 1 + 3 + 7 + 9 = 25 Sum of the even value nodes is: 2 + 6 + 4 + 8 = 20 Absolute difference = (25 – 20) = 5. In
10 min read
Difference between sums of odd level and even level nodes in an N-ary Tree
Given an N-ary Tree rooted at 1, the task is to find the difference between the sum of nodes at the odd level and the sum of nodes at even level. Examples: Input: 4 / | \ 2 3 -5 / \ / \ -1 3 -2 6Output: 10Explanation:Sum of nodes at even levels = 2 + 3 + (-5) = 0Sum of nodes at odd levels = 4 + (-1) + 3 + (-2) + 6 = 10Hence, the required difference
9 min read
Difference between Minimum Spanning Tree and Shortest Path
Spanning tree: A spanning tree (T) of an undirected graph(G) is a subgraph which is a tree that includes all the vertices of a graph (G) and the minimum number of edges required to connect the graph (G). And it is a known maximal set of edges with no cycles. Properties: If a graph(G) is not connected then it does not contain a spanning tree (i.e. i
4 min read
Difference between Stack and Tree
Stack: A Stack is a linear data structure in which elements can be inserted and deleted only from one side of the list, called the top. Insertion is called push operation and deletion is called pop operation in case of the stack. The order of insertion and deletion may be LIFO(Last In First Out) i.e., the element inserted latest to the stack will a
2 min read
Difference between Tree edge and Back edge in graph
Tree Edge: It is an edge that is present in the tree obtained after performing DFS on the graph. All the Green edges are tree edges as shown in the below image. Back Edge: It is an edge (u, v) such that v is an ancestor of node u but not part of the DFS Traversal of the tree. Edge from 5 to 4 is a back edge. The presence of a back edge indicates a
1 min read
Difference between odd level and even level leaf sum in given Binary Tree
Given a Binary Tree, the task is to find the difference of the sum of leaf nodes at the odd level and even level of the given tree. Examples: Input: Output: -12Explanation: Following are the operations performed to get the result.odd_level_sum = 0, even_level_sum = 0Level 1: No leaf node, so odd_level_sum = 0Level 2: No leaf node, so even_level_sum
13 min read
Difference between Full and Complete Binary Tree
A binary tree is a type of data structure where each node can only have two offspring at most named as “left” and “right” child. There are different types of binary tree but here we are going to discuss about the difference of Complete binary tree and Full binary tree. Full Binary Tree: A full binary tree is a binary tree in which all of the nodes
4 min read
What is the difference between Heap and Red-Black Tree?
What is Heap? A Heap is a special Tree-based data structure in which the tree is a complete binary tree. There are two types of heap - Min heap and Max heap. To learn more about heap, go through the following article: Introduction to Heap What is a Red-Black Tree?The red-Black tree is a self-balancing binary search tree in which each node contains
2 min read
Maximum difference between node and its ancestor in Binary Tree
Given a Binary tree, The task is to find the maximum value by subtracting the value of node B from the value of node A, where A and B are two nodes of the binary tree and A is an ancestor of B. Examples: Input: Output: 7Explanation: We can have various ancestor-node difference, some of which are given below : 8 – 3 = 5 , 3 – 7 = -4, 8 – 1 = 7, 10 –
10 min read
Difference Between Graph and Tree
Graphs and trees are two fundamental data structures used in computer science to represent relationships between objects. While they share some similarities, they also have distinct differences that make them suitable for different applications. What is Graph?A graph data structure is a collection of nodes (also called vertices) and edges that conn
2 min read