0% found this document useful (0 votes)
126 views

Dsa Notes

Uploaded by

parthpathak2604
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

Dsa Notes

Uploaded by

parthpathak2604
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Unit - I

Basic Concepts ofData


Structures

What is Data Structure?


Data and Information.

Types of Data Structure


Primitive Data Structure:
Primitive Data
Structure:
Non Primitive Data
Structure:
Operations on Data
Structure:
Algorithms:
ALGORITHM: FIND AVERAGE OF N
NUMBER
Assume that array contains n integer
values.
Complexity of
Algorithms
Array:
Characteristics of Array
One dimensional
Array:
One dimensional
Array:
Operations on One
dimensional Array:
Insertion: This operation is used to
insert an element into one
dimensional array.
Deletion: This operation is used to delete
an element from one
dimensional array.
Searching: This operation is
used to search particular
element in one dimensional
array.
Find max Number from Array: This
operation is used to find the largest
element of an array.
Two Dimensional Array
(1) Row-major Arrays
(2) Column-major Arrays
(2) Column-major Arrays
Search algorithms
(2)Binary Search
Compare Sequential
Search with
Unit- II Strings
Strings and their
re12resentation
String Character set
Various string mani
ulation functions
• String Manipulation Functions:
- String length: this function find the
length of the string
- String copy: this function copy one
string in to another string
- String concatenation: this
function concatenate two string
and store it in to the another
strirng
- String compare: this function
compare two string
- substring: this function
finds one string into the
another string
- Reverse string: this operation
is used to reverse the given
string.

-
Algorithm to copy one
string in to
another string
Algorithm to concatenate
two strings.
for compare two string
for compare two string
Algorithm to find
whether a second
string is a substring of
first string
Algorithm to reverse the
string.
UNIT-3.STACKS AND
QUEUES
Stack data
structure:
Algorithms for Stack
Operation
PUSH(S,TOP,VAL)
(2) Algorithm for POP
Operation.
POP{S,TOP)
Example
Applications of Stack :
Recursion:
• Recursion means function call
itself.
• It is the technique of defining a
process in terms of itself.
• Stack is widely used in recursion
because of its Last in First out
Property.
• In terms of C language the function
calling itself is known as recursi•on.
• There are two types of recursion:
•Primitive Recursive
function: Example Factorial
Function
• Non primitive
Recursive function:
Example Ackerman's
function {not in your
syllabus).
Applications of Stack :
Recursion:
Applications of Stack :
Rules for converting infix
notation to
Examples polish
conversion
Examples reverse polish
conversion
A lications of Stack :
Queue
Queue
QDELETE (Q, Front, Rear)
Example
Circular Q.ueue
For example consider the following
operations:
CQINSERT (Q, Front, Rear, N, Val)
CQDELETE (Q, FRONT, REAR,N)
Com arison of LIFO
and FIFO
Unit 4
Pointers in C
A pointer is a variable whose
value is the address of another
variable, i.e., direct address of the
memory location.
Structure in c
Structure in
c
Structure using
pointers
Dynamic Memory
Allocation in C
PROGRAM
Linked list
Linked list
Linked list
Traversing a Linked
List
Count nodes from
Linked List
Searching for a Value in
a Linked List
Inserting a New Node in
a Linked List
Inserting a Node at the
Beginning of a Linked
List
Inserting a Node at the
Beginning of a Linked
List
Inserting a Node at the
Beginning of a Linked
List
Inserting a Node at the
End of a
Inserting a Node at the
End of a
Linked List
Inserting a Node at the
End of a Linked List
Inserting a Node After a
Given Node in a Linked
List
Inserting a Node After a
Given Node in a Linked
List
Inserting a Node After a
Given Node in a Linked
List
Node in a Linked List
Suppose we want to add a new node
with value 9 before the node containing
3.
Inserting a Node Before a
Given Node in a Linked
List
Inserting a Node Before a
Given Node in a Linked
List
Deleting a Node from a
Linked List
Case 1: The first node is
deleted.
Case 2: The last node is
deleted.
Case 3: The node after a given
node is deleted.
Deleting the First Node
from a
Deleting the First Node
from a Linked List
DELETEFI RST(START)
Deleting the Last Node
from a
Linked List
Deleting the Last Node
from a
Deleting the Last Node
from a
Linked List
Deleting a Node After
a Given Node in a
Linked List
Suppose we want to delete the
node that succeeds the node
which contains data value 3.
Deleting a Node After a
Given Node in a Linked
List
Deleting a Node After
a Given Node in a
Linked List
CIRCULAR LINKED
LISTS
DOUBLY LINKED
LISTS
DOUBLY LINKED
LISTS
DOUBLY
LINKED LISTS
Inserting a Node at the
Beginning of a Doubly
Linked List
INSERTBEG_DQ(START)
Inserting a Node at the
End end of a Doubly
Linked List
Inserting a Node at the
End end of a Doubly
Linked List
Move PTR so that it points to the last
node of the list. Add the new node after
the node pointed by PTR.
Step 1: IF AVAIL== NULL
Inserting a Node After a
Given Node in a
Doubly Linked List
Suppose we want to add a new
node with value 9 after the node
containing 3.
INSERTAFTERNUM_DLL(START)
Inserting a Node
before a Given Node
in a Doubly Linked
List
Supposewe want to add a new
node with value 9 before the node
containing 3.
Inserting a Node before a
Given Node in a Doubly
Linked List
INSERTB4NUM_DLL(START)
Deleting a Node from a
Doubly Linked List
Case 1: The first node is
deleted.
Case 2: The last node is deleted.
Case 3: The node after a given
node is deleted.
Case 4: The node before a given
node is deleted.
Deleting the First Node
from a Doubly Linked
List
DELETEFIRST _DLL(START)
Deleting the Last Node
from a
Doubly Linked List
Deleting the Last
Node from a
Doubly Linked List
Move PTR so that it now points to the
last node of the Ii st.
DELETELAST_DLL(STARn
APPLICATIONS OF
LINKED LISTS
Polynomial
Representation
Difference between
Linked list and
Unit 5
TREES
Root node : The root node R is the
topmost node in the tree. If R =
NULL, then it means the tree is empty.
Sub-trees : If the root node R is not
NULL, then the trees Tl, T2, and T3
are called the sub-trees of R.
Descendant node : A descendant node
is any successor node on any path from
the node to a leaf node. Leaf nodes do
not have any descendants. In the tree
given in Fig. 6.1, nodes C, G, J, and Kare
the descendants of node A.
TYPES OF TREES
General Trees
Forests
Binary Trees
Root node
level higher than its parents. So all
child nodes are defined to have level
number as parent's level number + 1.
Leaf node : A node that has no children
is called a leaf node or a terminal node.
The leaf nodes in the tree are: 8, 9, 5, 10,
11, and 12.
Similar binary trees : Two binary trees
T and T1 are said to
In-degree/out-degree of a node It is the
number of edges arriving at a node. The
root node is the only node that has an in-
degree equal to zero. Similarly, out-
degree of a node is the number of edges
To make a file more beautiful, you can:

1. Use consistent formatting: Ensure that


headings, subheadings, and paragraph
styles are consistent throughout the
document. This makes it easier to read
and visually appealing.
2. Add colors and visuals: Incorporate a
color scheme and use it consistently
throughout the file. You can also include
relevant visuals, such as graphs, charts,
or high-quality images, to enhance the
aesthetics.
3. Choose an appealing font: Select a
clean and professional font that is easy
to read. Avoid using too many different
fonts, as it can make the file look
cluttered.
4. Use appropriate spacing: Use proper
line spacing and margins to create a
balanced and visually pleasing layout.
Avoid large blocks of text, and break up
information into smaller sections or bullet
points for better readability.
5. Include tables and columns: When
presenting data or organizing
information, use tables or columns to
keep the content structured and visually
appealing.
6. Add a cover page or title: Start the file
with an attractive cover page or title that
captures the essence of the content. This
helps create a professional and polished
look.
7. Apply consistent styles: Use styles and
formatting options consistently, such as
bold or italics, to add emphasis where
needed. This consistency improves the
overall visual appeal and readability.
8. Proofread and edit: Eliminate any
grammatical or spelling errors, as they
can negatively affect the overall
impression of the file.
9. Consider the document type: Adjust
the design elements based on the
purpose of the file. For example, a
presentation may require larger fonts
and more visuals, while a formal report
may focus on a professional and clean
layout.
10. Seek feedback: Ask for feedback
from others on the appearance of the file.
Getting different perspectives can help
identify areas for improvement and make
the file more visually appealing overall.

Remember that the main goal is to create


a visually pleasing and easily
understandable document. Balance
aesthetics with usability to make the file
both beautiful and functional.

To format your current Word files in a


professional look, you can follow these
steps:

1. Page Layout: Go to the "Page Layout"


tab in the Word ribbon and choose an
appropriate page orientation (Portrait or
Landscape) and set margins according
to your preference.

2. Font Style and Size: Select a


professional font style such as Arial,
Calibri, or Times New Roman for your
text. Set a standard font size (usually
between 10-12) for the body text and
adjust headings accordingly.

3. Consistent Heading Styles: Use the


built-in heading styles to format your
section and subsection headings. This
creates a consistent and hierarchical
structure throughout your document. You
can modify the heading styles in the
"Home" tab's "Styles" group.

4. Line Spacing: Use appropriate line


spacing to enhance readability. You can
select "1.5" or "double" spacing to make
your text more readable.

5. Alignment and Justification: Avoid


using centered alignment for the body
text; instead, left-align your text for a
professional look. Justify your text if
required to create a clean and polished
appearance.

6. Indentations: Use consistent


indentations for paragraphs to maintain
readability. You can set your desired
indentation value using the indentation
options under the "Page Layout" tab.

7. Bulleted or Numbered Lists: Utilize


bullet points or numbered lists to
organize information. Ensure that the
indentation, spacing, and alignment are
consistent for all lists.

8. Tables and Figures: If your document


includes tables or figures, make sure
they are properly aligned, labeled, and
formatted. Adjust column widths, font
sizes, and cell formatting to enhance
appearance and readability.

9. Headers and Footers: Consider


adding headers and footers to provide
additional information such as page
numbers, document title, or date. This
creates a polished and professional look
for your document.

10. Proofreading and Spell Check:


Always proofread your document for
grammar, spelling, and punctuation
errors. Additionally, run a spell-check to
catch any typos or mistakes that may
have been overlooked.

11. Page Breaks: Insert page breaks to


ensure that new sections or chapters
start on new pages, giving your
document a clean and organized
structure.

12. Consistent Formatting: Maintain


consistency throughout your document
in terms of font styles, sizes, spacing,
headings, and other formatting elements.

By following these steps, you can


transform your Word files into a more
professional-looking format. Remember
to save your changes regularly and
adjust formatting as needed based on
your specific requirements.
leaving that node.
Representation of Binary Trees
in the Memory
Creating a Binary Tree from a
General Tree
Step 2: Left child of node A is the
leftmost child of node A in the general
tree and right child of node A is the right
sibling of the node A in the general tree.
Since node A has no right sibling in the
general tree, it has no right child in the
binary tree.
Step 5: Now process node D. Left child
of D is I (leftmost child). There will be no
right child of D because it has no right
sibling in the general tree.
Step 6: Now process node I. There will
be no left child of I
TRAVERSING A BINARY TREE
ALGORITHM PREORDER(TREE):
EXAMPLE: For trees in fig 5.11 find the
sequence of nodes that will be visited using
pre-order traversal algorithm.
In-order Traversal :
To traverse a non-empty binary
tree in in-order, the
Consider the tree given in Fig. 5.10.
The post-order traversal of the tree is
given as B, C, and A. Left sub-tree first,
the right sub-tree next, and finally the
root node. In this algorithm, the left
sub-tree is always traversed
Searching for a Node in a Binary
Search Tree
The search function is used to find
whether a given value is present in the
tree or not. The searching process
begins at the root node. The function
first checks if the binary
(Step 1)
(Step 1,
Algorithm SearchElement (TREE, VAL)
Step 1: IF TREE-> DATA= VAL OR TREE=
NULL
Inserting a New Node in a Binary
Search Tree
(Step 1)
Case 2: Deleting a Node with One
Child
Case 3: Deleting a Node with Two
Children
To handle this case, replace the node's
value with its in- order predecessor
(largest value in the left sub-tree) or in-
order successor (smallest value in the
right sub-tree). The in-order
predecessor or the successor can then
be deleted using any of the above
cases. Look at the binary search tree
given in Fig. and see how deletion of
node with value 56 is handled.
(Step 1)
This deletion could also be handled by
replacing node 56 with its in-order
successor, s shown in Fig.
If the node to be deleted does not have
any child, then we simply set the node
to NULL. Last but not the least, if the
node to be deleted has either a left or a
right child but not both, then the current
node is replaced by its child node and
the initial child node is deleted from the
tree.
Finding the Largest Node in a Binary
Search Tree:
To find the node with the largest value,
we find the value of the rightmost
node of the right subtree. However, if
the right sub-tree is empty, then the root
node will be the largest value in the
tree.
APPLICATIONS OF BST
Unit 6
Sorting algorithms
BUBBLE SORT
Example
Pass 2:
Pass 3:
Pass 4:
Pass 5:
Pass 6:
Algorithm
INSERTION
SORT
Technique:
EXAMPLE
Step 1: Repeat Steps 2 to 5 for K = 1
to N - 1
SELECTION SORT
Example
SMALLEST {ARR, K, N, POS)
Step 1: [INITIALIZE] SET SMALL= ARR[K]

MERGE SORT
•A smaller list takes fewer steps and
thus less time to sort than a large list.
EXAMPLE
Step 1: [INITIALIZE] SET I= BEG, J = MID+ 1,
INDEX= 0
ELSE
QUICK SORT
Technique
EXAMPLE
RADIX SORT
Algorithm for RadixSort (ARR, N)
Step 1: Find the largest number in
ARR as LARGE
EXAMPLE
345,654,924,123,567,472,5
55,808,911
Hashing and Collision
In a small company of 100 employees,
each employee is assigned an
Emp_lD in the range 0-99. To store
the records in an array, each
employee's Emp_lD acts as an index
into the array where the employee's
record will be stored as shown in Fig.
we need a way to convert a five-digit
key number to a two-digit array index.
We need a function which will do the
transformation. In this case, we will use
the term hash table for an array and the
function that will carry out the
transformation will be called a hash
function.
HASH FUNCTIONS
Solution We will use A = 0.618033, m =
1000, and k =
To make a file more
beautiful, you can follow
these suggestions:

1. Use consistent formatting:


Ensure that headings,
subheadings, and body text
are formatted consistently
throughout the file. Consider
using a clean and readable
font.

2. Add visuals: Incorporate


images, charts, or graphs to
enhance the visual appeal of
the file. Make sure these
visuals are relevant and add
value to the content.

3. Create a hierarchy: Use


appropriate headings,
subheadings, and bullet
points to improve readability
and make the file easier to
navigate. Consider using
different font sizes, bold, or
italics to emphasize
important points.
4. Add color and style:
Choose a color scheme that
complements the content and
use it consistently throughout
the file. Avoid using too
many colors or
overwhelming the reader
with loud graphics.

5. Use whitespace
effectively: Utilize
whitespace to create
breathing room between
sections and paragraphs. This
helps improve clarity and
readability.

6. Check for grammar and


spelling errors: Make sure the
file is proofread for any
grammatical or spelling
mistakes. Correcting these
errors can significantly
improve the overall look and
feel of the document.
7. Consider layout and
organization: Arrange
content in a logical and
visually pleasing way. Use
tables or columns if
appropriate to present
information neatly.

8. Add relevant and engaging


content: Make sure the file
contains valuable and
interesting information. Use
concise sentences and
engaging language to keep
the reader's attention.

9. Ensure consistency and


coherence: Maintain a
consistent tone and writing
style throughout the file. This
helps create a cohesive and
polished look.

10. Use proper citations and


references: If you are
including external sources or
quotes, ensure they are
properly cited and referenced
according to the appropriate
style guide.

By implementing these
suggestions, you can make
your file more visually
appealing and engaging for
the reader.
12345
Step 1: Square the value of the key.
That is, find kA2. Step 2: Extract the
middle r digits of the result
obtained in Step 1.
Calculate the hash value for keys
1234 and 5642 using the mid-square
method. The hash table has 100
memory locations.
Folding Method
Given a hash table of 100 locations,
calculate the hash value using folding
method for keys 5678, 321, and 34567.
COLLISIONS
Consider a hash table of size
10. Using linear probing,
insert the keys 72, 27, 36,
24, 63, 81, 92, and 101
into
the table.
Step 2 Key = 27
h(27, 0) = (27 mod 10 + 0) mod 10
Step 4 Key = 24
h(24, 0) = (24 mod 10 + 0) mod 10
h(l0l, 0) = {101 mod 10 + 0) mod 10

You might also like