0% found this document useful (0 votes)
10 views26 pages

Unit3 and Unit-4

Uploaded by

Aman Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views26 pages

Unit3 and Unit-4

Uploaded by

Aman Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Tree Data Structure

● A tree data structure is defined as a collection of objects or entities known as


nodes that are linked together to represent or simulate hierarchy.
● A tree data structure is a non-linear data structure because it does not store in
a sequential manner. It is a hierarchical structure as elements in a Tree are
arranged in multiple levels.
● In the Tree data structure, the topmost node is known as a root node. Each
node contains some data, and data can be of any type. In the above tree
structure, the node contains the name of the employee, so the type of data
would be a string.
● Each node contains some data and the link or reference of other nodes that
can be called children.

Some basic terms used in Tree data structure

Let's consider the tree structure, which is shown below:

In the above structure, each node is labeled with some number. Each arrow
shown in the above figure is known as a link between the two nodes.

● Root: The root node is the topmost node in the tree hierarchy. In other words,
the root node is the one that doesn't have any parent. In the above structure,
node numbered 1 is the root node of the tree. If a node is directly linked to
some other node, it would be called a parent-child relationship.
● Child node: If the node is a descendant of any node, then the node is known
as a child node.
● Parent: If the node contains any sub-node, then that node is said to be the
parent of that sub-node.
● Sibling: The nodes that have the same parent are known as siblings.
● Leaf Node:- The node of the tree, which doesn't have any child node, is
called a leaf node. A leaf node is the bottom-most node of the tree. There can
be any number of leaf nodes present in a general tree. Leaf nodes can also
be called external nodes.
● Internal nodes: A node has atleast one child node known as an internal

Binary Tree
The Binary tree means that the node can have maximum two children. Here, binary
name itself suggests that 'two'; therefore, each node can have either 0, 1 or 2 children.
Let's understand the binary tree through an example.

The above tree is a binary tree because each node contains the utmost two
children.
A binary tree data structure is represented using two methods. Those methods are as follows...

1. Array Representation
2. Linked List Representation

Consider the following binary tree...

1.Array Representation of Binary Tree


In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent a
binary tree.
Consider the above example of a binary tree and it is represented as follows...

To represent a binary tree of depth 'n' using array representation, we need one dimensional
array with a maximum size of 2n + 1.

2. Linked List Representation of Binary Tree


We use a double linked list to represent a binary tree. In a double linked list, every node consists
of three fields. First field for storing left child address, second for storing actual data and third for
storing right child address.
In this linked list representation, a node has the following structure...

The above example of the binary tree represented using Linked list representation is shown as
follows...

Tree traversal (Inorder, Preorder an Postorder)


The term 'tree traversal' means traversing or visiting each node of a tree. There is a
single way to traverse the linear data structure such as linked list, queue, and stack.
Whereas, there are multiple ways to traverse a tree that are listed as follows -
● Preorder traversal
● Inorder traversal
● Postorder traversal

Preorder traversal
This technique follows the 'root left right' policy. It means that, first root node is visited
after that the left subtree is traversed recursively, and finally, right subtree is recursively
traversed. As the root node is traversed before (or pre) the left and right subtree, it is
called preorder traversal.

The applications of preorder traversal include -


● It is used to create a copy of the tree.
● It can also be used to get the prefix expression of an expression tree.
Algorithm
​ Until all nodes of the tree are not visited

​ Step 1 - Visit the root node
​ Step 2 - Traverse the left subtree recursively.
​ Step 3 - Traverse the right subtree recursively.

Postorder traversal
This technique follows the 'left-right root' policy. It means that the first left subtree
of the root node is traversed, after that recursively traverses the right subtree,
and finally, the root node is traversed. As the root node is traversed after (or post)
the left and right subtree, it is called postorder traversal.
The applications of postorder traversal include -
● It is used to delete the tree.
● It can also be used to get the postfix expression of an expression tree.
Algorithm
​ Until all nodes of the tree are not visited

​ Step 1 - Traverse the left subtree recursively.
​ Step 2 - Traverse the right subtree recursively.
​ Step 3 - Visit the root node.

