DSA Notes Unit 1 To Unit 6
DSA Notes Unit 1 To Unit 6
By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Basic Terminology
• Classification of Data Structures
• Data Structure Operations
• Review Questions
Basic Terminology
• Data: are values or set of values.
Primitive Non-Primitive
Data Structures Data Structures
• Integer
Linear Non-Linear
• Real Data Structures Data Structures
• Character
• Boolean • Array • Tree
• Stack • Graph
• Queue
• Linked List
Data Structure Operations
Data Structures are processed by using certain operations.
1. Traversing: Accessing each record exactly once so that
certain items in the record may be processed.
•A – Yes
•B - No
Brain Storming Questions
• Linked list is a linear data structure.
•A – True
•B - False
Data Structures
Lecture: Asymptotic Notations & Complexity
Analysis
By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Basic Terminology
• Complexity of Algorithm
• Asymptotic Notations
• Review Questions
Basic Terminology
• Algorithm: is a finite step by step list of well-defined
instructions for solving a particular problem.
Specification of Output
Specification of Input
(e.g. any sequence of natural Algorithm as a function of Input
numbers) (e.g. sequence of sorted
natural numbers)
Characteristics of Good Algorithm
• Efficient
• Running Time
• Space used
• Worst Case: The maximum value of f(n) for any possible input.
Example:
• 50n log n is O(n log n)
• 8n2 log n + 5 n2 + n is O(n2 log n)
Blank
Big-Omega (Ω) Notation
• Asymptotic lower bound
By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Basic Terminology
• Linear Array
• Memory Representation of Linear Array
• Traversing Array
• Insertion and Deletion in Array
• Sorting (Bubble Sort)
• Searching (Linear Search and Binary Search)
• Review Questions
Basic Terminology
• Linear Data Structures: A data structure is said to be linear if its
elements form a sequence or a linear list.
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
Traversing Linear Array
• Suppose we have to count the number of element is an array
or print all the elements of array.
1. Repeat for K = LB to UB
Apply PROCESS to A[K].
[ End of Loop.]
2. Exit.
Question 2: Find out the sum of all the two digit numbers in an
array.
Insertion and Deletion in an Array
• Two types of insertion are possible:
• Insertion at the end of array
• Insertion in the middle of the array
Insertion into a Linear Array
• Algorithm: (Insertion of an element ITEM into Kth position in
a Linear Array A)
11
Merging Algorithm
Algorithm: Merging (A, R,B,S,C)
Here A and B be sorted arrays with R and S elements
respectively. This algorithm merges A and B into an array
C with N=R+ S elements
Step 1: Set NA=0, NB=0 and NC=0
Step 2: Repeat while NA < R and NB < S:
if A[NA] ≤ B[NB], then:
Set C[NC] = A[NA]
Set NA = NA +1
else
Set C[NC] = B[NB]
Set NB = NB +1
[End of if structure]
Set NC= NC +1
[End of Loop]
12
Merging Algorithm
Step 3: If NA >=R, then:
Repeat while NB < S:
Set C[NC] = B[NB]
Set NB = NB+1
Set NC = NC +1
[End of Loop]
else
Repeat while NA < R:
Set C[NC] = A[NA]
Set NC = NC + 1
Set NA = NA +1
[End of loop]
[End of if structure]
Step 4: Return C[NC]
13
Merging Algorithm
• Complexity of merging: The input consists of the total number
n=r+s elements in A and B. Each comparison assigns an
element to the array C, which eventually has n elements.
Accordingly, the number f(n) of comparisons cannot exceed n:
f(n) ≤ n = O(n)
14
Searching
1. Linear Search:
• Compares the item of interest with each element of Array
one by one.
• Traverses the Array sequentially to locate the desired item.
Linear Search Algorithm
• LINEAR (DATA, N, ITEM, LOC)
By
Arvind Kumar
Asst. Professor
(1, 1)
(2, 1) Column 1
(3, 1)
(1, 2)
(2, 2) Column 2
(3, 2)
(1, 3)
(2, 3) Column 3
(3, 3)
(1, 4)
(2, 4) Column 4
(3, 4)
Row-Major Order
(1, 1)
(1, 2)
Row 1
(1, 3)
(1, 4)
(2, 1)
(2, 2)
(2, 3) Row 2
(2, 4)
(3, 1)
(3, 2)
(3, 3) Row 3
(3, 4)
Column-Major Order:
LOC (A[j, k]) = Base (A) + w [M (k-1) + (j-1)]
Row-Major Order:
LOC (A[j, k]) = Base (A) + w [N (j-1) + (k-1)]
Blank
Review Questions
• Given, in a 2-D Array the lower bound and upper bound for first
index is 3 and 11 and that for second index is 5 and 9. Find out
the size of the array.
• In any 2-D Array, which are the elements that will be always
having the same memory address for both column-major order
& Row-major order?
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Min
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
1 5 3 4 6 2
Min
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Min
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Min
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 6 5
Min
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
DONE!
Comparison
Data Movement
Sorted
Blank
Selection Sort
Selection_Sort (A, n)
1. Set j = 0.
2. Repeat While j < n – 1:
3. Set Min = j and i = j+1.
4. Repeat step 5 while i < n:
5. if(a[i] < a[Min]), then:
Min = i.
[End of step 4 loop.]
6. if (Min != j), then:
7. swap (a[j], a[Min]).
8. [End of step 2 loop.]
9. Return.
Comparison Table of Sorting
Best Average Worst
Case Case Case
By
Arvind Kumar
Asst. Professor
2 8 4 9 3 6
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 3 4 6 8 9 done
Insertion Sort
INSERTION_SORT (A, N)
1. Set A[0] = -∞.
2. Repeat Step 3 to 5 for K = 2, 3, …, N:
3. Set TEMP = A[K] and PTR = K – 1.
4. Repeat while TEMP < A[PTR]:
(a) Set A[PTR+1] = A[PTR]
(b) Set PTR = PTR – 1.
[End of Loop.]
5. Set A[PTR+1] = TEMP.
6. Return.
Insertion Sort Complexity
➢ This Sorting algorithm is frequently used when n is very
small.
By
Arvind Kumar
Asst. Professor
• Second part contains the address of the next node in the list.
Linked List
• A singly linked list is a
concrete data structure
consisting of a sequence of next
nodes
• Each node stores
– element
– link to the next node node
element
A B C D
Blank
Key Points
❑ Linked list
• Linear collection of nodes
• Connected by pointer links
• Accessed via a pointer to the first node of the list
• Link pointer in the last node is set to null to mark the
list’s end
• Linked list contains a List Pointer Variable called
START or NAME, which contains the address of the
first node.
Why Linked List?
Arrays: pluses and minuses
+ Fast element access.
-- Impossible to resize.
INFO LINK
Start 2 1
2 A 6
3 E 9
4 C 7
5
6 B 4
7 D 3
8
9 F 0
10
Memory Representation (2)
• Multiple lists in memory
INFO LINK
Start1 2 1 S4 0
2 A 6
3 E 9
Start 2 10 4 C 7
5 S2 8
6 B 4
7 D 3
8 S3 1
9 F 0
10 S1 5
Blank
Memory Representation (3)
• INFO part of a node may be a record with multiple data items.
• Records in memory using Linked lists
By
Arvind Kumar
Asst. Professor
• Second part contains the address of the next node in the list.
Blank
Traversing a Linked List
• PTR is a pointer variable which points to the node currently
being processed.
• LINK [PTR] points to the next node to be processed.
INFO LINK
START 2 1 0
2 A 6
3 E 9
AVAIL 10 4 C 7
5 8
6 B 4
7 D 3
8 1
9 F 0
10 5
Garbage Collection
• Garbage collection is a technique of collecting all the
deleted spaces or unused spaces in memory.
By
Arvind Kumar
Asst. Professor
❑ Types of insertion:
• Insertion at the beginning
• Insertion between two nodes
Checking the Available List
AVAIL
ITEM Ø
Free-Storage List
NEW
Insertion at the beginning of Linked List
START
ITEM
Blank
Insertion Algorithm (Beginning of the list)
LOC
ITEM
Insertion Algorithm (After a given node)
INSLOC (INFO, LINK, START, AVAIL, LOC, ITEM)
1. [OVERFLOW?] If AVAIL = NULL, then: Write: OVERFLOW, and
Exit.
2. [Remove first node from AVAIL list]
Set NEW= AVAIL and AVAIL= LINK [AVAIL].
3. Set INFO [NEW] = ITEM. [Copy the new data to the node].
4. If LOC = NULL, then: [Insert as a first node]
Set LINK [NEW] = START and START = NEW.
Else: [Insert after node with location LOC.]
Set LINK [NEW] = LINK [LOC] and LINK [LOC] = NEW.
[End of If structure.]
5. Exit.
Blank
Insertion into a sorted Linked List
• If ITEM is to be inserted into a sorted linked list. Then ITEM must be
inserted between nodes A and B such that:
INFO [A] < ITEM < INFO [B]
• First of all find the location of Node A
• Then insert the node after Node A.
1. [List Empty?] If START = NULL, then: Set LOC = NULL, and Return.
2. [Special Case?] If ITEM < INFO [START], then: Set LOC = NULL, and
Return.
3. Set SAVE = START and PTR = LINK [START]. [Initializes pointers]
4. Repeat step 5 and 6 while PTR ≠ NULL.
5. If ITEM < INFO [PTR] then:
Set LOC = SAVE, and Return.
[End of If Structure.]
6. Set SAVE = PTR and PTR = LINK [PTR]. [Update pointers]
[End of Step 4 Loop.]
7. Set LOC = SAVE.
8. Return.
Blank
Review Questions
• What is the condition for the list being empty?
By
Arvind Kumar
Asst. Professor
• Deletion Algorithm
– Deleting the Node following a given Node
– Deleting a Node with a given ITEM of Information
• Review Questions
Deletion from a Linked List
❑ A node N is to be deleted from the Linked List.
• Node N is between node A and node B.
❑ Types of Deletion:
• Deleting the node following a given node
• Deleting the Node with a given ITEM of Information.
Deletion from Linked List
START
ITEM Ø
3. EXIT.
Blank
Deleting the Node with a given ITEM of
Information
DELETE (INFO, LINK, START, AVAIL, ITEM)
1. Call FIND_B (INFO, LINK, START, ITEM, LOC, LOCP)
[Find the Location of node N and its preceding node]
2. If LOC = NULL, then: Write: ITEM not in LIST and EXIT.
3. [Delete node].
If LOCP = NULL, then:
Set START = LINK [START]. [Delete First node]
Else:
Set LINK [LOCP] = LINK [LOC].
[End of If Structure.]
By
Arvind Kumar
Asst. Professor
START
Ø
HEADER NODE
Header Linked List
• This header node need not represent the same type of data that
succeeding nodes do.
• Header node can access the data of all the nodes in the linked
list.
Types of Header Linked List
❑ A Grounded header list is a header list where the last node
contains the null pointer.
Note:
• Unless otherwise stated or implied, header list will always be circular
list.
• Accordingly, in such a case, the header node also acts as a sentinel
indicating the end of the list.
START
Ø
HEADER NODE
HEADER NODE
By
Arvind Kumar
Asst. Professor
FIRST
BACK
X X
INFO FORW
Blank
Key Points
• If LOCA and LOCB are the locations of node A and node B
respectively, then
START HEADER
NODE
INFO FORW
Blank
Insertion in a Two-way List
• INST_TWL (INFO, FORW, BACK, FIRST, LAST, AVAIL,
LOCA, LOCB, ITEM)
2. [Remove Node from AVAil List and copy the Item into it.]
Set NEW = AVAIL, AVAIL = FORW [AVAIL],
INFO [NEW] = ITEM.
4. Exit.
Blank
Deletion in a Two-way List
• DEL_TWL (INFO, FORW, BACK, FIRST, LAST, AVAIL,
LOC)
1. [Delete Node.]
Set FORW [BACK [LOC] ] = FORW [LOC] and
BACK [FORW [LOC] ] = BACK [LOC].
3. Exit.
Blank
Review Questions
• What is the advantage of Two-way List over Linked List?
By
Arvind Kumar
Asst. Professor
TOP = 1
TOP = 0
Array Representation of Stacks
STACK
1 2 3 4 5 6 7 8
4 8
TOP MAXSTK
Blank
Array Representation of Stacks
PUSH (STACK, TOP, MAXSTK, ITEM)
4. Return
Blank
Array Representation of Stacks
POP (STACK, TOP, ITEM)
4. Return
Minimizing Overflow
• Stack involves a Time-space Trade-off.
By
Arvind Kumar
Asst. Professor
Example:
(5 * 3) + 2 & 5 * (3 + 2)
Blank
Polish Notation
❑ Polish Notation: The Operator Symbol is placed before
its two operands.
Example: + A B, * C D, / P Q etc.
2. Scan P from left to right and Repeat Step 3 and 4 for each element of
P until the sentinel “)” is encountered.
By
Arvind Kumar
Asst. Professor
18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43
18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9
18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
Blank
Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers
By
Arvind Kumar
Asst. Professor
By
Arvind Kumar
Asst. Professor
5. Exit
Blank
Deletion in a Queue
LINK_Q_DELETE (INFO, LINK, FRONT, REAR, AVAIL, ITEM)
9. Exit
Blank
Deques
➢ Deque (Double-ended queue) is a linear list in which elements
can be added or removed at either end but not in the middle.
Output-Restricted Deque:
allows deletion only at one end while insertion at both the ends
of the list.
Blank
Priority Queue
➢ A Priority Queue is a collection of elements such that each
element has been assigned a priority and such that the order in
which elements are processed comes from the following rules:
3. Process ITEM.
4. Exit.
Blank
Insertion in a Priority Queue
1. Traverse the One-Way list until finding a node X whose priority
number exceeds N.
3. If no such node is found, insert the ITEM as the last element of the
list.
4. Exit.
Blank
Array Representation
of
Priority Queues
Array Representation of Priority Queues
➢ Use a separate queue for each level of priority (for each priority
number).
➢ Each such queue will appear in its own circular array and have its
own pointers FRONT and REAR.
FRONT REAR 1 2 3 4 5
2 4 [ X Y Z ]
3 3 [ P ]
0 0 [ ]
5 2 [ A B D ]
Blank
Deletion in a Priority Queue
➢ Delete and process the first element in a priority queue
maintained by a two-dimensional array QUEUE.
3. Exit.
Blank
Insertion in a Priority Queue
➢ Insert an ITEM with priority number M to a priority queue
maintained by a two-dimensional array QUEUE.
2. Exit.
Blank
Blank
Data Structures
Lecture: Tree
By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Introduction
• Binary Tree
• Basic Terminology
• Complete Binary Tree
• Extended Binary Tree
• Traversing Binary Tree
•Pre-order
• In-order
• Post-order
Introduction
• Trees are non-linear data structures.
By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Introduction
• Representing Binary Tree in Memory
• Linked Representation
• Sequential Representation
• Traversing Binary Tree
• Preorder
• Inorder
• Postorder
• Traversal algorithms using Stacks
• Review Questions
Introduction
• Trees are non-linear data structures.
By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
A Binary Search Tree is a binary tree with
the following properties:
•All items in the left subtree are less than the root.
•All items in the right subtree are greater or equal to the root.
• Traversal
• Search
• Insertion
• Deletion
Blank
Searching and inserting in Binary Search Tree
FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR )
1.If ROOT = NULL, then Set LOC = NULL and PAR = NULL and return
2.If ITEM = INFO[ROOT], then Set LOC = ROOT and PAR = NULL and return
3.If ITEM < INFO[ROOT], then:
Set PTR = LEFT[ROOT] and SAVE = ROOT
Else
Set PTR = RIGHT[ROOT] and SAVE = ROOT
4.Repeat step 5 and 6 while PTR != NULL
5. If ITEM = INFO[PTR], then Set LOC = PTR and PAR = SAVE and return
6. If ITEM < INFO[PTR], then:
Set SAVE = PTR and PTR = LEFT[PTR]
Else
Set SAVE = PTR and PTR = RIGHT[PTR]
7.Set LOC = NULL and PAR = SAVE
8.Exit
Blank
INSBST (INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM, LOC)
1. Call FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR )
2. If LOC != NULL then Exit
3. [copy item into new node]
(a) If AVAIL = NULL then write OVERFLOW and Exit
(b) Set NEW = AVAIL, AVAIL = LEFT [AVAIL] and INFO[NEW] = ITEM
(c) LEFT[NEW] = NULL and ROGHT[NEW] = NULL
4. [Add item to tree]
If PAR = NULL then:
Set ROOT = NEW
Else if ITEM < INFO[PAR] then
Set LEFT[PAR] = NEW
Else
Set RIGHT[PAR] = NEW
7. Exit
Blank
Deletion in Binary Search Tree
CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR )
1. If LEFT[LOC] = NULL and RIGHT[LOC] = NULL then:
Set CHILD = NULL
Else If LEFT[LOC] != NULL then:
Set CHILD = LEFT[LOC]
Else
Set CHILD = RIGHT[LOC]
4. If PAR != NULL then
5. If LOC =LEFT[PAR] then
Set LEFT[PAR] = CHILD
Else
Set RIGHT[PAR] = CHILD
Else
Set ROOT = CHILD
7. Return
Blank
CASEB(INFO, LEFT, RIGHT, ROOT, LOC, PAR )
1. (a) Set PTR = RIGHT[LOC] and SAVE = LOC
(b) Repeat while LEFT[PTR] != NULL
Set SAVE = PTR and PTR = LEFT[PTR]
(c) Set SUC = PTR and PARSUC = SAVE
2. Call CASEA(INFO, LEFT, RIGHT, ROOT, SUC, PARSUC)
4. (a) If PAR != NULL then
If LOC =LEFT[PAR] then
Set LEFT[PAR] = SUC
Else
Set RIGHT[PAR] = SUC
Else
Set ROOT = SUC
(b) Set LEFT[SUC] = LEFT[LOC] and
RIGHT[SUC] = RIGHT[LOC]
7. Return
Blank
DEL(INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM)
1. Call FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR)
2. If LOC = NULL then Write ITEM not in tree, and Exit
3. If RIGHT[LOC] != NULL and LEFT[LOC] != NULL then
Call CASEB (INFO, LEFT, RIGHT, ROOT, LOC, PAR)
Else
Call CASEA (INFO, LEFT, RIGHT, ROOT, LOC, PAR)
4. Set LEFT[LOC] =AVAIL and AVAIL = LOC
5. Exit.
6.
Blank
Questions
4.https://www.cs.usfca.edu/~galles/visualization/BST.html
Blank
Data Structures
Lecture: AVL Tree
By
Arvind Kumar
Asst. Professor
2
Motivation
• When building a binary search tree, what type of trees
would we like? Example: 3, 5, 8, 20, 18, 13, 22
3
5 13
8
5 20
13
18 3 8 18 22
20
22
3
Blank
4
• Complete binary tree is hard to build when we allow dynamic insert
and remove.
– We want a tree that has the following properties
• Tree height = O(log(N))
• allows dynamic insert and remove with O(log(N)) time
complexity.
– The AVL tree is one of this kind of trees.
8
13
5 18
5 20
3 13 20
3 8 18 22 22
5
Blank
6
AVL (Adelson-Velskii and Landis) Trees
8
• AVL tree is a binary search tree with balance
condition
– To ensure depth of the tree is O(log(N))
– And consequently, search/insert/remove
complexity bound O(log(N))
• Balance condition
– For every node in the tree, height of left and right
subtree can differ by at most 1
9
Blank
10
Which is an AVL Tree?
11
Blank
12
Balance Condition Violation
• If condition violated after a node insertion
– Which nodes do we need to rotate?
– Only nodes on path from insertion point to root may have their
balance altered
• Rebalance the tree through rotation at the deepest node with balance
violated
– The entire tree will be rebalanced
14
AVL Trees Complexity
• Overhead
– Extra space for maintaining height information at each
node
– Insertion and deletion become more complicated, but still
O(log N)
• Advantage
– Worst case O(log(N)) for insert, delete, and search
15
Single Rotation (Case 1)
16
Blank
17
Example
• After inserting 6
– Balance condition at node 8 is violated
18
Blank
19
Example
• Inserting 3, 2, 1, and then 4 to 7 sequentially into empty AVL tree
3
2
2
1 3
1
20
Blank
21
• Inserting 4 2
1 3
4
• Inserting 5
2 2
1 3 4
1
4 5
3
5
22
Blank
23
• Inserting 6
4
2
2 5
1 4
1 3 6
3 5
• Inserting 7
6
4
4
2 6
2 5
1 3 5 7
1 3 6
7
24
Blank
25
Example
• Inserting 16 and 15
4
4
2 6
2 6
1 3 5 15
1 3 5 7
7 16
16
15
26
Blank
27
• Inserting 14
4
4
2 7
2 6
1 3 6 15
1 3 5 15
14 16
7 16 5
14
28
Blank
29
Questions
Blank
• http://www.motleytech.net/balanced-binary-
tree-avl-tree-animation.html
• https://www.cs.usfca.edu/~galles/visualizatio
n/AVLtree.html
31
Data Structures
Lecture : Heap
By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Introduction
• Heap Sort
• Heap Sort Complexity
• Review Questions
Blank
Introduction
• Heap (MaxHeap): A complete binary tree H with
n elements is called a Heap if each node N of H has the
following property:
“ The value at N is greater than or equal to the value
at each of the children of N.”
By
Arvind Kumar
Asst. Professor
Lovely Professional University, Punjab
Contents
• Introduction
• Basic Terminology
• Sequential Representation of Graphs
• Adjacency Matrix
• Path Matrix
• Linked Representation of Graphs
• Warshall’s Algorithm: Shortest Path
• Review Questions
Introduction
• A Graph G is a collection of:
1. A set V of elements called Nodes or Vertices.
2. A set E of Edges such that each edge e in E is identified with
a unique pair [u, v] of nodes in V, denoted by e = [u, v].
G = (V, E)
• Nodes u and v are called end points of edge e and also known
as adjacent nodes or neighbors.
Blank
Basic Terminology
• Degree of a node: Degree of a node, deg(u), is the
number of edges containing u.
Teminology:
• Edge e = (u, v) is called an Arc.
• e begins at u and ends at v.
• u is called origin and v is called destination.
• u is adjacent to v and v is not adjacent to u.
• u is predecessor of v and v is sucessor of u.
Blank
Degree of Graph
• Outdegree: Outdegree of a node u in G is the
number of edges beginning at u.
• http://matrixmultiplication.xyz/
Data Structures
Lecture: Operations on Graph
By
Arvind Kumar
Asst. Professor
Lovely Professional University, Punjab
Contents
• Introduction
• Graph Traversal
• BFS
• DFS
• Searching in a Graph
• Insertion in a Graph
• Deletion in a Graphs
• Review Questions
Blank
Introduction
• There are two different ways of Traversing a Graph.
• Breadth First Search (BFS) : uses Queue
• Depth First Search (DFS) : uses Stack
E e r i space
y s n a l k .
Building a Tree
Scan the original text
Eerie eyes seen near lake.
• What is the frequency of each character in the text?
E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8
Building a Tree
• While priority queue contains two or more nodes
– Create new node
– Dequeue node and make it left subtree
– Dequeue next node and make it right subtree
– Frequency of new node equals sum of frequency of left
and right children
– Enqueue new node back into queue
Building a Tree
E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8
Building a Tree
y l k . r s n a sp e
1 1 1 1 2 2 2 2 4 8
E i
1 1
Building a Tree
y l k . r s n a sp e
2
1 1 1 1 2 2 2 2 4 8
E i
1 1
Building a Tree
k . r s n a sp e
2
1 1 2 2 2 2 4 8
E i
1 1
y l
1 1
Building a Tree
2
k . r s n a 2 sp e
1 1 2 2 2 2 4 8
y l
E i 1 1
1 1
Building a Tree
r s n a 2 2 sp e
2 2 2 2 4 8
y l
E i 1 1
1 1
k .
1 1
Building a Tree
r s n a 2 2 sp e
2
2 2 2 2 4 8
E i y l k .
1 1 1 1 1 1
Building a Tree
n a 2 sp e
2 2
2 2 4 8
E i y l k .
1 1 1 1 1 1
r s
2 2
Building a Tree
n a 2 sp e
2 4
2
2 2 4 8
E i y l k . r s
1 1 1 1 1 1 2 2
Building a Tree
2 4 e
2 2 sp
8
4
y l k . r s
E i 1 1 1 1 2 2
1 1
n a
2 2
Building a Tree
2 4 4 e
2 2 sp
8
4
y l k . r s n a
E i 1 1 1 1 2 2 2 2
1 1
Building a Tree
4 4 e
2 sp
8
4
k . r s n a
1 1 2 2 2 2
2 2
E i y l
1 1 1 1
Building a Tree
4 4 4
2 sp e
4 2 2 8
k . r s n a
1 1 2 2 2 2
E i y l
1 1 1 1
Building a Tree
4 4 4
e
2 2 8
r s n a
2 2 2 2
E i y l
1 1 1 1
2 sp
4
k .
1 1
Building a Tree
4 4 4 6 e
2 sp 8
r s n a 2 2
4
2 2 2 2 k .
E i y l 1 1
1 1 1 1
Building a Tree
4 6 e
2 2 2 8
sp
4
E i y l k .
1 1 1 1 1 1
8
4 4
r s n a
2 2 2 2
Building a Tree
4 6 e 8
2 2 2 8
sp
4 4 4
E i y l k .
1 1 1 1 1 1
r s n a
2 2 2 2
Building a Tree
8
e
8
4 4
10
r s n a
2 2 2 2 4
6
2 2 2 sp
4
E i y l k .
1 1 1 1 1 1
Building a Tree
8 10
e
8 4
4 4
6
2 2
r s n a 2 sp
2 2 2 2 4
E i y l k .
1 1 1 1 1 1
Building a Tree
10
16
4
6
2 2 e 8
2 sp 8
4
E i y l k . 4 4
1 1 1 1 1 1
r s n a
2 2 2 2
Building a Tree
10 16
4
6
e 8
2 2 8
2 sp
4 4 4
E i y l k .
1 1 1 1 1 1
r s n a
2 2 2 2
Building a Tree
26
16
10
4 e 8
6 8
2 2 2 sp 4 4
4
E i y l k .
1 1 1 1 1 1 r s n a
2 2 2 2
Building a Tree
•After
enqueueing
26 this node
there is only
16
10 one node left
4 e 8
in priority
6 8 queue.
2 2 2 sp 4 4
4
E i y l k .
1 1 1 1 1 1 r s n a
2 2 2 2
Building a Tree
Dequeue the single node
left in the queue.
26
16
This tree contains the 10
new code words for each
4 e 8
character. 6 8
2 2 2 sp 4 4
4
Frequency of root node E i y l k .
1 1 1 1 1 1 r s n a
should equal number of 2 2 2 2
characters in text.
Eerie eyes seen near lake. 26 characters
Encoding the File
Traverse Tree for Codes
Char Code
E 0000
i 0001
y 0010 26
l 0011
k 0100 16
. 0101 10
space 011 4
e 10 e 8
6 8
r 1100 2 2 2 4 4
s 1101 sp
4
n 1110 E i y l k .
a 1111 1 1 1 1 1 1 r s n a
2 2 2 2
Encoding the File
• Rescan text and encode file
using new code words Char Code
E 0000
Eerie eyes seen near lake.
i 0001
y 0010
0000101100000110011 l 0011
k 0100
1000101011011010011 . 0101
1110101111110001100 space 011
1111110100100101 e 10
r 1100
• Why is there no need s 1101
for a separator n 1110
character? a 1111
.
Encoding the File
Results
• Have we made things any
better? 0000101100000110011
1000101011011010011
• 73 bits to encode the text
1110101111110001100
• ASCII would take 8 * 26 = 1111110100100101
208 bits
Decoding the File
• 0 go left 10
16
• 1 go right 4 e 8
6 8
2 2 2 sp 4 4
4
101000110111101111 E i y l k .
1 1 1 1 1 1 r s n a
01111110000110101 2 2 2 2
Summary
• Huffman coding is a technique used to compress files
for transmission
• Uses statistical coding
– more frequently used symbols have shorter code words
• Works well for text and fax transmissions
• An application that uses several data structures
Question
• https://people.ok.ubc.ca/ylucet/DS/Huffman.
html
Data Structures
Lecture: Hashing
By
Arvind Kumar
Asst. Professor
H: K → L
Blank
Hash Functions
➢ Hash function H is a mapping between set of Keys K and set of
memory locations L.
H: K → L
H(K) = k (mod m)
or H(K) = k (mod m) + 1
(when we want hash address to range from 1 to m rather than 0 to m-1)
Blank
Midsquare Method
➢ The key is squared and some digits are deleted from both sides
to obtained l digits.
H(k) = l
➢ Then the parts are added together, ignoring the last carry.
H(k) = k1 + k2 + … + kr
Blank
Hash Table
➢ A hash table (also hash map) is a data structure used to
implement an associative array, a structure that can map keys to
values.
➢ Linear Probing
• Resolves collisions by placing the data into the next
open slot in the table.
0 4 8 12 16
34 0 45 6 23 7 28 12 29 11 30 33
Blank
Performance of Linear Probing
0 ...
1
...
2
D-1 ...
Blank
Let’s Do It
• Store the given record in table of size 7 by applying
following hash functions and calculate average no. of
probes for successful and unsuccessful search using
linear probing.
Records A B C D
Keys 1011 2012 3331 4114
• Hash Functions:
– Division method
– Mid square method
– Folding method
Blank
Merged by Skillanta.
https://t.me/skillanta