堆栈:堆栈是一种线性数据结构,其中元素只能从列表的一侧插入和删除,称为顶部。在栈的情况下,插入称为推操作,删除称为弹出操作。插入和删除的顺序可能是LIFO (后进先出)即,最新插入堆栈的元素也将首先插入。在堆栈中,列表中存在的最后一个元素的轨迹由一个名为top的指针进行跟踪。下面是相同的图形表示:
树:树是一个或多个节点的有限集合,使得:
- 有一个特别指定的节点,称为root 。
- 剩余的节点被划分为 N>=0 个不相交的集合 T 1 , T 2 , T 3 , …, T N ,其中T 1 , T 2 , T 3 , …, T N称为根的子树。
每个节点都有一个特定的父节点,可能有也可能没有子节点。每个节点都包含一个值和对子节点的引用。它是一种图数据结构,但没有环并且是全连接的。树的概念如下图所示:
下面是 Stack 和 Tree 之间的表格差异:
S No. |
Parameter |
Stack |
Tree |
1 | Basic Nature | Linear Data Structure | Non-Linear Data Structure |
2 | Base Notion | Top of the stack | The root of the Tree |
3 | Successor | Element pushed before the reference element | The notion of child and parent exists |
4 | Order of Insertion | Elements inserted on the TOP of the stack | Depends on the type of tree. |
5 | Order of Deletion | Elements deleted from the TOP of the stack | Depends on the type of tree. |
6 | Insertion Complexity | O(1) | Depends on the type for example AVL- O(log2N). |
7 | Deletion Complexity | O(1) | Depends on the type for example AVL- O(log2N). |
8 | Searching | O(1) | Depends on the type for example AVL- O(log2N). |
9 | Finding Min | O(N) | Depends on the type for example Min Heap- O(log2N). |
10 | Finding Max | O(N) | Depends on the type for example Min Heap- O(log2N). |
11 | IsEmpty | O(1) | Mostly O(1) |
12 | Implementation | Using arrays and linked list | Can be implemented using array and user-defined type of nodes |
13 | Types | No types exist | Many types like Binary Tree, AVL Tree, nary Tree, etc. |
14 | Applications | Expression evaluation, Backtracking, Memory management, etc. | Fast search, insert, delete, etc. |
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。