Stack, Queue, Tree, Graph PDF
Stack, Queue, Tree, Graph PDF
Chapter 04 -
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. This
means that the last element added to the stack is the first one to be removed. Think of it
like a stack of plates: you add plates on top and remove the top plate first.
Applications of a Stack: x2
● Function call management (recursive function calls use a stack to keep track of
return points).
● Expression evaluation (used in compilers to evaluate arithmetic or logical
expressions).
● Undo operations in text editors.
● Balancing parentheses in mathematical expressions.
● Depth-First Search (DFS) in graph traversal algorithms.
● Browser History
1. Infix Notation:
● In prefix notation, also known as Polish notation, the operator is placed before the
operands.
Example:
○ + A B (equivalent to A + B in infix)
○ * A + B C (equivalent to A * (B + C) in infix)
● In postfix notation, also known as Reverse Polish Notation (RPN), the operator is
placed after the operands.
Example:
○ A B + (equivalent to A + B in infix)
○ A B C + * (equivalent to A * (B + C) in infix)
Real-life Examples:
A primitive queue is a basic form of a queue data structure that follows the First-In-First-Out
(FIFO) principle, where the first element added to the queue will be the first one to be
removed. Primitive queues are often implemented using arrays or linked lists, and they
provide fundamental operations to manage the order of elements.
Circular Queue is a linear data structure in which the operations are performed based on
FIFO (First In First Out) principle and the last position is connected back to the first position
to make a circle. It is also called ‘Ring Buffer’. This queue is primarily used in the following
case:
Traffic system: In a computer-controlled traffic system, circular queues are used to switch on
the traffic lights one by one repeatedly as per the time set.
priority queue
A priority queue is a special type of queue in which each element is associated with a priority
and is served according to its priority. There are two types of Priority Queues. They are:
Ascending Priority Queue: Element can be inserted arbitrarily but only smallest element can
be removed. For example, suppose there is an array having elements 4, 2, 8 in the same
order. So, while inserting the elements, the insertion will be in the same sequence but while
deleting, the order will be 2, 4, 8.
Descending priority Queue: Element can be inserted arbitrarily but only the largest element
can be removed first from the given Queue. For example, suppose there is an array having
elements 4, 2, 8 in the same order. So, while inserting the elements, the insertion will be in
the same sequence but while deleting, the order will be 8, 4, 2.
What is a double-ended queue? Explain it with an example.
Double Ended Queue is also a Queue data structure in which the insertion and deletion
operations are performed at both the ends (front and rear). That means, we can insert at
both front and rear positions and can delete from both front and rear positions. Since Deque
supports both stack and queue operations, it can be used as both. The Deque data structure
supports clockwise and anticlockwise rotations in O(1) time which can be useful in certain
applications.
Trees
Chapter 06 -
Tree Data Structure is a non-linear data structure in which a collection of elements known as
nodes are connected to each other via edges such that there exists exactly one path
between any two nodes.
A tree is defined as a hierarchical data structure in which the elements (known as nodes)
are linked together via edges such that there is only one path between any two nodes of
the tree.
A Binary Search Tree (BST) is a type of binary tree where each node has
at most two children, and the tree maintains a specific order. This order
helps in efficiently performing search, insertion, and deletion operations.
1. Left Subtree: All nodes in the left subtree of a node contain values
less than the node's value.
2. Right Subtree: All nodes in the right subtree of a node contain
values greater than the node's value.
3. No Duplicate Values: Typically, no two nodes in the BST have the
same value (though this can vary in some implementations).
Introduction to Height Balanced Binary Tree
A is a type of binary tree where the difference in height between the left
and right subtrees of any node is kept within a specified limit. This balance
ensures that the tree remains relatively “flat,” allowing for efficient
operations. 1. AVL tree, red-black tree are examples of height-balanced
trees.
Balance Condition: For every node, the height difference (or balance
factor) between its left and right subtrees is usually at most 1. This makes
traversal operations faster than in unbalanced trees.
The above tree is AVL because the differences between the heights of left
and right subtrees for every node are less than or equal to 1.
Rotating the subtrees in an AVL Tree:
Left Rotation: When a node is added into the right subtree of the right subtree, if the tree
gets out of balance, we do a single left rotation.
Right Rotation: If a node is added to the left subtree of the left subtree, the AVL tree may
get out of balance, so we do a single right rotation.
Left-Right Rotation: A left-right rotation is a combination in which first left rotation takes
place after that right rotation executes.
1. A complete binary tree is a binary tree where all levels, except possibly the last, are
completely filled.
2. In the last level, nodes are filled from left to right, with no gaps.
3. It ensures efficient use of space, often used in binary heaps.
1. An almost complete binary tree is a binary tree where all nodes are as far left as
possible.
2. It is similar to a complete binary tree but allows for the last level to be incomplete.
3. The tree fills leftmost nodes first before filling the right nodes.
1. The balance factor of a node in a binary tree is the difference between the heights
of its left and right subtrees.
2. Balance Factor = Height of left subtree - Height of right subtree.
3. It is used in AVL trees to maintain balance, ensuring efficient operations like search
and insertion.
1. The balance factor is calculated by subtracting the height of the right subtree from
the height of the left subtree.
2. Balance Factor = Height of Left Subtree - Height of Right Subtree.
3. A balance factor of -1, 0, or 1 is ideal; if it's outside this range, rotations are
performed to balance the tree.
1. An ancestor of a node is any node that lies on the path from the node to the root.
2. The parent, grandparent, and so on, up to the root, are considered ancestors of the
node.
3. The root node is the ultimate ancestor of all other nodes in the tree.
1. Create: Start with an empty tree by initializing the root node as null.
2. Insert: For each new node, locate the appropriate position based on tree rules and
insert the node at that position.
3. Delete: For a given node to delete, follow one of the three cases:
a. If it has no children, simply remove it.
b. If it has one child, replace it with its child.
c. If it has two children, replace it with either the in-order predecessor or
successor.
4. Counting Leaf Nodes: Traverse through the tree and count all nodes with no
children (leaf nodes).
5. Counting Non-Leaf Nodes: Traverse through the tree and count all nodes with at
least one child (non-leaf nodes).
6. Counting Total Nodes: Traverse the entire tree and count each node to get the total
number of nodes.
Tree Traversal :
Tree Traversal is the process of visiting each node in a tree in a specific order. There are
four main types of tree traversal:
Types of Graphs :
In this method, the graph is stored in the form of the 2D matrix where
rows and columns denote vertices. Each entry in the matrix represents the
weight of the edge between those vertices.
● Construct an AVL tree for the following data: 20, 10, 30, 5, 15, 25, 35, 13, 17.
● Construct Binary Search Tree for the following data: 78, 95, 2, 57, 13, 29, 61,
10.
● Construct an AVL tree for the following data: Jan, Feb, Apr, May, July, Aug,
June.
● Construct Binary Search Tree for the following data: RAM, SITA, AMIT, JOEL,
IVAN, ASHA.
● Construct an AVL tree for the following data: WED, TUE, MON, SAT, THUR, FRI.
● Construct Binary Search Tree for the following data: 15, 30, 20, 5, 10, 2, 7.
● Construct an AVL tree for the following data: SRI, IND, AUS, FRA, CAN, DEN.
● Sort the following data using Selection Sort: 12, 11, 13, 5, 6.
● Sort the following data using Merge Sort: 45, 85, 96, 78, 34, 12, 49, 38, 18.
● Sort the following data using Quick Sort: 10, 5, 75, 62, 49, 58.
● Sort the following data using Insertion Sort: 18, 7, 22, 3, 14, 2.
● What is the degree of a vertex? Find the indegree and outdegree of each
vertex for the following graph. (Repeated 4 times)