📜  数组和树的区别

📅  最后修改于: 2021-09-12 11:09:15             🧑  作者: Mango

数组

数组是存储在连续内存位置的同类(相同类型)数据项的集合。例如,如果数组是“int”类型,则它只能存储整数元素,而不能存储其他类型的元素,例如 double、float、char 等。

  • 数组是一种线性数据结构,其中元素存储在连续的内存位置。
  • 在数组中,我们将相同数据类型的元素存储在一起。
  • 它具有基于索引的寻址,因为元素存储在连续的内存位置。
  • 索引从0开始一直到(N – 1) ,其中N是数组中的元素数。
  • 由于数组允许随机访问 O(1) 中的元素。它使按位置访问元素更快。
  • 数组最大的缺点是它的大小不能增加。

下面是数组的一般表示:

树表示由边连接的节点。具体是二叉树或二叉搜索树。二叉树是一种用于数据存储目的的特殊数据结构。二叉树有一个特殊条件,即每个节点最多可以有两个子节点。二叉树具有有序数组和链表的优点,因为搜索与排序数组一样快,插入或删除操作与链表一样快。

  • 树是从根节点开始的一组节点。
  • 每个节点都有一个特定的父节点,可能有也可能没有多个子节点。
  • 每个节点都包含一个值和对子节点的引用。
  • 它是一种图数据结构,但没有环并且是全连接的。
  • 可视化树数据结构的最佳示例是可视化自然有根的树。

数组和树之间的表格差异

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.

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。