Data structure and algorithm short note
Data structure and algorithm short note
An Abstract Data Type is a collection of data together with a set of data management operations,
called Access Procedures, defined on these data.
ADT is a type (or class) for objects whose behavior is defined by a set of values and a set of
operations.
A data structure is a specialized format for organizing, processing, retrieving and storing
data.
★ What is an algorithm ?
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
★ An algorithm possesses the following properties:
● It must be correct.
● It must be composed of a series of concrete steps.
● There can be no ambiguity as to which step will be performed next.
● It must be composed of a finite number of steps.
● It must terminate.
● Design
● Validation
● Analysis
● Implementation
● Testing
Big-O notation represents the upper bound of the running time of an algorithm. Therefore, it gives
the worst-case complexity of an algorithm. (c is a constant value)
f (n) = O ( g (n) )
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
Ex- 01
f (n) = 3n +2
0 ≤ f (n) ≤ c g(n)
0 ≤ 3n+2 ≤ 3n
n=1, 5≤3
n=2, 8≤6
n=3, 10 ≤ 9
n=4, 14 ≤ 12
Ex- 02
f (n) = 2n2 + 10
0 ≤ f (n) ≤ c g(n)
0 ≤ 2n2 + 10 ≤ 2n2
n=1, 12 ≤ 2
Ex- 03
f (n) = 2n2 + n
0 ≤ f (n) ≤ c g(n)
0 ≤ 2n2 + n ≤ 2n2
n=1, 3≤2
n=2, 10 ≤ 8
● O ( n log n )
● O ( n ) - linear
● O ( log n ) - logarithmic
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
★ What is the Data abstraction ?
Data abstraction is the process of defining a collection of data and relative operations, by specifying
what the operations do on the data.
An Abstract Data Type is a collection of data together with a set of data management operations,
called Access Procedures, defined on these data.
ADT is a type (or class) for objects whose behavior is defined by a set of values and a set of
operations.
★ What is an Array ?
An array is a collection of items of the same data type stored at contiguous memory locations.
Types of Array
1. Static Array: Memory allocated at compile time .It is defined as fixed size of array. It
cannot edit or update the size of this array by user. Ex -
• Declaring Arrays
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
int myArray[];
declares myArray to be an array of integers
• Assigning Values
• Notes on Arrays
• index starts at 0.
• arrays can’t shrink or grow.
e.g., use Vector instead.(Vector , It will discuss next session more
details)
• each element is initialized.
• array bounds checking (no overflow!)
ArrayIndexOutOfBoundsException
• Arrays have array.length
• Example program
}
}
—-------------------------------------------------------------------------------------
}
}
—-------------------------------------------------------------------------------------
class MultiDimArrayDemo
{
public static void main(String[] args)
{
String[][] names = {{"Mr. ", "Mrs. ", "Ms. "}, {"Smith", "Jones"}};
System.out.println(names[0][0] + names[1][0]); //Mr. Smith
System.out.println(names[0][2] + names[1][1]); //Ms. Jones
}
}
—-------------------------------------------------------------------------------------
2. Dynamic Array: memories are allocated at run time. So not having a fixed size.
Suppose, user wants to declare any random size of an array.
●
A dynamic array size assign at running time
●
It can be resized automatically.
●
Its array expands as it adds more elements. So I do not need to determine
the size ahead of time.
● It is a growable array
● It is a resizable array
Advantage of Dynamic Array
1. Array List
Ex-
import java.util.*;
class TestCollection1{
public static void main(String args[]) {
//Creating arraylist
ArrayList<String> l=new ArrayList<String>();
l.add("Saman"); //Adding object in arraylist
l.add("Kamal");
l.add("Saman");
l.add("Maharoof");
//Traversing list through Iterator
Iterator IT=l.iterator();
while(IT.hasNext()){ //travers end of the list
System.out.println(IT.next());
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
}}}
//hash Next() and next method use to interator
2. Linked List
3. Vector
Ex-
import java.util.*;
public class TestCollection3 {
public static void main(String args[]){
Vector<String> ve=new Vector<String>();
ve.add("Saman"); //Adding object in Vector
ve.add("Kamal");
ve.add("Saman");
ve.add("Maharuf");
//Traversing list through Iterator
Iterator IT=ve.iterator();
while(IT.hasNext()) { //travers end of the Vector
System.out.println(IT.next());
}}}
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
1. Traverse
Algorithm
1 Start
2. Initialize an Array of certain size and datatype.
3. Initialize another variable ‘i’ with 0.
4. Print the i th value in the array and increment i.
5. Repeat Step 4 until the end of the array is reached.
6. End
Ex -
// Printing elements of the array using an enhanced for loop (for-each loop)
System.out.println("\nUsing enhanced for loop:");
for (int num : X) {
System.out.println("Element: " + num);
}
}
}
2. Insertion Operation
Algorithm
1. Start
2. Create an Array of a desired data type and size.
3. Initialize a variable ‘i’ as 0.
4. Enter the element at it n index of the array.
5. Increment i by 1.
6. Repeat Steps 4 & 5 until the end of the array.
7. Stop
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
Ex -
3. Deletion
Algorithm
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set X[J] = X[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Ex-
int myArray[] = { 2, 4, 7 };
int indexToDelete = 1;
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
// Displaying the updated array
for (int i = 0; i < myArray.length-1; i++) {
System.out.println("myArray [" + i + "] = " + myArray[ i ]);
}
}
}
Multidimensional Arrays
Print 2D array
System.out.println();
}
}
}
★ Advantages of ArrayList
★ Disadvantages of ArrayList
● If a data entry is added or removed from an array-based list, the data must be transferred to update
the list.
● In the worst case, for an array-based list with n Data insertions, additions, and deletions are O(n)
Time.
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
● Also, all data in an array-based list must be stored in memory in order. Large lists require significant
contiguous chunks of memory.
★ Limitation of arrays
● Need to know the size at the start (at least some good estimate)
● Can not go beyond that
★Advantages of arrays
Linked List is a linear data structure, in which elements are not stored at a contiguous location,
rather they are linked using pointers. Linked List forms a series of connected nodes, where each
node stores the data and the address of the next node.
Alternative - Linked list used when we do not know the size in advance and size varies a lot
What is Pointer?
Pointer contains memory address of a particular type of data.
1. Make the first node of Linked List linked to the new node
2. Remove the head from the original first node of Linked List
3. Make the new node as the Head of the Linked List.
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
How to Insert a Node after a Given Node in Linked List
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
How to deletion Node at the beginning
A stack is a linear data structure which can be accessed only at one of its ends for storing and
retrieving data.The order may be LIFO (Last In First Out)
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
Graphically illustrate the following stack operations sequentially.
i. initializeStack()
ii. push(15)
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
iii. y = topElement( )
y=15
iv. x= pop()
x=15
Array implementation
Public operations
● void push(x) – Insert x
● void pop() – Remove most recently inserted item
● boolean isEmpty() – Return true if empty, false otherwise
● void display() – Display all items
● Boolean isFull()-Return true is Stack is Full, false otherwise
A Queue is defined as a linear data structure that is open at both ends and the operations are
performed in First In First Out (FIFO) order.
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
● isFull() - returns true if the queue is full of elements and false otherwise.
● displayQueue() - displays all elements from front to rear.
1. initializeQueue()
2. p=isEmpty()
p = true
3. enQueue(5)
4. enQueue(9)
enQueue(7)
5. x=deQueue()
x=5
6. enQueue(2)
enQueue(6)
7. q = isFull()
q = false
8. enQueue(3)
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
Graphically illustrate the implementation of the following queue operations.
enQueue(G)
deQueue(H)
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
circular array implementation
All the positions before the front are unused and can thus be recycled. When either rear or
front reaches the end of the array, we reset it to the beginning. This operation is called a
circular array implementation
Size=7,front=1,rear=8
Size=8,front=1,rear=0
Tree is one of the most important non-linear data structures in computing. It allows us to implement
faster algorithms.
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
A Tree is a set of nodes storing elements in a parent-child relationship with the following properties:
It has a special node called root.
Tree types
A binary tree is a finite set of nodes that is either empty or consists of a root and two
disjoint binary trees called the left subtree and the right subtree.
A full Binary tree is a special type of binary tree in which every parent node/internal
node has either two or no children.
It is also known as a proper binary tree.
A complete binary tree is a binary tree in which all the levels are completely filled
except possibly the lowest one, which is filled from the left.
A perfect binary tree is a special type of binary tree in which all the leaf nodes are at
the same depth, and all non-leaf nodes have two children. In simple terms, this
means that all leaf nodes are at the maximum depth of the tree, and the tree is
completely filled with no gaps.
An AVL tree is defined as a self-balancing BST where the difference between heights
of left and right sub trees for any node cannot be more than one.
The difference between the heights of the left sub tree and the right sub tree for any
node is known as the balance factor of the node.
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
VI. Degree of a tree – the maximum degree of any of nodes within the tree
Root node: A
Leaf nodes: L, M, N, O
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
Binary Search Tree
The left subtree of a node contains only nodes with keys lesser than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Note: if duplicate keys are allowed, then nodes with values that are equal to the key in node
N can be either in N's left sub tree or in its right sub tree (but not both). In these notes, we
will assume that duplicates are not allowed.
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
Differentiate the Binary Tree and Binary Search
★ Sorting Algorithm
What is sorting?
Arranging items in ascending or descending order is called sorting.
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj
This short note making resources are,google search engine,chat gpt and lecture slides
WhatsApp group (click on this/new syllabus notes only) Credit - Kanishka viraj