Sem Preparatin
Sem Preparatin
Stack
FULL()
Used to check whether stack is full or not
Empry()
Used to check whether stack is empty or not
Push()
Used to push x into the stack
Pop()
Used to get the top most element of the stack
Size()
Used to get number of elements present into the stack
Queue
FULL()
Used to check whether queue is full or not
Empry()
Used to check whether queue is empty or not
Insert()
Used to add element into the queue at the rear end
Delete()
Used to delete one element from the front end of the queue
Size()
Used to get number of elements present into the queue
List
Size()
Used to get number of elements present into the list
Insert
Used to insert one element into the list
Remove()
Used to remove given element from the list
Get()
Used to get element at position i
Unit 2 Linear Data structures
1. Stack
❑ Stack allows operations on data only at one end
❑ It allows access to the last inserted data only
❑ The stack is called as LIFO ( Last In First Out ) data
structure
❑ The operations like push, pop are used only to push item
and pop the item
Basic operations
❑ The basic operations are
▪ Push
❖ Push the element at the top of the stack
▪ Pop
❖ Pop an element from the top of the stack
▪ Peek
❖ Get the top element of the stack
▪ isFull
❖ Check if stack if full
▪ isEmpty
❖ Check if the stack is empty
Push
❑ Insert the element into stack at the top of the storage
❑ It increments the top index
❑ The storage is full then error message is shown
Pop
❑ Pop is used to remove the element from the stack
❑ It retrieves the element from the top of the storage and decrements the top index
Pseudo code
Program
2. Queue
❑ Queue is similar to stack
❑ It is also an abstract data structure
❑ The stack follows FIFO ( First In First Out )
❑ The data item inserted first will be accessed first
❑ The data is inserted into the queue through one end and deleted from it using the other end
Operations
❑ The operation in queue includes
▪ Enqueue()
❖ It is the data manipulation operation that is used to insert elements into the
stack
▪ Dequeue()
❖ It is the data manipulation operation that is used to remove elements from
the stack
▪ Peek()
❖ It is used to retrieve the front most element
❖ It is used to check the status of the queue with the help of pointer
▪ isFull()
❖ It is used operation verifies whether the queue is full
▪ isEmpty()
❖ It verifies the queue is empty
❖ It is used to check the status of the stack with the help of top pointer
❑ The operations initialize of queue, usage and permanently delete the data from the memory
❑ The build in operations carry out data manipulation and check the status of the queue
❑ Queue use the pointers like
▪ Front
▪ Rear
❑ The front pointer access the data from the front end ( enqueueing ) and the rear pointer
access data from the rear end ( dequeuing )
Pseudo code
Program
Output
Operations
❑ Operations are used to modify the array
❑ The operations are
▪ Traverse
❖ Print all the array elements
▪ Insertion
❖ Adds / inserts an element
▪ Deletion
❖ Deletes / removes the element
▪ Search
❖ Searches an element using the index or value
▪ Update
❖ Updates element
▪ Display
❖ Displays the content
Insertion
❑ Insertion is used to add element to the array
❑ The new element can be added at the beginning, end, using index of array
Program
#include <stdio.h>
int main(){
int LA[3], i;
LA[i] = i + 2;
return 0;
Deletion
❑ Delete / remove an element by using index of the array
Program
#include <stdio.h>
void main(){
int n = 3;
int i;
LA[i] = LA[i+1];
n = n – 1;
Search
❑ Searches an element in the array using the key
❑ Key element sequentially compares every value in the array to check if the key is present in
the array or not
Program
#include <stdio.h>
void main(){
int item = 5, n = 5;
int i = 0, j = 0;
Traversal
❑ Traverses through all element of array
Program
#include <stdio.h>
int main(){
int i = 0, j = n;
Update
❑ Updates an existing element from the array using index
Program
#include <stdio.h>
void main(){
int i, j;
LA[k-1] = item;
Display
❑ Display all the elements in the entire array using print statement
Program
#include <stdio.h>
int main(){
int n = 5;
int i;
}
Unit 3
Usages
✓ Dynamic memory allocation
✓ Efficient insertion and deletion at beginning
Pseudo code
2. Double linked list
✓ Each node contains data and 2 links Time complexity
✓ The first links points to the previous node and next link points Access 0(n)
to the next node Search 0(n)
✓ Space complexity O (n ) Insertion 0(1)
✓ It is a bi directional liked list Deletion 0(1)
✓ It can traverse in both directions
✓ The nodes contain one extra pointer that points the previous node
✓ It is used for fast insertion and deletion
✓ We create a node with two pointers that points the previous node and next node
Usages
✓ Bi directional traversal
✓ Efficient deletion of node
Pseudo code
3. Palindrome using double linked list
4. Circular linked list
✓ In this the first and the last nodes are also connected to each other to form circle
✓ There is no NULL at the end
✓ It is a unidirectional linked list
✓ We can traverse it in one direction
✓ The last node will be pointing to the head node
✓ It has no beginning or ending
✓ We can add or remove the data from the list at any time
Pseudo code
Unit 4
1. Tree terminology
Tree is non linear data structure
It organizes data in hierarchical structure
It is the collection of data
Every individual element is known as node
It stores the actual data of the particular element and line to next element in the hierarchical
structure
Root
First node of the tree
Every tree has root node
It is the origin of the tree data structure
Edge
The connecting link between 2 nodes is called as edge
The tree with n number of nodes will be maximum of n-1 number of edges
Parent
Predecessor of any node
It has branch from it to other nodes
It has child or children
Child
Descendant of a node
It has link from its parent nodes
Any node can have any number of child nodes
All nodes except root are child nodes
Siblings
Nodes with same parent are called as siblings
The nodes with same parent are called sibling
Leaf
Nodes that do not have child
Leaf nodes have no child
Internal nodes
Node which has at least one child
Nodes other than leaves are called as internal nodes
The root node is also said to be internal node
The internal nodes are also called as non terminal nodes
Degree
The total number of children of node called as degree of the node
The degree of node is total number of children it has
The highest degree of node among all the nodes in tree
Level
The root is said to be at level 0 and the
children of root are at level and children nodes
are at level 1
Each step from top to bottom is called as level
The level counts starts from 0 and incremented
by 1
Depth
It goes from the top to the bottom
The total number of edges from root node to particular node is the depth
The total number of edges from root node to leaf node is the longest path
The highest depth of leaf node is said to be the depth of the tree
Height
The total number of edges from leaf to particular node is the
particular node in the longest path
The largest Height of the node in a tree is called as the height of the
tree
It goes from the bottom to the top
Height of all leaves nodes is 0
Path
The sequence of nodes and edges from one node to another node
Length of the path is total number of nodes in the path
Sub tree
Each child from node forms a subtree
Every child node will form subtree on its parent node
2. Traversal
Inorder - left root (parent) right
Preorder - root(parent) left right
Postorder - left right root(parent)
Time complexity
3. Huffman coding
Access 0(1)
The Huffman coding technique compress the string into a smaller
Search 0(n)
size
Insertion 0(n)
It creates a tree using the frequencies of the character and
Deletion 0(n)
generates the code for each character
Once the data is encoded then decoding is done using same tree
It prevents any ambiguity in the decoding process using prefix code concept
Steps
Calculate the frequency of each character in the string
Sort the characters in increasing order of the frequency
Then store them in priority queue
Make unique character as leaf node
Create empty node and assign the minimum frequency to the left child of z and assign the
second minimum frequency to the right of child Z
Set the value of Z as sum of above 2 minimum frequency
Remove 2 minimum frequencies from queue and add the sum into it
Insert node z into the tree
Repeat the step
Assign non leaf node to 0 and leaf node as 1
Unit 5
1. Priority queue
Declare max priority queue
Set bool process ( priority_queue <int> &q
)
Check whether q is empty
Then extract the guest who have v
interlocutors
The value v is the maximum in queue
Set v as top and pop
Queue<int>add
While v—then check queue is empty if not
then pop them
Add queue to q
Check whether c == 2
Clean the queue for next case
Standard mechanism for ordering tasks
It is based on First Come First served basis
When tasks have importance then it is
priority
The priority queue tasks using partial
ordering based on priority
The highest priority task is first served
Heap uses priority queue for structuring
data
It is used to improve the performance
Operations
There are 2 main operation like
Insert
➢ Insert is also known as enqueuer
➢ The values are dynamically inserted
➢ The specification of priority is 0 –high,1,2..low)
DeleteMin
➢ It is also known as dequeuer
➢ It finds the minimum element or highest priority in queue
➢ After finding it, it gets deleted from queue and returned
Implementations
The implementation are
Unordered linked list
➢ 0(1) insert
➢ 0(n) delete min
Ordered linked list
➢ 0(n) insert
➢ 0(1) delete min
Ordered array
➢ 0(logn+n) insert
➢ 0(n) delete min
Balanced BST
➢ O(log2n) for insert and delete min
2. Binary heap
Heap is a priority queue data structure
It is a complete binary tree
The binary heap is a binary tree with 2 properties
Structure property
Heap order property
Structure property
It is a complete binary tree
Each level is completely filled ( except bottom most level )
The bottom most level may be partially filled ( from left to right )
The height of complete binary tree is N elements log 2 N
Property
Max heap
Key present at the root node must be greatest among all keys present at its children
It should be followed by its sub tree too
Min heap
Key present at the root node must be greatest among all keys
present at its children
It should be followed by its sub tree too
Application
Heap sort algorithm
Graph algorithm – prims, dijkstra
Priority queue implementation
Operations
getMin()
getMax()
extractMin()
insert()
delete()
heapify()
getMIn()
return root element of min heap
complexity O(1)
getMax()
return root element of max heap
complexity O(1)
extractMin()
removes minimum element from heap
complexity O(Logn)
also maintain heap property by calling heapify()
Insert
The insertion of new element into heap at the
next available slot (hole)
The element up the heap while heap order
property no satisfied
Complexity O(Logn)
The new key is added at the end of the tree
If the key is greater than the element then do
nothing
Otherwise call heapify to fix the violation of heap
property
Delete
DeleteMin
The minimum element is always at root
The heap decreases by one size
Then move last element into the hole at root
Percolate down when heap order property not satisfied
Arrangement
Min heap
A[(i-1)/2] - return parent
A[(2*i)+1] - return left child
A[(2*1)+2] - return left child
Other types
binomial heaps
it is a forest of heap
it satisfies structure property and heap order property
it is different from binary heap
the structure is totally different
its heap property is same as binary heap
the binomial tree of height is called as BK
It has 2k nodes
The number of nodes at depth d = ( dk )
Each binomial tree contain minimum element at the root of every subtree
The order of elements across binomial tree is irrelevant
The binomial heap of n nodes is the forest of binomial trees as dictated by the
binary representation of n
Each binomial tree is min heap or max heap
d-heaps
leftist heaps
skew heaps
Fibonacci heaps
4. Minheap
✓ In minheap the root or the parent must be smaller than its descendant
5. Maxheap
✓ IN maxheap the root or the parent must be greater than its descendant
5. Operations in heap
* getMin()
* getMax()
* extractMin()
* insert()
* delete()
* heapify()
Applications of heap
* heapsort sorting algorithm
* graph algorithms : prims, spanning, dijkstra shortest algorithm
* priority queue can implement with heap or variety of other method
6. Leftist
❑ Efficient implementation of meldable priority queues
❑ The total number of element in 2 rpiroity queues that are melded
❑ The priority queue then meld operation takes O(n) time
❑ When meldable is used
▪ Meld, insert and delete operations take 0 (log n) time
▪ Minimum(maximum) element found in 0(1) time
❑ It is the extended binary tree
❑ All empty nodes are replaced by square node
❑ The square nodes are known as external nodes
❑ The original nodes of the binary tree are called internal nodes
Insertion
Create new heap with single node containing the key
Merge the new heap with existing heap using union operation
Deletion
Find binomial tree with minimum key
Remove the minimum node from its binomial tree and update the heap
Merge the remaining tree if they have same degree
Search
Iterate through each binomial tree in heap
Search for the key within each binomial tree
If key found return value else return null
Algorithm
Create empty queue
Insertion
Create new binomial tree containing only the element to be inserted
Merge new tree with exiting queue
❑ Compare degree of roots of tree
❑ If same degree then merge into single binomial tree with higher degree
❑ Id different degree add new tree to queue as separate element
Minimum retrieval
Find binomial tree with smallest root element
Deletion
Remove binomial tree containing minimum element from queue
Eg:
4 14 24 34 44 54
Unit 6
1. AVL tree
✓ Self balancing tree Time complexity
✓ The difference between the height of the left and right subtree Access 0(log n)
is at most 1, 0 -1 Search 0(log n)
✓ Space complexity: O(n) Insertion 0(log n)
Deletion 0(log n)
Usages
✓ Self balancing property
✓ Efficient searching and insertion
Rotation
✓ There are 4 ways to keep the AVL tree balanced
❖ Left rotation
❖ Right rotation
❖ Left right rotation
❖ Right left rotation
Left rotation
✓ When node is added to the right sub tree
✓ If the tree gets unbalanced then single left rotation is used
Right rotation
✓ When node is added to the left sub tree
✓ If the tree gets unbalanced then single right
rotation is used
Left right rotation
✓ It is the combination of left and right rotation
✓ First left rotation is takes then right rotation is
executed
Operations
✓ Insertion
✓ Deletion
✓ search
Insertion
Balance factor
Deletion Search
Usage
✓ Efficient searching
✓ In order traversal
Insertion
Deletion Print
Findlargest(node)
Sum of BST
Properties
❖ Root is black
❖ Every leaf is black in red black tree
❖ The children of red node are black
❖ All the leaves have same black depth
❖ Root to descendant leaf node contains same number of black nodes
❖ There will be no 2 adjacent red nodes
Rules
❖ If the tree is empty the new node is root and colour black
❖ When the tree is not empty then new node as leaf with color red
❖ If parent is black then exit
❖ If parent is red then check colour of parents sibling of new node if red then recolour as black
⬧ If color is black then do rotations and recoloring
⬧ If color is not red then recolour to red and check parent and do the same
1. B tree
It is special type of self balancing tree
Each node contain more than one key and have more than 2 children
It is the generalized form of binary search tree
B tree can store many keys in single node and have multiple child nodes
It decreases the height significantly allowing faster disk access
Properties
All leaves have same depth
All leaves have same level
Defined by minimum degree t
T depends on disk block size
All nodes can contain 2*t-1 keys
Number of children node = number of keys +1
All keys are sorted in increasing order
The child between k1 and k2 range from k1 to k2
Can grow and shrink form the root
Time complexity O(log n)
Insertion of node done at leaf node
Each node x the keys are stored in increasing order
There is Boolean value x.leaf which is true if x is leaft
If n is order of the tree, each internal node can contain at most n-1 keys along with pointer
to each child
Each node except root have at most n children at least n/2 children
Root has atleast 2 children and contain minimum of q key
Operations
Searching
Start from root node compare k with first key of the node
K.leaf = true return NULL
Search the left child of the key
If it is more than one key the current then search the left child
Repeat steps 1 to 4 until leaf is reached
Applications
Database and file systems
Store blocks of data
Multilevel indexing
2. B+tree
It is the variation of B tree
The data pointers are stored only at the leaf node
The leaf differs from the structure of internal nodes
The leaf nodes are linked together to provide ordered access to the search field tot the
records
The internal nodes are used to guide the search
Features
Balanced
Multi level
Ordered
Cache friendly
Disk oriented
B tree B+ tree
Nodes store both keys and values Leaf nodes store value and internal nodes store
indexing
Leaf do not form linked list Leaf forms linked list
Lower order ( fewer keys ) Higher order ( more keys )
Does not allow key duplication Allow key duplication
More disk i/o due to non sequential reads in Better disk access due to sequential reads
internal nodes
Balanced performance for search insert Better performance for range queries
Requires less memory Requires more memory`
3. Trie
✓ Trie is tree like data structure used for efficient retrieval and storage strings
✓ It organizes strings by their character n tree like structure
✓ It is ideal for tasks like autocomplete, spell checking and searching for words with
✓ It is used to store set of strings in it
Usages
✓ Efficient prefix searching
✓ Autocomplete suggestions
Properties
✓ The root is always null
✓ Each node have maximum of 26 children
✓ Each node except root can store one letter of the alphabet
Types
✓ Standard trie
✓ Compressed trie
✓ Suffix trie
Standard trie
✓ Root node must be null
✓ Node can have 26 children
✓ Children are arranged in alphabetical order
✓ Path from root to leaf node / external node gives
string set
✓ When 2 string have common prefix share the same ancestors
Compressed trie
✓ Space optimization
✓ Nodes of degree at least 2
✓ Obtained by compressing chain of redundant nodes
Suffix trie
✓ It is a compressed trie for all suffixes of text
✓ Eg: minimize
➢ Suffixes: e, ze, ize, mize, imize, nimize,
inimize, minimize
Unit 9
1. Hash table
✓ The hash table is also known as hash map
✓ It provides efficient key value pair storage and retrieval
✓ It uses has function to compute index known as hash code
✓ It maps the key to specific location in array
✓ Space complexity 0 ( n+m ) n – size, m – elements inserted
Usages
✓ Fast data retrieval
✓ Key value storage
Open addressing
Open addressing store multiple elements into same slot
Each slot is either filled with single key or NIL
Linear probing
Linear probing collision is resolved by checking next slot
H(k,i)=(h’(k)+i) mode m
i={0,1…}
h’(k) is new hash function
collision occur at h(k,0) then h(k,1) is checked
the value of I is incremented linearly
linear probing is the cluster of adjacent slots is filled
when inserting a new element entire cluster must be traversed
it adds to the time required perform operations on hash table
Quadratic probing
works similar to linear probing but spacing between slots is increased
h(k, i) = (h′(k) + c1i + c2i2) mod m
c1 and c2 are are positive auxillay constants
I = 0 ,1 ……
H(k,i)=(h’k+i*I) mod m
Double hashing
Collision occurs after applying hash function h(k)
Another hash function is calculated for finding next slot
h(k, i) = (h1(k) + ih2(k)) mod m
Closed addressing
Closed hashing includes
Chaining
Chaining
The chaining builds a linked list of items whose key hashes have same value
It adds a linked list to each table position
The elements with same hash value is stored in a list
Hash functions
hash function cannot prevent the collisions completely
it can reduce the number of collision
functions
division method
multiplication method
universal hashing
Division method
if k is key and m is the size of the hash table
it is calculated as h (k ) = h mod m
Multiplication method
h ( k ) = m(ka mod 1)
Ka mod 1 gives fractional part kA
⌊ ⌋ gives floor value
A is constant
The value of A lies between – and 1
Universal hashing
It is chosen at random independent of keys
Applications
Constant time lookup and insertion is required
Cryptographic applications
Indexing data is required
Algorithm
Select a hash function
Declare array size
Initialize the hash function
Get the key
Based on the key put the value at the array
if the index contains value the use linear probing or quadratic probing to insert the element
Separate chain hashing
Insertion
Compute hash index I
Check whether the linked list at i is
empty
If empty then create new node
containing the key value pair and add it
to the list
If not empty then traverse the list and
create new node containing the key
value pair and add it to the list
Search(key)
Compute hash index i
Traverse the linked list at i
Search the key
If found the return the value
Otherwise return key not present
Delete(key)
Compute hash index I
Traverse the linked list at i
Find the key and remove it
Otherwise return not found
Impact
Insert first()
Efficient for small chains
Lead to performance degradation for large chains due to longer search
Insert last()
Less prone to performance degradation
Insertion time increases as the lengths of the chain increases