0% found this document useful (0 votes)
28 views17 pages

Stack, Queue, Tree, Graph PDF

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)
28 views17 pages

Stack, Queue, Tree, Graph PDF

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/ 17

Stack

Chapter 04 -

What is stack? (4 marks) x2

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.

Basic Operations of a Stack:

1. Push: Adds an element to the top of the stack.


2. Pop: Removes the top element from the stack.
3. Peek (or Top): Returns the top element without removing it.
4. isEmpty: Checks if the stack is empty.
5. isFull: Checks if the stack is full (in case of a fixed-size stack).

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

State the difference between stack & linked list. (2 marks)

1. Infix Notation:

● In infix notation, the operator is placed between the operands.


● This is the most common notation that we use in everyday math.
Example:
○ A+B
1. Infix Notation:

● In infix notation, the operator is placed between the operands.


● This is the most common notation that we use in everyday math.
Example:
○ A+B
○ A * (B + C)

2. Prefix Notation (Polish 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)

3. Postfix Notation (Reverse Polish Notation):

● 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)

Write an algorithm to convert a given infix expression to postfix expression.


Queue
Chapter 05 -

A Queue Data Structure is a fundamental concept in computer science


used for storing and managing data in a specific order. It follows the
principle of “First in, First out” (FIFO), where the first element added to
the queue is the first one to be removed. Queues are commonly used in
various algorithms and applications for their simplicity and efficiency in
managing data flow.

Real-life Examples:

● Printer Queue: Documents sent to a printer are printed in the order


they are received.
● Call Center: Customer calls are answered in the order they are
received.
Types of Queues
Primitive Queue with example

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 with example

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.

Node: The basic element of a tree, which contains data.


Edge: The connection between two nodes.
Root: The topmost node in a tree, from which all other nodes branch out.
Parent: A node that has one or more child nodes.
Child: A node that is a descendant of another node.
Leaf: A node that has no children.
Subtree: A portion of a tree that itself forms a tree.
Height: The longest path from a node to a leaf.
Types of Trees
binary Tree
A binary Tree is defined as a Tree data structure with at most 2 children.
Since each element in a binary tree can have only 2 children, we typically
name them the left and right child.

Binary Search Tree (BST)

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.

Properties of a Binary Search Tree:

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.

AVL Tree Data Structure

An AVL tree is defined as a self-balancing Binary Search Tree (BST)


where the difference between heights of left and right subtrees for any
node cannot be more than one.
The difference between the heights of the left subtree and the right
subtree for any node is known as the balance factor of the node.

Example of AVL 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.

Right-Rotation in AVL Tree

Left-Right Rotation: A left-right rotation is a combination in which first left rotation takes
place after that right rotation executes.

Left-Right Rotation in AVL tree

Right-Left Rotation: A right-left rotation is a combination in which first


right rotation takes place after that left rotation executes.
1. What is a complete binary tree? (2 marks )

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.

2. What is an almost complete binary tree?

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.

3. What is the balance factor? x2

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.

4. How is the balance factor calculated?

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.

5. What is the Ancestor of a Node?

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.

7. List out different types of trees.


Operations on binary tree and binary search 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:

1. In-Order Traversal (Left, Root, Right):


○ Visit the left subtree first.
○ Visit the root node.
○ Visit the right subtree.
○ Usage: In a Binary Search Tree (BST), in-order traversal results in nodes
being visited in ascending order.
2. Pre-Order Traversal (Root, Left, Right):
○ Visit the root node first.
○ Traverse the left subtree.
○ Traverse the right subtree.
○ Usage: Pre-order is often used for creating a copy of the tree or to get prefix
expressions in expression trees.
3. Post-Order Traversal (Left, Right, Root):
○ Traverse the left subtree first.
○ Traverse the right subtree.
○ Visit the root node.
○ Usage: Post-order is useful for deleting nodes or evaluating expression trees.
4. Level-Order Traversal (Breadth-First Traversal):
○ Visit nodes level by level, starting from the root.
○ Usage: Useful for finding the shortest path in an unweighted tree and is often
implemented using a queue.
Graph
Chapter 06 - Introduction to Graph Data Structure

1. Graph Data Structure is a non-linear data structure consisting of vertices and


edges.
2. It is useful in fields such as social network analysis, recommendation systems, and
computer networks. In the field of sports data science, graph data structure can be
used to analyze and understand the dynamics of team performance and player
interactions on the field.
3. The vertices are sometimes also referred to as nodes and the edges are lines or arcs
that connect any two nodes in the graph. More formally a Graph is composed of a
set of vertices( V ) and a set of edges( E ). The graph is denoted by G(V, E).

Types of Graphs :

representation techniques in detail. adjacency list of a graph.

Representation of Graph Data Structure:


Representation of Graph
Adjacency Matrix

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.

Adjacency List Representation of Graph:

This graph is represented as a collection of linked lists. There is an array of


pointer which points to the edges connected to that vertex.
Questions for solving :
4 marks questions

1. Tree Construction (AVL and Binary Search Trees)

● 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.

2. Sorting Techniques (Selection, Merge, Quick, Insertion Sort)

● 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.

3. Attempt any two of the following : [2 × 3= 6]

1. Expression Conversion (Prefix and Postfix)

● Convert the following expression into prefix:


○ i) A + B / C * (D – A) ^ F ^ H
○ ii) A * (B * C + D * E) + F
○ i) p * q – r l s
○ ii) (A + B) / (C + D * E)
● Convert the following expression into postfix:
○ i) A / B $ C D * E – A * C
○ ii) (A + B * C – D) / E $ F
○ i) (A + B) * C – D
○ ii) A + B * C – D / E * F
2. Graph-Related Questions (Degree, Indegree, Outdegree)

● What is the degree of a vertex? Find the indegree and outdegree of each
vertex for the following graph. (Repeated 4 times)

4. Linked Lists (Display, Traverse, Count, Reverse)

● Write a C-program to display a linked list in reverse order.


● Write a C-program to traverse the linked list.
● Write a C-program to create a linked list with given numbers, where the data
part of each node contains individual digits of the numbers.
● Write a C-program to count the number of nodes in a singly linked list.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy