Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Difference between Stack and Tree

  • Last Updated : 14 Dec, 2020

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 also be inserted first. In stack, the track of the last element present in the list is tracked with a pointer called top. Below is the diagrammatic representation of the same:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Tree: A Tree tree is a finite set of one or more nodes such that:



  • There is a specially designated node called root.
  • The remaining nodes are partitioned into N>=0 disjoint sets T1, T2, T3, …, TN, where T1, T2, T3, …, TN is called the subtree of the root.

Each node has a specific parent and may or may not have a child node. 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 concept of a tree is represented in the below diagram:

Below is the tabular difference between the Stack and Tree:

S No.

Parameter

Stack

Tree

1Basic NatureLinear Data StructureNon-Linear Data Structure
2Base NotionTop of the stackThe root of the Tree
3SuccessorElement pushed before the reference elementThe notion of child and parent exists
4Order of InsertionElements inserted on the TOP of the stackDepends on the type of tree.
5Order of DeletionElements deleted from the TOP of the stackDepends on the type of tree.
6Insertion ComplexityO(1)Depends on the type for example AVL- O(log2N).
7Deletion ComplexityO(1)Depends on the type for example AVL- O(log2N).
8SearchingO(1)Depends on the type for example AVL- O(log2N).
9Finding MinO(N)Depends on the type for example Min Heap- O(log2N).
10Finding MaxO(N)Depends on the type for example Min Heap- O(log2N).
11IsEmptyO(1)Mostly O(1)
12ImplementationUsing arrays and linked listCan be implemented using array and user-defined type of nodes
13TypesNo types existMany types like Binary Tree, AVL Tree, nary Tree, etc.
14ApplicationsExpression evaluation, Backtracking, Memory management, etc.Fast search, insert, delete, etc.
My Personal Notes arrow_drop_up
Recommended Articles
Page :