DSA (Lalit)
DSA (Lalit)
0|Page
INDEX
Sr. No. Content Page No.
2 Assignments associated with linked list, operations on linked lists and 5-8
their applications.
1|Page
1 – Creation of matrices, linear lists and
operations.
Code:
Output:
2|Page
Code:
Output:
3|Page
Code:
Output:-
4|Page
2-Linked list & operations on LL
Code:
5|Page
6|Page
7|Page
Output:
8|Page
3- Array and Linked list implementation of
stacks and queues.
Stack using Array
Code: Output:
9|Page
Queue using Array
Code:
10 | P a g e
Output:
11 | P a g e
Queue using Linked list
Code:
12 | P a g e
Output:
13 | P a g e
Stack using Linked list
Code:
14 | P a g e
Output:
15 | P a g e
4-Trees and their applications
Introduction
Trees: Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees are
hierarchical data structures.
Tree Vocabulary: The topmost node is called root of the tree. The elements that are directly
under an element are called its children. The element directly above something is called its
parent. For example, ‘a’ is a child of ‘f’, and ‘f’ is the parent of ‘a’. Finally, elements with no
children are called leaves.
Why Trees?
16 | P a g e
Binary Tree: A tree whose elements have at most 2 children is called a binary tree. Since each
element in a binary tree can have only 2 children, we typically name them the left and right child.
1. Data
2. Pointer to left child
3. Pointer to right child
Properties
17 | P a g e
The following are the examples of Perfect Binary Trees:
In a Perfect Binary Tree, the number of leaf nodes is the number of internal nodes plus 1
L = I + 1 Where L = Number of leaf nodes, I = Number of internal nodes.
A Perfect Binary Tree of height h (where the height of the binary tree is the longest path from the
root node to any leaf node in the tree, height of root node is 1) has 2h – 1 node.
An example of a Perfect binary tree is ancestors in the family. Keep a person at root, parents as
children, parents of parents as their children.
B-Tree
18 | P a g e
1. All leaves are at the same level.
2. A B-Tree is defined by the term minimum degree ‘t’. The value of t depends upon disk
block size.
3. Every node except root must contain at least (ceiling)([t-1]/2) keys. The root may contain
minimum 1 key.
4. All nodes (including root) may contain at most t – 1 keys.
5. Number of children of a node is equal to the number of keys in it plus 1.
6. All keys of a node are sorted in increasing order. The child between two keys k1 and k2
contains all keys in the range from k1 and k2.
7. B-Tree grows and shrinks from the root which is unlike Binary Search Tree. Binary
Search Trees grow downward and also shrink from downward.
8. Like other balanced Binary Search Trees, time complexity to search, insert and delete is
O(log n).
AVL tree
19 | P a g e
1. Manipulate hierarchical data.
2. Make information easy to search (see tree traversal).
3. Manipulate sorted lists of data.
4. As a workflow for compositing digital images for visual effects.
5. Router algorithms
6. Form of a multi-stage decision-making (see business chess).
Other Applications
1. Store hierarchical data, like folder structure, organization structure, XML/HTML data.
2. Binary Search Tree is a tree that allows fast search, insert, delete on a sorted data. It also
allows finding closest item
3. Heap is a tree data structure which is implemented using arrays and used to implement
priority queues.
4. B-Tree and B+ Tree: They are used to implement indexing in databases.
5. Syntax Tree: Used in Compilers.
6. K-D Tree: A space partitioning tree used to organize points in K dimensional space.
7. Trie: Used to implement dictionaries with prefix lookup.
8. Suffix Tree: For quick pattern searching in a fixed text.
9. Spanning Trees and shortest path trees are used in routers and bridges respectively in
computer networks
10. As a workflow for compositing digital images for visual effects.
20 | P a g e
5-Graph Data Stucture
In this tutorial, you will learn what a Graph Data Structure is. Also, you will find representations
of a graph.
A graph data structure is a collection of nodes that have data and are connected to other nodes.
Let's try to understand this through an example. On facebook, everything is a node. That includes
User, Photo, Album, Event, Group, Page, Comment, Story, Video, Link, Note...anything that has
data is a node.
Every relationship is an edge from one node to another. Whether you post a photo, join a group,
like a page, etc., a new edge is created for that relationship.
All of facebook is then a collection of these nodes and edges. This is because facebook uses a
graph data structure to store its data.
A collection of vertices V
A collection of edges E, represented as ordered pairs of vertices
(u,v)
21 | P a g e
Graph Terminology
Adjacency: A vertex is said to be adjacent to another vertex if there is an edge connecting them.
Vertices 2 and 3 are not adjacent because there is no edge between them.
Path: A sequence of edges that allows you to go from vertex A to vertex B is called a path. 0-1,
1-2 and 0-2 are paths from vertex 0 to vertex 2.
Directed Graph: A graph in which an edge (u,v) doesn't necessarily mean that there is an edge
(v, u) as well. The edges in such a graph are represented by arrows to show the direction of the
edge.
Graph Representation
1. Adjacency Matrix
An adjacency matrix is a 2D array of V x V vertices. Each row and column represent a vertex.
If the value of any element a[i][j] is 1, it represents that there is an edge connecting vertex i and
vertex j.
Since it is an undirected graph, for edge (0,2), we also need to mark edge (2,0); making the
adjacency matrix symmetric about the diagonal.
22 | P a g e
Edge lookup(checking if an edge exists between vertex A and vertex B) is extremely fast in
adjacency matrix representation but we have to reserve space for every possible link between all
vertices(V x V), so it requires more space.
2. Adjacency List
The index of the array represents a vertex and each element in its linked list represents the other
vertices that form an edge with the vertex.
The adjacency list for the graph we made in the first example is as follows:
An adjacency list is efficient in terms of storage because we only need to store the values for the
edges. For a graph with millions of vertices, this can mean a lot of saved space.
Graph Operations
23 | P a g e