Inorder traversal
This technique follows the 'left root right' policy. It means that first left subtree is visited
after that root node is traversed, and finally, the right subtree is traversed. As the root
node is traversed between the left and right subtree, it is named inorder traversal.
The applications of Inorder traversal includes -
● It is used to get the BST nodes in increasing order.
● It can also be used to get the prefix expression of an expression tree.
Algorithm
​ Until all nodes of the tree are not visited

​ Step 1 - Traverse the left subtree recursively.
​ Step 2 - Visit the root node.
​ Step 3 - Traverse the right subtree recursively.

What is a Binary Search tree?


A binary search tree follows some order to arrange the elements. In a Binary search
tree, the value of left node must be smaller than the parent node, and the value of right
node must be greater than the parent node. This rule is applied recursively to the left
and right subtrees of the root.

In the above figure, we can observe that the root node is 40, and all the nodes of
the left subtree are smaller than the root node, and all the nodes of the right
subtree are greater than the root node.
Similarly, we can see the left child of root node is greater than its left child and
smaller than its right child. So, it also satisfies the property of binary search tree.
Therefore, we can say that the tree in the above image is a binary search tree.

Advantages of Binary search tree


● Searching an element in the Binary search tree is easy as we always have a
hint that which subtree has the desired element.
● As compared to array and linked lists, insertion and deletion operations are
faster in BST.

Example of creating a binary search tree


Now, let's see the creation of binary search tree using an example.
Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50

● First, we have to insert 45 into the tree as the root of the tree.
● Then, read the next element; if it is smaller than the root node, insert it as the
root of the left subtree, and move to the next element.
● Otherwise, if the element is larger than the root node, then insert it as the root
of the right subtree.

Now, let's see the process of creating the Binary search tree using the given data
element. The process of creating the BST is shown below -
Step 1 - Insert 45.

Step 2 - Insert 15.


As 15 is smaller than 45, so insert it as the root node of the left subtree.

Step 3 - Insert 79.

As 79 is greater than 45, so insert it as the root node of the right subtree.

As 79 is greater than 45, so insert it as the root node of the right subtree.
Step 4 - Insert 90.
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.
Step 5 - Insert 10.
10 is smaller than 45 and 15, so it will be inserted as a left subtree of 15.

Step 6 - Insert 55.


55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree of 79.

Step 7 - Insert 12.


12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the right
subtree of 10.
Step 8 - Insert 20.
20 is smaller than 45 but greater than 15, so it will be inserted as the right subtree of 15.

Step 9 - Insert 50.


50 is greater than 45 but smaller than 79 and 55. So, it will be inserted as a left subtree
of 55.

Searching in Binary search tree


Searching means to find or locate a specific element or node in a data structure. In
Binary search tree, searching a node is easy because elements in BST are stored in a
specific order. The steps of searching a node in Binary Search tree are listed as follows -

1. First, compare the element to be searched with the root element of the tree.
2. If root is matched with the target element, then return the node's location.
3. If it is not matched, then check whether the item is less than the root element, if it is
smaller than the root element, then move to the left subtree.
4. If it is larger than the root element, then move to the right subtree.
5. Repeat the above procedure recursively until the match is found.
6. If the element is not found or not present in the tree, then return NULL.

Now, let's understand the searching in binary tree using an example. We are taking the
binary search tree formed above. Suppose we have to find node 20 from the below tree.
Step1:
Step2:

Step3:

Deletion in Binary Search tree


In a binary search tree, we must delete a node from the tree by keeping in mind that the
property of BST is not violated. To delete a node from BST, there are three possible
situations occur -

● The node to be deleted is the leaf node, or,


● The node to be deleted has only one child, and,
● The node to be deleted has two children

When the node to be deleted is the leaf node


It is the simplest case to delete a node in BST. Here, we have to replace the leaf node
with NULL and simply free the allocated space.
We can see the process to delete a leaf node from BST in the below image. In below
image, suppose we have to delete node 90, as the node to be deleted is a leaf node, so
it will be replaced with NULL, and the allocated space will free.
When the node to be deleted has only one child
In this case, we have to replace the target node with its child, and then delete the child
node. It means that after replacing the target node with its child node, the child node will
now contain the value to be deleted. So, we simply have to replace the child node with
NULL and free up the allocated space.
We can see the process of deleting a node with one child from BST in the below image.
In the below image, suppose we have to delete the node 79, as the node to be deleted
has only one child, so it will be replaced with its child 55.
So, the replaced node 79 will now be a leaf node that can be easily deleted.

When the node to be deleted has two children


This case of deleting a node in BST is a bit complex among other two cases. In such a
case, the steps to be followed are listed as follows -

● First, find the inorder successor of the node to be deleted.


● After that, replace that node with the inorder successor until the target node is
placed at the leaf of tree.
● And at last, replace the node with NULL and free up the allocated space.

The inorder successor is required when the right child of the node is not empty. We can
obtain the inorder successor by finding the minimum element in the right child of the
node.
We can see the process of deleting a node with two children from BST in the below
image. In the below image, suppose we have to delete node 45 that is the root node, as
the node to be deleted has two children, so it will be replaced with its inorder successor.
Now, node 45 will be at the leaf of the tree so that it can be deleted easily.
Insertion in Binary Search tree
A new key in BST is always inserted at the leaf. To insert an element in BST, we have to
start searching from the root node; if the node to be inserted is less than the root node,
then search for an empty location in the left subtree. Else, search for the empty location
in the right subtree and insert the data. Insert in BST is similar to searching, as we
always have to maintain the rule that the left subtree is smaller than the root, and right
subtree is larger than the root.
Now, let's see the process of inserting a node into BST using an example.

AVL Tree
AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is named
AVL in honour of its inventors.
AVL Tree can be defined as height balanced binary search tree in which each node is
associated with a balance factor which is calculated by subtracting the height of its right
sub-tree from that of its left sub-tree.
Tree is said to be balanced if balance factor of each node is in between -1 to 1,
otherwise, the tree will be unbalanced and need to be balanced.
Balance Factor (k) = height (left(k)) - height (right(k))

AVL Rotations
We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and 1.
There are basically four types of rotations which are as follows:

1. L L rotation: Inserted node is in the left subtree of left subtree of A


2. R R rotation : Inserted node is in the right subtree of right subtree of A
3. L R rotation : Inserted node is in the right subtree of left subtree of A
4. R L rotation : Inserted node is in the left subtree of right subtree of A

1. RR Rotation
When BST becomes unbalanced, due to a node is inserted into the right subtree of the
right subtree of A, then we perform RR rotation, RR rotation is an anticlockwise rotation,
which is applied on the edge below a node having balance factor -2

In above example, node A has balance factor -2 because a node C is inserted in the
right subtree of A right subtree. We perform the RR rotation on the edge below A.

2. LL Rotation
When BST becomes unbalanced, due to a node is inserted into the left subtree of the
left subtree of C, then we perform LL rotation, LL rotation is clockwise rotation, which is
applied on the edge below a node having balance factor 2.

In above example, node C has balance factor 2 because a node A is inserted in the left
subtree of C left subtree. We perform the LL rotation on the edge below A.

3. LR Rotation
Double rotations are bit tougher than single rotation which has already explained above.
LR rotation = RR rotation + LL rotation, i.e., first RR rotation is performed on subtree and
then LL rotation is performed on full tree, by full tree we mean the first node from the
path of inserted node whose balance factor is other than -1, 0, or 1.
Let us understand each and every step very clearly:

State Action

A node B has been inserted into the


right subtree of A the left subtree of C,
because of which C has become an
unbalanced node having balance factor
2. This case is L R rotation where:
Inserted node is in the right subtree of
left subtree of C

As LR rotation = RR + LL rotation,
hence RR (anticlockwise) on subtree
rooted at A is performed first. By doing
RR rotation, node A, has become the
left subtree of B.
After performing RR rotation, node C is
still unbalanced, i.e., having balance
factor 2, as inserted node A is in the left
of left of C

Now we perform LL clockwise rotation


on full tree, i.e. on node C. node C has
now become the right subtree of node
B, A is left subtree of B

Balance factor of each node is now


either -1, 0, or 1, i.e. BST is balanced
now.

4. RL Rotation
As already discussed, that double rotations are bit tougher than single rotation which has
already explained above. R L rotation = LL rotation + RR rotation, i.e., first LL rotation is
performed on subtree and then RR rotation is performed on full tree, by full tree we
mean the first node from the path of inserted node whose balance factor is other than -1,
0, or 1.

State Action

A node B has been inserted into the left


subtree of Cthe right subtree of A,
because of which A has become an
unbalanced node having balance factor
- 2. This case is RL rotation where:
Inserted node is in the left subtree of
right subtree of A

As RL rotation = LL rotation + RR
rotation, hence, LL (clockwise) on
subtree rooted at C is performed first.
By doing RR rotation, node Chas
become the right subtree of B.

After performing LL rotation, node Ais


still unbalanced, i.e. having balance
factor -2, which is because of the
right-subtree of the right-subtree node
A.

Now we perform RR rotation


(anticlockwise rotation) on full tree, i.e.
on node A. node C has now become
the right subtree of node B, and node A
has become the left subtree of B.

Balance factor of each node is now


either -1, 0, or 1, i.e., BST is balanced
now.

Graph
A graph can be defined as group of vertices and edges that are used to connect
these vertices. A graph can be seen as a cyclic tree, where the vertices (Nodes)
maintain any complex relationship among them instead of having parent child
relationship.

Definition
A graph G can be defined as an ordered set G(V, E) where V(G) represents the set of
vertices and E(G) represents the set of edges which are used to connect these vertices.
A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B), (B,C), (C,E), (E,D),
(D,B), (D,A)) is shown in the following figure.

Directed and Undirected Graph


A graph can be directed or undirected. However, in an undirected graph, edges are not
associated with the directions with them. An undirected graph is shown in the above
figure since its edges are not attached with any of the directions. If an edge exists
between vertex A and B then the vertices can be traversed from B to A as well as A to B.
In a directed graph, edges form an ordered pair. Edges represent a specific path from
some vertex A to another vertex B. Node A is called initial node while node B is called
terminal node.

Graph Terminology
Path
A path can be defined as the sequence of nodes that are followed in order to reach
some terminal node V from the initial node U.

Closed Path
A path will be called as closed path if the initial node is same as terminal node. A path
will be closed path if V0=VN.
Simple Path
If all the nodes of the graph are distinct with an exception V0=VN, then such path P is
called as closed simple path.

Cycle
A cycle can be defined as the path which has no repeated edges or vertices except the
first and last vertices.

Connected Graph
A connected graph is the one in which some path exists between every two vertices (u,
v) in V. There are no isolated nodes in connected graph.

Complete Graph
A complete graph is the one in which every node is connected with all other nodes. A
complete graph contain n(n-1)/2 edges where n is the number of nodes in the graph.

Weighted Graph
In a weighted graph, each edge is assigned with some data such as length or weight.
The weight of an edge e can be given as w(e) which must be a positive (+) value
indicating the cost of traversing the edge.

Digraph
A digraph is a directed graph in which each edge of the graph is associated with some
direction and the traversing can be done only in the specified direction.

Loop
An edge that is associated with the similar end points can be called as Loop.

Adjacent Nodes
If two nodes u and v are connected via an edge e, then the nodes u and v are called as
neighbours or adjacent nodes.

Degree of the Node


A degree of a node is the number of edges that are connected with that node. A node
with d

Graph representation
Advertisement
In this article, we will discuss the ways to represent the graph. By Graph representation,
we simply mean the technique to be used to store some graph into the computer's
memory.
A graph is a data structure that consist a sets of vertices (called nodes) and edges.
There are two ways to store Graphs into the computer's memory:

● Sequential representation (or, Adjacency matrix representation)


● Linked list representation (or, Adjacency list representation)

In sequential representation, an adjacency matrix is used to store the graph. Whereas in


linked list representation, there is a use of an adjacency list to store the graph.
In this tutorial, we will discuss each one of them in detail.
Now, let's start discussing the ways of representing a graph in the data structure.

Sequential representation
In sequential representation, there is a use of an adjacency matrix to represent the
mapping between vertices and edges of the graph. We can use an adjacency matrix to
represent the undirected graph, directed graph, weighted directed graph, and weighted
undirected graph.
If adj[i][j] = w, it means that there is an edge exists from vertex i to vertex j with weight w.
An entry Aij in the adjacency matrix representation of an undirected graph G will be 1 if
an edge exists between Vi and Vj. If an Undirected Graph G consists of n vertices, then
the adjacency matrix for that graph is n x n, and the matrix A = [aij] can be defined as -
aij = 1 {if there is a path exists from Vi to Vj}
aij = 0 {Otherwise}
It means that, in an adjacency matrix, 0 represents that there is no association exists
between the nodes, whereas 1 represents the existence of a path between two edges.
If there is no self-loop present in the graph, it means that the diagonal entries of the
adjacency matrix will be 0.
Now, let's see the adjacency matrix representation of an undirected graph.

In the above figure, an image shows the mapping among the vertices (A, B, C, D, E),
and this mapping is represented by using the adjacency matrix.
There exist different adjacency matrices for the directed and undirected graph. In a
directed graph, an entry Aij will be 1 only when there is an edge directed from Vi to Vj.

Adjacency matrix for a directed graph


In a directed graph, edges represent a specific path from one vertex to another vertex.
Suppose a path exists from vertex A to another vertex B; it means that node A is the
initial node, while node B is the terminal node.
Consider the below-directed graph and try to construct the adjacency matrix of it.
In the above graph, we can see there is no self-loop, so the diagonal entries of the
adjacent matrix are 0.
Adjacency matrix is easier to implement and follow. An adjacency matrix can be used
when the graph is dense and a number of edges are large.
Though, it is advantageous to use an adjacency matrix, but it consumes more space.
Even if the graph is sparse, the matrix still consumes the same space.

Linked list representation


An adjacency list is used in the linked representation to store the Graph in the
computer's memory. It is efficient in terms of storage as we only have to store the values
for edges.
Let's see the adjacency list representation of an undirected graph.

In the above figure, we can see that there is a linked list or adjacency list for every node
of the graph. From vertex A, there are paths to vertex B and vertex D. These nodes are
linked to nodes A in the given adjacency list.

An adjacency list is maintained for each node present in the graph, which stores the
node value and a pointer to the next adjacent node to the respective node. If all the
adjacent nodes are traversed, then store the NULL in the pointer field of the last node of
the list.
The sum of the lengths of adjacency lists is equal to twice the number of edges present
in an undirected graph.
Now, consider the directed graph, and let's see the adjacency list representation of that
graph.
For a directed graph, the sum of the lengths of adjacency lists is equal to the number of
edges present in the graph.
Now, consider the weighted directed graph, and let's see the adjacency list
representation of that graph.

In the case of a weighted directed graph, each node contains an extra field that is called
the weight of the node.
In an adjacency list, it is easy to add a vertex. Because of using the linked list, it also
saves space.

BFS algorithm
Breadth-first search is a graph traversal algorithm that starts traversing the graph
from the root node and explores all the neighboring nodes. Then, it selects the
nearest node and explores all the unexplored nodes. While using BFS for
traversal, any node in the graph can be considered as the root node.

Applications of BFS algorithm


The applications of breadth-first-algorithm are given as follows -
● BFS can be used to find the neighboring locations from a given source
location.
● In a peer-to-peer network, BFS algorithm can be used as a traversal method
to find all the neighboring nodes. Most torrent clients, such as BitTorrent,
uTorrent, etc. employ this process to find "seeds" and "peers" in the network.
● BFS can be used in web crawlers to create web page indexes. It is one of the
main algorithms that can be used to index web pages. It starts traversing from
the source page and follows the links associated with the page. Here, every
web page is considered as a node in the graph.
● BFS is used to determine the shortest path and minimum spanning tree.
● BFS is also used in Cheney's technique to duplicate the garbage collection.
● It can be used in ford-Fulkerson method to compute the maximum flow in a
flow network.

Breadth First Search (BFS) algorithm starts at the tree root and explores all
nodes at the present depth prior to moving on to the nodes at the next depth
level.

As in the example given above, BFS algorithm traverses from A to B to E to F


first then to C and G lastly to D. It employs the following rules.
● Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display
it. Insert it in a queue.
● Rule 2 − If no adjacent vertex is found, remove the first vertex from
the queue.
● Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.

Step Traversal Description

1 Initialize the queue.

We start from visiting


2 S(starting node), and
mark it as visited.
We then see an
unvisited adjacent node
from S. In this
example, we have
3
three nodes but
alphabetically we
choose A, mark it as
visited and enqueue it.

Next, the unvisited


adjacent node from S
4
is B. We mark it as
visited and enqueue it.

Next, the unvisited


adjacent node from S
5
is C. We mark it as
visited and enqueue it.

Now, S is left with no


unvisited adjacent
6
nodes. So, we dequeue
and find A.

From A we have D as
unvisited adjacent
7
node. We mark it as
visited and enqueue it.

Breadth First Search: S A B C D

Depth First Search (DFS) Algorithm


Depth First Search (DFS) algorithm is a recursive algorithm for searching all
the vertices of a graph or tree data structure. This algorithm traverses a
graph in a depthward motion and uses a stack to remember to get the next
vertex to start a search, when a dead end occurs in any iteration.

As in the example given above, DFS algorithm traverses from S to A to D to


G to E to B first, then to F and lastly to C. It employs the following rules.
● Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display
it. Push it in a stack.
● Rule 2 − If no adjacent vertex is found, pop up a vertex from the
stack. (It will pop up all the vertices from the stack, which do not have
adjacent vertices.)
● Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
Step Traversal Description

1 Initialize the stack.

Mark S as visited and


put it onto the stack.
Explore any unvisited
adjacent node from S.
We have three nodes
2
and we can pick any of
them. For this
example, we shall take
the node in an
alphabetical order.
Mark A as visited and
put it onto the stack.
Explore any unvisited
adjacent node from A.
3
Both S and D are
adjacent to Abut we
are concerned for
unvisited nodes only.

Visit D and mark it as


visited and put onto
the stack. Here, we
have Band C nodes,
4 which are adjacent to
Dand both are
unvisited. However, we
shall again choose in
an alphabetical order.

We choose B, mark it
as visited and put onto
the stack. Here Bdoes
5
not have any unvisited
adjacent node. So, we
pop B from the stack.

We check the stack top


for return to the
previous node and
6 check if it has any
unvisited nodes. Here,
we find D to be on the
top of the stack.

Only unvisited adjacent


node is from D is
7 Cnow. So we visit C,
mark it as visited and
put it onto the stack.

As C does not have any unvisited adjacent node so we keep popping the
stack until we find a node that has an unvisited adjacent node. In this case,
there's none and we keep popping until the stack is empty.
Difference between BFS and DFS
The following are the important differences between BFS and DFS −

Key BFS DFS

BFS stands for Breadth DFS stands for Depth


Definition
First Search. First Search.

BFS uses a Queue to DFS uses a Stack to


Data structure
find the shortest path. find the shortest path.

BFS is better when DFS is better when

Source target is closer to target is far from


Source. source.

As BFS considers all DFS is more suitable


neighbor so it is not for decision tree. As
suitable for decision with one decision, we
Suitability for decision tree used in puzzle need to traverse
tree games. further to augment the
decision. If we reach
the conclusion, we
won.

BFS is slower than DFS is faster than BFS.


Speed
DFS.

Time Complexity of Time Complexity of


BFS = O(V+E) where V DFS is also O(V+E)
Time Complexity
is vertices and E is where V is vertices and
edges. E is edges.

BFS requires more DFS requires less


Memory
memory space. memory space.
In BFS, there is no In DFS, we may be
Tapping in loops problem of trapping trapped into infinite
into finite loops. loops.

BFS is implemented DFS is implemented


Principle using FIFO (First In using LIFO (Last In
First Out) principle. First Out) principle.

You might also like