0% found this document useful (0 votes)
20 views18 pages

DSA All Exams Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views18 pages

DSA All Exams Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

TRAVERSAL Step 5: SET PTR = START

Step 1: [INITIALIZE] SET PTR = START Step 6: SET PREPTR = PTR


Step 2: Repeat Steps 3 and 4 while PTR! = NULL Step 7: Repeat Steps 8 and 9 while PREPTR -> DATA! =NUM
Step 3: Apply Process to PTR -> DATA Step 8: SET PREPTR = PTR
Step 4: SET PTR = PTR -> NEXT [END OF LOOP] Step 9: SET PTR = PTR -> NEXT [END OF LOOP]
Step 5: EXIT Step 10: PREPTR -> NEXT = NEW_NODE
INSERT A NEW NODE AT BEGIN Step 11: SET NEW_NODE -> NEXT = PTR
Step 1: IF AVAIL = NULL Write OVERFLOW Go to Step 7 Step 12: EXIT
[END OF IF] DELETE THE FIRST NODE
Step 2: SET NEW_NODE = AVAIL Step 1: IF START = NULL Write UNDERFLOW Go to Step 5
Step 3: SET AVAIL = AVAIL -> NEXT [END OF IF]
Step 4: SET NEW_NODE -> DATA = VAL Step 2: SET PTR = START
Step 5: SET NEW_NODE -> NEXT = START Step 3: SET START = START -> NEXT
Step 6: SET START = NEW_NODE Step 4: FREE PTR
Step 7: EXIT Step 5: EXIT
INSERT A NEW NODE AT THE END DELETE THE LAST NODE
Step 1: IF AVAIL = NULL Write OVERFLOW Go to Step 10 Step 1: IF START = NULL Write UNDERFLOW Go to Step 8
[END OF IF] [END OF IF]
Step 2: SET NEW_NODE= AVAIL Step 2: SET PTR = START
Step 3: SET AVAIL = AVAIL -> NEXT Step 3: Repeat Step 4 and 5 While PTR -> NEXT! = NULL
Step 4: SET NEW_NODE -> DATA = VAL Step 4: SET PREPTR = PTR
Step 5: SET NEW_NODE -> NEXT = NULL Step 5: SET PTR = PTR -> NEXT [END OF LOOP]
Step 6: SET PTR = START Step 6: SET PREPTR -> NEXT = NULL
Step 7: Repeat Step 8 while PTR -> NEXT! =NULL Step 7: FREE PTR
Step 8: SET PTR = PTR -> NEXT [END OF LOOP] Step 8: EXIT
Step 9: SET PTR -> NEXT = NEW_NODW DELETE AFTER A GIVEN NODE
Step 10: EXIT Step 1: IF START = NULL Write UNDERFLOW Go to Step 10
INSERT A NEW NODE AFTER A GIVEN NODE [END OF IF]
Step 1: IF AVAIL = NULL Write OVERFLOW Go to Step 12 Step 2: SET PTR = START
[END OF IF] Step 3: SET PREPTR = PTR
Step 2: SET = AVAIL Step 4: Repeat Steps 5 and 6 while PREPTR -> DATA! =NUM
Step 3: SET AVAIL = AVAIL NEXT Step 5: SET PREPTR = PTR
Step 4: SET DATA = VAL Step 6: SET PTR = PTR -> NEXT [END OF LOOP]
Step 5: SET PTR = START Step 7: SET TEMP = PTR
Step 6: SET PREPTR = PTR Step 8: SET PREPTR -> NEXT = PTR -> NEXT
Step 7: Repeat Steps 8 and 9 while != NUM Step 9: FREE TEMP
Step 8: SET PREPTR = PTR Step 10: EXIT
Step 9: SET PTR = PTR NEXT [END OF LOOP] SINGLY LINKED LIST
Step 10: PREPTR NEXT = NEW_NODE
Step 11: SET NEW_NODE NEXT = PTR
Step 12: EXIT
INSERT BEFORE A GIVEN NODE
Step 1: IF AVAIL = NULL Write OVERFLOW Go to Step 12
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT A singly linked list is the simplest type of linked list in which
Step 4: SET NEW_NODE -> DATA = VAL every node contains some data and a pointer to the next
Step 5: SET PTR = START node of the same data type. By saying that the node
Step 6: SET PREPTR = PTR contains a pointer to the next node, we mean that the node
Step 7: Repeat Steps 8 and 9 while PTR -> DATA! =NUM stores the address of the next node in sequence. A singly
Step 8: SET PREPTR = PTR linked list allows traversal of data only in one way. Below
Step 9: SET PTR = PTR -> NEXT [END OF LOOP] Figure shows a singly linked list.
Step 10: PREPTR -> NEXT = NEW_NODE
Step 11: SET NEW_NODE -> NEXT = PTR
Step 12: EXIT
INSERT A GIVEN NODE AFTER A GIVEN NODE
Step 1: IF AVAIL = NULL Write OVERFLOW Go to Step 12
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
INSERT A NEW NODE AT BEGIN REVERSING A LINKED LIST
Step 1: IF AVAIL = NULL Write OVERFLOW Go to Step 11 Step 1: INIT TEMP = NULL, PREV= NULL
[END OF IF] Step 2: IF START = NULL WRITE UNDERFLOW GO TO STEP
Step 2: SET NEW_NODE= AVAIL 10 END OF IF
Step 3: SET AVAIL = AVAIL -> NEXT Step 3: SET PTR = START
Step 4: SET NEW_NODE -> DATA = VAL Step 4: Repeat 5 to 8 While PTR ! = NULL
Step 5: SET PTR = START Step 5: TEMP = PTR -> NEXT
Step 6: Repeat Step 7 while PTR -> NEXT! =START Step 6: PTR -> NEXT = PREV
Step 7: PTR = PTR -> NEXT [END OF LOOP] Step 7: PREV = PTR
Step 8: SET NEW_NODE -> NEXT = START Step 8: PTR = TEMP END OF LOOP
Step 9: SET PTR -> NEXT= NEW_NODE Step 9: SET START = PREV
Step 10: SET START = NEW_NODE Step 10: EXIT
Step 11: EXIT In the reversing of singly linked list the first node become
INSERT A NEW NODE AT THE END the last node and last node become the first node.
Step 1: IF AVAIL = NULL Write OVERFLOW Go to Step 10
[END OF IF]
Step 2: SET NEW_NODE= AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = START
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR -> NEXT! = START Step 8:
SET PTR = PTR -> NEXT [END OF LOOP]
Step 9: SET PTR -> NEXT = NEW_NODE UPDATEING A LINKED LIST
Step 10: EXIT Step 1: IF START = NULL WRITE UNDERFLOW GO TO STEP 6
DELETE A NODE FROM BEGINNING END OF IF
Step 1: IF START = NULL Write UNDERFLOW Go to Step 8 Step 2: SET PTR = START
[END OF IF] Step 3: Repeat 4 While PTR -> DATA! = OLD_VALUE Step 4:
Step 2: SET PTR = START PTR = PTR -> NEXT End of Loop
Step 3: Repeat Step 4 while PTR NEXT! =START Step 5: PTR -> DATA = NEW_VALUE
Step 4: SET PTR = PTR -> NEXT [END OF LOOP] Step 6: EXIT
Step 5: SET PTR -> NEXT = START -> NEXT
Step 6: FREE START Updating a data element of any node at singly linked list.
Step 7: SET START = PTR -> NEXT User need to enter existing value and new value to update.
Step 8: EXIT LINKED LIST
DELETE A NODE FROM END A linked list, in simple terms, is a linear collection of data
Step 1: IF START = NULL Write UNDERFLOW Go to Step 8 elements. These data elements are called nodes. Linked list
[END OF IF] is a data structure which in turn can be used to implement
Step 2: SET PTR = START other data structures. Thus, it acts as a building block to
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT!=START implement data structures such as stacks, queues, and their
Step 4: SET PREPTR = PTR variations. A linked list can be perceived as a train or a
Step 5: SET PTR = PTR -> NEXT [END OF LOOP] sequence of nodes in which each node contains one or
Step 6: SET PREPTR -> NEXT = START more data fields and a pointer to the next node.
Step 7: FREE PTR
Step 8: EXIT
CIRCULAR LINKED LIST
Circular linked list is a linked list where all nodes are
connected to form a circle. There is no NULL at the end. A
circular linked list can be a singly circular linked list or
doubly circular linked list. Any node can be a starting point.
We can traverse the whole list by starting from any point.
We just need to stop when the first visited node is visited
again.
DOUBLY LINKED LIST Step 4: SET START -> PREV = NULL
Step 5: FREE PTR
INSERT A NEW NODE AT A BEGIN
Step 6: EXIT
Step 1: IF AVAIL = NULL Write OVERFLOW Go to Step 9
DELETE A NODE FROM THE END
[END OF IF]
Step 1: IF START = NULL Write UNDERFLOW Go to Step 7
Step 2: SET NEW_NODE = AVAIL
[END OF IF]
Step 3: SET AVAIL = AVAIL -> NEXT
Step 2: SET PTR = START
Step 4: SET NEW_NODE -> DATA = VAL
Step 3: Repeat Step 4 and 5 while PTR -> NEXT !=NULL Step
Step 5: SET NEW_NODE -> PREV = NULL
4: PREPTR = PTR
Step 6: SET NEW_NODE -> NEXT = START
Step 5: SET PTR = PTR -> NEXT [END OF LOOP]
Step 7: SET START -> PREV = NEW_NODE
Step 6: SET PREPTR -> NEXT = NULL
Step 8: SET START = NEW_NODE
Step 7: Free PTR
Step 9: EXIT
Step 8: Exit
INSERT A NEW NODE AT THE END
DELETE A NODE BEFORE A GIVEN NODE
Step 1: IF AVAIL = NULL Write OVERFLOW Go to Step 11
Step 1: IF START = NULL Write UNDERFLOW Go to Step 9
[END OF IF]
[END OF IF]
Step 2: SET NEW_NODE= AVAIL
Step 2: SET PTR = START
Step 3: SET AVAIL = AVAIL -> NEXT
Step 3: Repeat Step 4 and 5 while PTR -> DATA != VAL Step
Step 4: SET NEW_NODE -> DATA = VAL
4: SET PREPTR = PTR
Step 5: SET NEW_NODE -> NEXT= NULL
Step 5: SET PTR = PTR -> NEXT [END OF LOOP]
Step 6: SET PTR = START
Step 6: SET PTR -> PREV = PREPTR -> PREV
Step 7: Repeat Step 8 while PTR -> NEXT!=NULL
Step 7: SET PREPTR -> PREV -> NEXT = PTR
Step 8: SET PTR = PTR -> NEXT [END OF LOOP]
Step 8: Free PREPTR
Step 9: SET PTR -> NEXT = NEW_NODE
Step 9: EXIT
Step 10: SET NEW_NODE -> PREV = PTR
DELETE A NODE AFTER A GIVEN NODE
Step 11: EXIT
Step 1: IF START = NULL Write UNDERFLOW Go to Step 9
INSERT A NEW NODE BEFORE A GIVEN NODE
[END OF IF]
Step 1: IF AVAIL = NULL Write OVERFLOW Go to Step 13
Step 2: SET PTR = START
[END OF IF]
Step 3: Repeat Step 4 and 5 while PREPTR -> DATA != VAL
Step 2: SET NEW_NODE= AVAIL
Step 4: SET PREPTR = PTR
Step 3: SET AVAIL = AVAIL -> NEXT
Step 5: SET PTR = PTR -> NEXT [END OF LOOP]
Step 4: SET NEW_NODE -> DATA = VAL
Step 6: SET PREPTR -> NEXT = PTR -> NEXT
Step 5: SET PTR = START
Step 7: SET PTR -> NEXT -> PREV = PREPTR
Step 6: Repeat Step 7 and 8 while PTR -> DATA != NUM
Step 8: Free PTR
Step 7: SET PREPTR = PTR
Step 9: EXIT
Step 8: SET PTR = PTR -> NEXT [END OF LOOP]
Step 9: SET NEW_NODE -> NEXT = PREPTR - > NEXT Step 10: A doubly linked list or a two-way linked list is a more
SET NEW_NODE -> PREV = PTR -> PREV complex type of linked list which contains a pointer to
Step 11: SET PREPTR -> NEXT = NEW_NODE the next as well as the previous node in the sequence.
Step 12: SET PTR -> PREV = NEW_NODE Therefore, it consists of three parts—data, a pointer to
Step 13: EXIT the next node, and a pointer to the previous node.
INSERT A NEW NODE AFTER A GIVEN NODE
Step 1: IF AVAIL = NULL Write OVERFLOW Go to Step 13
[END OF IF]
Step 2: SET NEW_NODE= AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 7 and 8 while PREPTR -> DATA != NUM
Step 7: SET PREPTR = PTR
Step 8: SET PTR = PTR -> NEXT [END OF LOOP]
Step 9: SET NEW_NODE -> NEXT = PREPTR - > NEXT Step 10:
SET NEW_NODE -> PREV = PTR -> PREV
Step 11: SET PREPTR -> NEXT = NEW_NODE
Step 12: SET PTR -> PREV = NEW_NODE
Step 13: EXIT
DELETE A NODE FROM A BEGIN
Step 1: IF START = NULL Write UNDERFLOW Go to Step 6
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START -> NEXT
POP STACK
Step 1: IF TOP = MAX-1 PRINT “OVERFLOW” Go to Step 4 Stack is an important data structure which stores its
[END OF IF] elements in an ordered manner. A stack is a linear
Step 2: SET TOP = TOP+1 data structure which uses the same principle, i.e., the
Step 3: SET STACK[TOP] = VALUE
elements in a stack are added and removed only from
Step 4: END
one end, which is called the TOP. Hence, a stack is
PUSH
Step 1: IF TOP = NULL PRINT UNDERFLOW Go to Step 4 called a LIFO (LastIn-First-Out) data structure, as the
[END OF IF] element that was inserted last is the first one to be
Step 2: SET VAL = STACK[TOP] taken out.
Step 3: SET TOP = TOP-1
Step 4: END
PEEK
Step 1: IF TOP = NULL PRINT STACK IS EMPTY Go to Step 3
Step 2: RETURN STACK[TOP]
Step 3: END

Array Representation of Stack: In the computer’s


memory, stacks can be represented as a linear array.
Every stack has a variable called TOP associated with
it, which is used to store the address of the topmost
element of the stack. It is this position where the
element will be added to or deleted from. There is
another variable called MAX, which is used to store
the maximum number of elements that the stack can
hold. If TOP = NULL, then it indicates that the stack is
empty and if TOP = MAX–1, then the stack is full.
A Queue is a FIFO (First-In, First-Out) data structure in There are two types of deque that are discussed as Input
which the element that is inserted first is the first one to be restricted deque - As the name implies, in input restricted
taken out. The elements in a queue are added at one end queue, insertion operation can be performed at only one
called the REAR and removed from the other end called the end, while deletion can be performed from both ends.
FRONT.
Array Representation of Queue: Queues can be easily
represented using linear arrays. As stated earlier, every
queue has front and rear variables that point to the
position from where deletions and insertions can be done,
respectively.
Output restricted deque - As the name implies, in output
restricted queue, deletion operation can be performed at
only one end, while insertion can be performed from both
ends.

INSERT NEW ELEMENT


Step 1: IF REAR = MAX-1 Write OVERFLOW Go to step 4
[END OF IF]
Step 2: IF FRONT=-1 and REAR=-1 SET FRONT = REAR = 0
ELSE SET REAR = REAR+1 [END OF IF] Priority Queue: It is a special type of queue in which the
Step 3: SET QUEUE[REAR] = NUM elements are arranged based on the priority. It is a special
Step 4: EXIT type of queue data structure in which every element has a
DELETE A NEW ELEMENT priority associated with it. Suppose some elements occur
Step 1: IF FRONT = -1 OR FRONT > REAR Write UNDERFLOW with the same priority, they will be arranged according to
ELSE SET VAL = QUEUE[FRONT] SET FRONT = FRONT+1 [END the FIFO principle.The representation of priority queue is
OF IF] shown belowimage
Step 2: EXIT
Circular Queue: In Circular Queue, all the nodes are
represented as circular. It is similar to the linear Queue
except that the last element of the queue is connected to
the first element. It is also known as Ring Buffer, as all the
ends are connected to another end. The drawback that
occurs in a linear queue is overcome by using the circular
queue. If the empty space is available in a circular queue,
the new element can be added in an empty space by simply here are two types of priority queue that are discussed as
incrementing the value of rear. follows –
Ascending priority queue - In ascending priority queue,
elements can be inserted in arbitrary order, but only
smallest can be deleted first Descending priority queue - In
descending priority queue, elements can be inserted in
arbitrary order, but only the largest element can be deleted
first.
Multiple Queue: When we implement a queue using an
array, the size of the array must be known in advance. If the
queue is allocated less space, then frequent overflow
Dequeue: In Deque or Double Ended Queue, insertion and conditions will be encountered. To deal with this problem,
deletion can be done from both ends of the queue either the code will have to be modified to reallocate more space
from the front or rear. It means that we can insert and for the array. In case we allocate a large amount of space
delete elements from both front and rear ends of the for the queue, it will result in sheer wastage of the memory.
queue. Deque can be used as a palindrome checker means Thus, there lies a tradeoff between the frequency of
that if we read the string from both ends, then the string overflows and the space allocated. So, a better solution to
would be the same. deal with this problem is to have multiple queues or to
have more than one queue in the same array of sufficient
size.
SEARCHING Binary SEARCH
Searching means to find whether a particular value is • Binary search, works only on sorted list.
present in an array or not. If the value is present in the • Binary search finds the middle value by averaging the first
array, then searching is said to be successful and the and last value. It is denoted as beg, end, and middle value.
searching process gives the location of that value in the • If the middle value is less then search value than will
array. if the value is not present in the array, then searching search from right and if middle value is greater than search
is said to be unsuccessful. There are two popular methods value then will search from left by value.
for searching the array elements: linear search and binary • Mid = beg + end / 2, if mid < search value than beg =
search. The algorithm that should be used depends entirely mid+1, end. if mid > search than beg, end=mid - 1, end.
on how the values are organized in the array. Example of Binary Search
LINEAR SEARCH Example 1: int a [] = {10, 14, 19, 26, 27, 31, 33, 35, 42, 44}
Linear search, also called as sequential search, is a very search value: 14
simple method used for searching an array for a particular beg: 10, end: 170, mid= 10+44/2 =54/2, mid= 27
value. Now, mid > search value
It works by comparing the value to be searched with every beg =10, end = 27-1 = 26, mid = 26+10 /2 = 36/ 2, mid =18 ≈
element of the array one by one in a sequence until a 19
match is found. Now, mid > search
Linear search is mostly used to search an unordered list of Beg =10, end = 19 -1= 18, mid = 10+18/ 2 = 14, mid =14
elements (array in which data elements are not sorted). Value found at position 1
Example of Linear Search Algorithm for Binary Search
int a [] = {10, 8, 2, 7, 3, 4, 9, 1, 6, 5} BINARY_SEARCH (A, lower-bound, upper-bound, VAL)
Search value: 7 Found at Position 3 Step 1: [INITIALIZE] SET BEG = lower-bound, END = upper-
Search value 5 Found at position 9 bound, POS=-1
Search value 22 No element found Step 2: Repeat Steps 3 and 4 while BEG <= END
Algorithm for Linear Search Step 3: SET MID = (BEG + END)/2
LINEAR_SEARCH (A, N, VAL) Step 4: IF A[MID] = VAL
Step 1: [INITIALIZE] SET POS=-1 SET POS = MID
Step 2: [INITIALIZE] SETI=0 PRINT POS
Step 3: Repeat Go to Step 6
Step 4 while I<=N ELSE IF A[MID] > VAL
Step 4: IF A[I] = VAL SET END = MID-1
SET POS= I PRINT POS ELSE
Go to Step 6 [END OF IF] SET BEG = MID+1
[END OF LOOP] [END OF IF]
Step 6: EXIT [END OF LOOP]
Complexity of Linear search is O(n) Step 5: IF POS=-1
PRINT “VALUE IS NOT PRESENT IN THE ARRAY”
[END OF IF]
Step 6: EXIT
Complexity of Binary search is O (Log n)
HASHING range should roughly be the same. The property of
Concept: The complexity of linear search is O(n) based on uniformity also minimizes the number of collisions.
number of array and complexity of binary search is O (log TYPES OF HASH FUNCTIONS
n). If we want to search value in constant time than Division Method It is the simplest method of hashing an
complexity will be O (1). integer k. This method divides k by M where, m is size of a
There are two solution for it. The first solution if storing table, and then uses the remainder obtained. In this case,
the database as per the index ID. For example, employee ID the hash function can be given as h(k) = k mod M
is ranging from 1 to 100 then we can store each employee Example: Calculate the hash values of key 1234 and 5462
id as per the index id. But, if the employee id is 4000 then where M=100.
we have to create index till 4000 which will cause the h (k) = k mod M
memory wastage. Now, second solution is Hashing. h (1234) = 1234 % 100 = 34
• Hashing is the process of mapping large amount of data h (5462) = 5462 % 100 = 62
item to smaller table with the help of hashing function. The main advantage of division method is it is extremely
• Hashing is also known as Hashing Algorithm or Message simple. A potential drawback of the division method is that
Digest Function. while using this method, consecutive keys map to
• It is a technique to convert a range of key values into a consecutive hash values.
range of indexes of an array. Truncation Method
• Hashing allows to update and retrieve any data entry in a The steps involved in the multiplication method are as
constant time O (1). follows
• Constant time O (1) means the operation does not Step 1: Choose a constant A such that 0 < A < 1.
depend on the size of the data. Step 2: Multiply the key k by A
• Hashing is used with a database to enable items to be Step 3: Extract the fractional part of kA.
retrieved more quickly. Step 4: Multiply the result of Step 3 by the size of hash
• It is used in the encryption and decryption of digital table (m). Hence, the hash function can be given as: h(k) =
signatures. [m (kA mod 1)] where (kA mod 1) gives the fractional part
HASH TABLE of kA and m is the total number of indices in the hash table.
• Hash table is a data structure in which keys are mapped Example: Calculate the hash value of 1234 where M=1000
to array positions by a hash function. and A= 0.618033
• A value stored in a hash table can be searched in O (1) h (k) = [m (kA mod 1)] h(1234)
time by using a hash function which generates an address = [1000 (1234 * 0.618033 % 1)
from the key by producing the index of the array where the = [1000 (7629.617385 % 1)
value is stored. = [1000 (0.617385) = 617.386 ≈ 617
• In a hash table, an element with key k is stored at index The greatest advantage of this method is that it works
h(k) which is hash function and not k. practically with any value of A.
• It means a hash function h is used to calculate the index Mid-Square Method The mid-square method is a good
at which the element with key k will be stored. hash function which works in two steps:
• This process of mapping the keys to appropriate locations Step 1: Square the value of the key. That is, find k2 .
(or indices) in a hash table is called hashing. Step 2: Extract the middle r digits of the result obtained in
HASH FUNCTION Step 1.
• A hash function is a mathematical formula which, when Hence, the hash function is h (k) = s where s is obtained by
applied to a key, produces an integer which can be used as selecting r digit from k2 . Example: calculate the hash value
an index for the key in the hash table. of key 1234 and 5642 where M=100
• The main aim of a hash function is that elements should h (k) = s
be relatively, randomly, and uniformly distributed. k= 1234, k2 = 1522756, h (1234) = 27
• It produces a unique set of integers within some suitable k= 5642, k2 = 31832164, h (5642) = 21
range in order to reduce the number of collisions. Note: the range of hash table from 0 to 99 hence, we will
• In practice, there is no hash function that eliminates select 2 digits r value.
collisions completely. A good hash function can only Folding Method The folding method works in the following
minimize the number of collisions by spreading the two steps:
elements uniformly throughout the array. Step 1: Divide the key value into a number of parts. That is,
• Properties of Good Hash Function: divide k into parts k1 , k2 , ..., kn , where each part has the
1. Low cost: The cost of executing a hash function must be same number of digits except the last part which may have
small, so that using the hashing technique becomes lesser digits than the other parts.
preferable over other approaches. Step 2: Add the individual parts. That is, obtain the sum of
2. Determinism: A hash procedure must be deterministic. k1 + k2 + ... + kn . The hash value is produced by ignoring
This means that the same hash value must be generated for the last carry, if any
a given input value. Note: the hash value will depend on the size of table. Now,
3. Uniformity A good hash function must map the keys as the size of table is from 0 to 100 so we can carry value from
evenly as possible over its output range. This means that 0 to 100. Therefore, we ignore the carry of 5678 element
the probability of generating every hash value in the output and the hash value is 34.
Bubble Sort d. The first element of the unsorted partition has array
Bubble sort is a very simple method that sorts the array index 1 (if LB = 0).
elements by repeatedly moving the largest element to the e. During each iteration of the algorithm, the first element
highest index position of the array segment. in the unsorted set is picked up and inserted into the
In bubble sorting, consecutive adjacent pairs of elements in correct position in the sorted set.
the array are compared with each other. If the element at Algorithm for Insertion Sort
the lower index is greater than the element at the higher INSERTION-SORT (ARR, N)
index, the two elements are interchanged so that the Step 1: Repeat Steps 2 to 5 for K=1 to N–1
element is placed before the bigger one. Step 2: SET TEMP = ARR[K]
This process will continue till the list of unsorted elements Step 3: SETJ=K-1
exhausts. This procedure of sorting is called bubble sorting Step 4: Repeat while TEMP <= ARR[J]
because elements ‘bubble’ to the top of the list. SET ARR[J + 1] = ARR[J]
Technique of Bubble Sort: SET J=J-1
The basic methodology of the working of bubble sort is [END OF INNER LOOP]
given as follows: Step 5: SET ARR[J + 1] = TEMP
a. In Pass 1, A[0] and A[1] are compared, then A[1] is [END OF LOOP]
compared with A[2], A[2] is compared with A[3], and so on. Step 6: EXIT
Finally, A[N–2] is compared with A[N–1]. Pass 1 involves n– Selection Sort
1 comparisons and places the biggest element at the Selection sort is a sorting algorithm that has a quadratic
highest index of the array. running time complexity of O(n2 ), thereby making it
b. In Pass 2, A[0] and A[1] are compared, then A[1] is inefficient to be used on large lists. Although selection sort
compared with A[2], A[2] is compared with A[3], and so on. performs worse than insertion sort algorithm, it is noted for
Finally, A[N–3] is compared with A[N–2]. Pass 2 involves n– its simplicity and also has performance advantages over
2 comparisons and places the second biggest element at more complicated algorithms in certain situations.
the second highest index of the array. Selection sort is generally used for sorting files with very
c. In Pass 3, A[0] and A[1] are compared, then A[1] is large objects (records) and small keys.
compared with A[2], A[2] is compared with A[3], and so on. Technique for Selection Sort
Finally, A[N–4] is compared with A[N–3]. Pass 3 involves n– a. In Pass 1, find the position POS of the smallest value in
3 comparisons and places the third biggest element at the the array and then swap ARR [POS] and ARR [0]. Thus, ARR
third highest index of the array. [0] is sorted.
d. In Pass n–1, A[0] and A[1] are compared so that A[0] b. In Pass 2, find the position POS of the smallest value in
Algorithm for Bubble Sort sub-array of N–1 element. Swap ARR [POS] with ARR [1].
Step 1: Repeat Step 2 For1= to N-1 Now, ARR [0] and ARR [1] is sorted.
Step 2: Repeat For J= to N-I c. In Pass N–1, find the position POS of the smaller of the
Step 3: IF A[J] > A[J+1] elements ARR [N–2] and ARR [N–1]. Swap ARR [POS] and
SWAP A[J] and A[J+1] ARR [N–2] so that ARR [0], ARR [1], ..., ARR [N–1] is sorted.
[END OF INNER LOOP] Algorithm for Selection Sort
[END OF OUTER LOOP] SMALLEST (ARR, K, N, POS)
Step:4 EXIT Step 1: [INITIALIZE] SET SMALL = ARR[K]
Insertion Sort Step 2: [INITIALIZE] SET POS=K
Insertion sort is a very simple sorting algorithm in which the Step 3: Repeat for J= K+1 to N
sorted array is built one element at a time. The main idea IF SMALL > ARR[J]
behind insertion sort is that it inserts each item into its SET SMALL = ARR[J]
proper place in the final list. To save memory, most SET POS=J
implementations of the insertion sort algorithm work by [END OF IF]
moving the current data element past the already sorted [END OF LOOP]
values and repeatedly interchanging it with the preceding Step 4: RETURN POS
value until it is in its correct place. Insertion sort is less SELECTION SORT (ARR, N)
efficient as compared to other more advanced algorithms Step 1: Repeat Steps 2 and 3 for K=1 to N-1
such as quick sort, heap sort, and merge sort. Step 2: CALL SMALLEST (ARR, K, N, POS)
Technique for Insertion Sort Step 3: SWAP A[K] with ARR[POS]
a. The array of values to be sorted is divided into two sets. [END OF LOOP]
One that stores sorted values and another that contains Step 4: EXIT
unsorted values.
b. The sorting algorithm will proceed until there are
elements in the unsorted set.
c. Suppose there are n elements in the array. Initially, the
element with index 0 (assuming LB = 0) is in the sorted set.
Rest of the elements are in the unsorted set.
Merge Sort Quick Sort
Merge sort is a sorting algorithm that uses the divide, The quick sort algorithm works as follows:
conquer, and combine algorithmic paradigm. Divide means 1. Select an element pivot from the array elements.
partitioning the n-element array to be sorted into two sub- 2. Rearrange the elements in the array in such a way that
arrays of n/2 elements. all elements that are less than the pivot appear before the
If A is an array containing zero or one element, then it is pivot and all elements greater than the pivot element come
already sorted. However, if there are more elements in the after it (equal values can go either way). After such a
array, divide A into two sub-arrays, A1 and A2, each partitioning, the pivot is placed in its final position. This is
containing about half of the elements of A. called the partition operation.
Conquer means sorting the two sub-arrays recursively using 3. Recursively sort the two sub-arrays thus obtained. (One
merge sort. with sub-list of values smaller than that of the pivot
Combine means merging the two sorted sub-arrays of size element and the other having higher value elements.)
n/2 to produce the sorted array of n elements. Algorithm for Quick Sort
Technique for Merge Sort PARTITION (ARR, BEG, END, LOC)
a. If the array is of length 0 or 1, then it is already sorted. Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, LOC =
b. Otherwise, divide the unsorted array into two sub-arrays BEG, FLAG = 0
of about half the size. Step 2: Repeat Steps 3 to 6 while FLAG = 0
c. Use merge sort algorithm recursively to sort each sub- Step 3: Repeat while ARR[LOC] <= ARR[RIGHT] AND LOC !=
array. RIGHT
d. Merge the two sub-arrays to form a single sorted list. SET RIGHT = RIGHT-1
Algorithm for Merge Sort [END OF LOOP]
MERGE (ARR, BEG, MID, END) Step 4: IF LOC = RIGHT
Step 1: [INITIALIZE] SET I= BEG, J= MID+1, INDEX = 0 SET FLAG=1
Step 2: Repeat while (I <= MID) AND (J<=END) ELSE IF ARR[LOC] > ARR[RIGHT]
IF ARR[I] < ARR[J] SWAP ARR[LOC] with ARR[RIGHT]
SET TEMP[INDEX] = ARR[I] SET LOC = RIGHT
SET I=I+1 [END OF IF]
ELSE Step 5: IF FLAG = 0
SET TEMP[INDEX] = ARR[J] Repeat while ARR[LOC] >= ARR[LEFT]AND LOC != LEFT
SET J=J+1 SET LEFT = LEFT+1
[END OF IF] [END OF LOOP]
SET INDEX = INDEX+1 Step 6: IF LOC = LEFT
[END OF LOOP] SET FLAG=1
Step 3: [Copy the remaining elements of right sub-array, if] ELSE IF ARR[LOC] < ARR[LEFT
IF I> MID SWAP ARR[LOC] with ARR[LEFT]
Repeat while J<= END SET LOC = LEFT
SET TEMP[INDEX] = ARR[J] [END OF IF]
SET INDEX = INDEX+1, SET J=J+1 [END OF IF]
[END OF LOOP] Step 7: [END OF LOOP]
[Copy the remaining elements of left sub-array, if any] Step 8: END
ELSE QUICK_SORT (ARR, BEG, END)
Repeat while I<= MID SET Step 1: IF (BEG < END)
TEMP[INDEX] = ARR[I] CALL PARTITION (ARR, BEG, END, LOC)
SET INDEX = INDEX+1, CALL QUICKSORT(ARR, BEG, LOC-1)
SET I=I+1 CALL QUICKSORT(ARR, LOC+1, END)
[END OF LOOP] [END OF IF]
[END OF IF] Step 2: END
Step 4: [Copy the contents of TEMP back to ARR] SET K= 0
Step 5: Repeat while K< INDEX
SET ARR[K] = TEMP[K]
SETK=K+1
[END OF LOOP]
Step 6: END
MERGE_SORT(ARR, BEG, END)
Step 1: IF BEG < END
SET MID = (BEG + END)/2
CALL MERGE_SORT (ARR, BEG, MID)
CALL MERGE_SORT (ARR, MID+1, END)
MERGE (ARR, BEG, MID, END)
[END OF IF]
Step 2: END
Besttimec Avgtimec Worsttime Worstspace Merg O (nlogn) O (nlogn) O (nlogn) O (n)
omplexity omplexity complexity complexity e
Linea O (1) O (n) O (n) O (1) sort
r Quic O (nlogn) O (nlogn) O (n2 ) O (nlogn)
searc k sort
h
Binar O (1) O (log n) O (log n) O (1) Radix sort O (nk) O (nk) O (nk) O (n+k)
y
searc Radix Sort
h Radix is used on integers, sorting is done on each of the
Open O (1) O (1) O (n) O (n) digits in the number. The sorting procedure proceeds by
addr sorting the least significant to the most significant digit.
essin While sorting the numbers, we have ten buckets, each for
g one digit (0, 1, 2, …, 9) and the number of passes will
Close O (1) O (1) O (m+n) O (m+n) depend on the length of the number having maximum
addr number of digits
essin
g
Bubb O (n) O (n2 ) O n2 ) O (1)
le
Inser O (n ) O (n2 ) O (n2 ) O (1)
tion
sort
Selec O (n2 ) O (n2 ) O (n2 ) O (1)
tion
sort
Algorithm for Radix Sort
Radix Sort (ARR, N)
Step 1: Find the largest number in ARR as LARGE
Step 2: [INITIALIZE] SET NOP = Number of digits in LARGE
Step 3: SET PASS = 0
Step 4: Repeat Step 5 while PASS <= NOP-1
Step 5: SET I= 0 and INITIALIZE buckets
Step 6: Repeat Steps 7 to 9 while I < N – 1
Step 7: SET DIGIT = digit at PASSth place in A[I]
Step 8: Add A[I] to the bucket numbered DIGIT
Step 9: INCEREMENT bucket count for bucket numbered
DIGIT
[END OF LOOP]
Step 10: Collect the numbers in the bucket
[END OF LOOP]
Step 11: END
GARBAGE COLLECTION 4. It is not more efficient in terms of space and time
In an operating system, garbage piles up rather quickly. complexity.
Memory is reserved, allocated, reallocated, and swapped 5. The computer may run out of memory if the recursive
across processes. Often there are memory leaks, which is calls are not properly checked.
when memory is allocated to a program or a process, but WINDING PHASE
never released back to the OS. In Winding phase, the recursive function keeps calling itself.
Individual programs also reserve and use memory, and a This phase ends when the base condition is reached.
lot of times that memory isn't given back. It can become UNWINDING PHASE
stranded. Therefore, an operating system needs its own In Unwinding phase When the base condition is reached,
way to keep things clean. unwinding phase starts and control returns back to the
original call.
Garbage collection (GC) in an operating system (OS) is a DIRECT RECURSION
dynamic memory management process that finds stranded In the direct recursion, functions call themselves. This kind
blocks of memory and reallocates them. Like the noisy of operation consists of a singlestage recursive call by the
garbage trucks that rumble down our streets, a GC function from within itself.
algorithms' job is to find stranded addresses, unused Example:
addresses, and clean them up. How this is performed can void directRecFun()
vary, as there are different strategies, or algorithms, to {
achieve this purpose. // Some code....
Garbage collection is a function of an operating system or directRecFun();
programming language that reclaims memory no longer in // Some code...
use. For example, Java and .NET have built-in garbage }
collection, but C and C++ do not, and programmers have to INDIRECT RECURSION
write the code to allocate and deallocate, which is tedious Indirect recursion happens when functions call other
and error prone. functions to call the initial function. This specific course of
RECURSION: WRITING A RECURSIVE FUNCTION action consists of 2 simple steps when developing a
The process in which a function calls itself directly or recursive call, essentially making functions call functions to
indirectly is called recursion and the corresponding generate a recursive call. Mutual recursion could be
function is called a recursive function. Using a recursive referred to as indirect recursion.
algorithm, certain problems can be solved quite easily. EXAMPLE
Examples of such problems are Towers of Hanoi (TOH), void indirectRecFun1()
Inorder/Preorder/Post-order Tree Traversals, DFS of Graph, {
etc. // Some code...
A recursive function solves a particular problem by calling a indirectRecFun2();
copy of itself and solving smaller subproblems of the // Some code...
original problems. Many more recursive calls can be }
generated as and when required. void indirectRecFun2()
Writing a recursive function is almost the same as reading {
one: // Some code...
1. Create a regular function with a base case that can be indirectRecFun1();
reached with its parameters // Some code...
2. Pass arguments into the function that immediately }
trigger the base case TAIL RECURSION
3. Pass the next arguments that trigger the recursive call A recursive function is referred to as tail-recursive if the
just once. recursive call is the end execution executed by the
Advantages of recursion: function. Let us try to figure out the definition with the help
1. The code may be easier to write. of one Example.
2. To solve such problems which are naturally recursive int fun(int z)
such as tower of Hanoi. {
3. Reduce unnecessary calling of function. printf(“%d”,z);
4. Extremely useful when applying the same solution. fun(z-1);
5. Recursion reduce the length of code. //Recursive call is last executed statement
6. It is very useful in solving the data structure problem. }
7. Stacks evolutions and infix, prefix, postfix evaluations
etc.
Disadvantages of recursion
1. Recursive functions are generally slower than non-
recursive function.
2. It may require a lot of memory space to hold
intermediate results on the system stacks.
3. Hard to analyze or understand the code.
SEQUENTIAL FIT METHODS: The two smaller parts of the block are of equal size and
For both fixed and dynamic memory allocation schemes, called buddies. The buddy system is a procedure in which
the operating system must keep list of each memory two individual buddies operate together as a single unit so
location noting which are free and which are busy. Then as that they can monitor and help each other. Similarly, one
new jobs come into the system, the free partitions must be of the two buddies will further divide into smaller parts
allocated. until the request is fulfilled. There are two types of buddy
FIRST FIT METHOD system which is binary buddy system and Fibonacci buddy
In this method, first job claims the first available memory system.
with space more than or equal to it’s size. The operating BINARY BUDDY SYSTEM
system doesn’t search for appropriate partition but just The buddy system maintains a list of the free blocks of
allocate the job to the nearest memory partition available each size (called a free list) so that it is easy to find a block
with sufficient size. of the desired size if one is available. If no block of the
BEST FIT METHOD requested size is available, allocate searches for the first
This method keeps the free/busy list in order by size – non-empty list for blocks of at least the size requested. In
smallest to largest. In this method, the operating system either case, a block is removed from the free list.
first searches the whole of the memory according to the
size of the given job and allocates it to the closest-fitting
free partition in the memory, making it able to use memory
efficiently. Here the jobs are in the order from smallest job
to largest job.
WORST FIT METHODS.
In this allocation technique, the process traverses the
whole memory and always search for the largest
hole/partition, and then the process is placed in that
hole/partition. It is a slow process because it has to
traverse the entire memory to search the largest hole.
FRAGMENTATION
Fragmentation is an unwanted problem in the operating
FIBONACCI BUDDY SYSTEM
system in which the processes are loaded and unloaded
A Fibonacci buddy system is a system in which blocks are
from memory, and free memory space is fragmented.
divided into sizes which are Fibonacci numbers
Processes can't be assigned to memory blocks due to their
small size, and the memory blocks stay unused.
It is also necessary to understand that as programs are
loaded and deleted from memory, they generate free space
or a hole in the memory. These small blocks cannot be
allotted to new arriving processes, resulting in inefficient
memory use.
User processes are loaded and unloaded from the main
memory, and processes are kept in memory blocks in the
main memory. Many spaces remain after process loading COMPACTION
and swapping that another process cannot load due to Compaction is a technique to collect all the free memory
their size. Main memory is available, but its space is present in form of fragments into one large chunk of free
insufficient to load another process because of the memory, which can be used to run other processes. It does
dynamical allocation of main memory processes. that by moving all the processes towards one end of the
Types of Fragmentation memory and all the available free space towards the other
Internal Fragmentation: end of the memory so that it becomes contiguous. It is not
When a process is allocated to a memory block, and if the always easy to do compaction. Compaction can be done
process is smaller than the amount of memory requested, only when the relocation is dynamic and done at execution
a free space is created in the given memory block. Due to time. Compaction cannot be done when relocation is static
this, the free space of the memory block is unused, which and is performed at load time or assembly time.
causes internal fragmentation.
External Fragmentation:
External fragmentation happens when a dynamic memory
allocation method allocates some memory but leaves a
small amount of memory unusable. The quantity of
available memory is substantially reduced if there is too
much external fragmentation. There is enough memory
space to complete a request, but it is not contiguous. It's
known as external fragmentation.

BUDDY SYSTEMS
APPLICATION OF QUEUES Disadvantage of Round-robin Scheduling • If slicing time
1. Scheduling of OS is low, the processor output will be reduced. • This
The process scheduling is the activity of the process method spends more time on context switching • Its
manager that handles the removal of the running process performance heavily depends on time quantum. • Round-
from the CPU and the selection of another process on the robin scheduling doesn’t give special priority to more
basis of a particular strategy. Process scheduling is an important tasks. • Lower time quantum results in higher
essential part of a Multiprogramming operating systems. the context switching overhead in the system. • Finding a
Such operating systems allow more than one process to be correct time quantum is a quite difficult task in this system.
loaded into the executable memory at a time and the APPLICATION OF TREES
loaded process shares the CPU using time multiplexing. Huffman Tree
The OS maintains all Process Control Blocks (PCBs) in Huffman coding is an entropy encoding algorithm
Process Scheduling Queues. The OS maintains a separate developed by David A. Huffman that is widely used as a
queue for each of the process states and PCBs of all lossless data compression technique. The Huffman coding
processes in the same execution state are placed in the algorithm uses a variable length code table to encode a
same queue. When the state of a process is changed, its source character where the variable-length code table is
PCB is unlinked from its current queue and moved to its derived on the basis of the estimated probability of
new state queue. The OS can use different policies to occurrence of the source character. The key idea behind
manage each queue (FIFO, Round Robin, Priority, etc.). The Huffman algorithm is that it encodes the most common
OS scheduler determines how to move processes between characters using shorter strings of bits than those used for
the ready and run queues which can only have one entry less common source characters. The algorithm works by
per processor core on the system; in the above diagram, it creating a binary tree of nodes that are stored in an array.
has been merged with the CPU. A node can be either a leaf node or an internal node.
2. Round Robin Scheduling Initially, all the nodes in the tree are at the leaf level and
Round Robin scheduling algorithm is one of the most store the source character and its frequency of occurrence
popular scheduling which can actually be implemented in also known as weight.
most of the operating systems. This is the preemptive
version of first come first serve scheduling. The Algorithm
focuses on Time Sharing. In this algorithm, every process
gets executed in a cyclic way. A certain time slice is defined
in the system which is called time quantum. Each process
present in the ready queue is assigned the CPU for that
time quantum, if the execution of the process is completed
during that time then the process will terminate else the
process will go back to the ready queue and waits for the
next turn to complete the execution.

Advantage of Round-robin Scheduling • It doesn’t face the


issues of starvation or convoy effect. • All the jobs get a fair
allocation of CPU. • It deals with all process without any
priority • This scheduling method does not depend upon
burst time. That’s why it is easily implementable on the
system. • Once a process is executed for a specific set of
the period, the process is preempted, and another process
executes for that given time period. • Allows OS to use the
Context switching method to save states of preempted
processes. • It gives the best performance in terms of
average response time.
Heap Sort • Finding the shortest path between two nodes, u and v, of
Heap sort also known as tournament sort processes the a weighted graph
elements by creating the min-heap or maxheap using the ALGORITHM
elements of the given array. Min-heap or max-heap Step 1: SET STATUS=1 (ready state) for each node in G
represents the ordering of array in which the root element Step 2: Enqueue the starting node A and set its STATUS=2
represents the minimum or maximum element of the array. (waiting state)
Heap sort basically recursively performs two main Step 3: Repeat Steps 4 and 5 until QUEUE is empty
operations – Step 4: Dequeue a node N. Process it and set its STATUS=3
o Build a heap H, using the elements of array. (processed state).
o Repeatedly delete the root element of the heap formed Step 5: Enqueue all the neighbours of N that are in the
in 1st phase. ready state (whose STATUS=1) and set their STATUS=2
A heap is a complete binary tree, and the binary tree is a (waiting state)
tree in which the node can have the utmost two children. A [END OF LOOP]
complete binary tree is a binary tree in which all the levels Step 6: EXIT
except the last level, i.e., leaf node, should be completely DEPTH FIRST SEARCH
filled, and all the nodes should be left-justified. The depth-first search algorithm progresses by expanding
Heap sort is a popular and efficient sorting algorithm. The the starting node of G and then going deeper and deeper
concept of heap sort is to eliminate the elements one by until the goal node is found, or until a node that has no
one from the heap part of the list, and then insert them children is encountered. When a dead-end is reached, the
into the sorted part of the list. Heap sort is the in-place algorithm backtracks, returning to the most recent node
sorting algorithm. that has not been completely explored.
Algorithm for Heap Applications of Depth-First Search
Sort HeapSort(arr) 1. Finding a path between two specified nodes, u and v, of
BuildMaxHeap(arr) an unweighted graph.
for i = length(arr) to 2 2. Finding a path between two specified nodes, u and v, of
swap arr[1] with arr[i] a weighted graph.
heap_size[arr] = heap_size[arr] ? 1 3. Finding whether a graph is connected or not.
MaxHeapify(arr,1) 4. Computing the spanning tree of a connected graph.
End ALGORITHM
BuildMaxHeap(arr) Step 1: SET STATUS=1 (ready state) for each node in G
BuildMaxHeap(arr) Step 2: Push the starting nodeAon the stack and set its
heap_size(arr) = length(arr) STATUS=2 (waiting state)
for i = length(arr)/2 to 1 Step 3: Repeat Steps 4 and 5 until STACK is empty
MaxHeapify(arr,i) Step 4: Pop the top node N. Process it and set its STATUS=3
End (processed state)
MaxHeapify(arr,i) Step 5: Push on the stack all the neighbours of N that are in
MaxHeapify(arr,i the ready state (whose STATUS=1) and set their STATUS=2
L = left(i) (waiting state)
R = right(i) [END OF LOOP]
if L ? heap_size[arr] and arr[L] > arr[i] Step 6: EXIT
largest = L
else
largest = i
if R ? heap_size[arr] and arr[R] > arr[largest]
largest = R
if largest != i swap arr[i] with arr[largest]
MaxHeapify(arr,largest)
End
BREADTH FIRST SEARCH
Breadth-first search (BFS) is a graph search algorithm that
begins at the root node and explores all the neighboring
nodes. Then for each of those nearest nodes, the algorithm
explores their unexplored neighbor nodes, and so on, until
it finds the goal.
Applications of Breadth-
• Finding all connected components in a graph G.
• Finding all nodes within an individual connected
component.
• Finding the shortest path between two nodes, u and v, of
an unweighted graph.
SEARCHING A NODE IN BST Algorithm:
SearchElement (TREE, VAL)
Step 1: IF TREE -> DATA = VAL OR TREE = NULL
Return TREE
ELSE
IF VAL < TREE -> DATA Return
searchElement(TREE -> LEFT, VAL)
ELSE
Return searchElement(TREE -> RIGHT, VAL) [END OF IF] Traversing a Threaded Binary Tree
[END OF IF] Step 1: Check if the current node has a left child that has
Step 2: END not been visited. If a left child exists that has not been
INSERTING A NEW NODE IN BST Algorithm: visited, go to Step 2, else go to Step 3.
Insert (TREE, VAL) Step 2: Add the left child in the list of visited nodes. Make it
Step 1: IF TREE = NULL as the current node and then go to Step 6.
Allocate memory for TREE Step 3: If the current node has a right child, go to Step 4
SET TREE -> DATA = VAL else go to Step 5.
SET TREE -> LEFT = TREE -> RIGHT = NULL Step 4: Make that right child as current node and go to Step
ELSE 6.
IF VAL < TREE -> DATA Step 5: Print the node and if there is a threaded node make
Insert(TREE -> LEFT, VAL) it the current node.
ELSE Step 6: If all the nodes have visited then END else go to
Insert(TREE -> RIGHT, VAL) Step 1.
[END OF IF] [END OF IF] DECISION TREE
Step 2: END Decision Tree is the most powerful and popular tool for
DELETEING A NODE IN BST ALGOITHM classification and prediction. A Decision tree is a flowchart-
Delete (TREE, VAL) like tree structure, where each internal node denotes a test
Step 1: IF TREE = NULL Write "VAL not found in the tree" on an attribute, each branch represents an outcome of the
ELSE IF VAL < TREE DATA test, and each leaf node (terminal node) holds a class label.
Delete(TREE->LEFT, VAL)
ELSE IF VAL > TREE DATA
Delete(TREE ->RIGHT, VAL)
ELSE IF TREE-> LEFT AND TREE ->RIGHT
SET TEMP = findLargestNode(TREE ->LEFT)
SET TREE-> DATA = TEMP-> DATA
Delete(TREE-> LEFT, TEMP ->DATA)
ELSE
SET TEMP = TREE
IF TREE-> LEFT = NULL AND TREE-> RIGHT = NULL
SET TREE = NULL
ELSE IF TREE ->LEFT != NULL
SET TREE = TREE ->LEFT
ELSE
SET TREE = TREE-> RIGHT GAME TREE
[END OF IF] In game theory, a game tree is a directed graph whose
FREE TEMP nodes are positions in a game (e.g., the arrangement of the
[END OF IF] pieces in a board game) and whose edges are moves (e.g.,
Step 2: END to move pieces from one position on a board to another).
THREADED BINARY TREE A threaded binary tree is the DIGITAL SEARCH TREE
same as that of a binary tree but with a difference in A digital search tree is a binary tree in which each node
storing the NULL pointers. Consider the linked contains one element. The element to node assignment is
representation of a binary tree as given in Figure: In the determined by the binary representation of the element
linked representation, a number of nodes contain a NULL keys.
pointer, either in their left or right fields or in both. This
space that is wasted in storing a NULL pointer can be
efficiently used to store some other useful piece of
information. For example, the NULL entries can be replaced
to store a pointer to the in-order predecessor or the in-
order successor of the node. These special pointers are
called threads and binary trees containing threads are
called threaded trees.
AVL TREE newly inserted node. The possible changes which may take
The AVL tree is also known as a height-balanced tree. The place in any node on the path are as follows: • Initially, the
key advantage of using an AVL tree is that it takes O(log n) node was either left- or right-heavy and after insertion, it
time to perform search, insert, and delete operations in an becomes balanced. • Initially, the node was balanced and
average case as well as the worst case because the height after insertion, it becomes either left- or right-heavy. •
of the tree is limited to O(log n). The structure of an AVL Initially, the node was heavy (either left or right) and the
tree is the same as that of a binary search tree but with a new node has been inserted in the heavy sub-tree, thereby
little difference. In its structure, it stores an additional creating an unbalanced sub-tree. Such a node is said to be a
variable called the Balance Factor. Thus, every node has a critical node.
balance factor associated with it. The balance factor of a There are four types of rebalancing rotations and
node is calculated by subtracting the height of its right sub- application of these rotations depends on the position of
tree from the height of its left sub-tree. A binary search the inserted node with reference to the critical node. The
tree in which every node has a balance factor of –1, 0, or 1 four categories of rotations are:
is said to be height balanced. A node with any other LL rotation: The new node is inserted in the left sub-tree of
balance factor is considered to be unbalanced and requires the left sub-tree of the critical node.
rebalancing of the tree.
Balance factor = Height (left sub-tree) – Height (right sub-
tree)
• If the balance factor of a node is 1, then it means that the
left sub-tree of the tree is one level higher than that of the
right sub-tree. Such a tree is therefore called as a left-heavy
tree.
• If the balance factor of a node is 0, then it means that the
height of the left sub-tree (longest path in the left sub-tree)
RR rotation: The new node is inserted in the right sub-tree
is equal to the height of the right sub-tree.
of the right sub-tree of the critical node.
• If the balance factor of a node is –1, then it means that
the left sub-tree of the tree is one level lower than that of
the right sub-tree. Such a tree is therefore called as a right
heavy tree

. LR rotation and RL Rotation: The new node is inserted in


the right sub-tree of the left sub-tree of the critical node.
The new node is inserted in the left sub-tree of the right
subtree of the critical node.
OPERATIONS ON AVL TREE
Searching for a Node in an AVL Tree
Searching in an AVL tree is performed exactly the same
way as it is performed in a binary search tree. Due to the
height-balancing of the tree, the search operation takes
O(log n) time to complete. Since the operation does not
modify the structure of the tree, no special provisions are
required.
Inserting a new node in an AVL
Tree Insertion in an AVL tree is also done in the same way
Deleting a Node from an AVL Tree Deletion of a node in an
as it is done in a binary search tree. In the AVL tree, the
AVL tree is similar to that of binary search trees. But it goes
new node is always inserted as the leaf node. But the step
one step ahead. Deletion may disturb the AVLness of the
of insertion is usually followed by an additional step of
tree, so to rebalance the AVL tree, we need to perform
rotation. Rotation is done to restore the balance of the
rotations. There are two classes of rotations that can be
tree. However, if insertion of the new node does not
performed on an AVL tree after deleting a given node.
disturb the balance factor, that is, if the balance factor of
These rotations are R rotation and L rotation. On deletion
every node is still –1, 0, or 1, then rotations are not
of node X from the AVL tree, if node A becomes the critical
required.
node (closest ancestor node on the path from X to the root
During insertion, the new node is inserted as the leaf node,
node that does not have its balance factor as 1, 0, or –1),
so it will always have a balance factor equal to zero. The
then the type of rotation depends on whether X is in the
only nodes whose balance factors will change are those
left sub-tree of A or in its right sub-tree.
which lie in the path between the root of the tree and the
B- TREE the second case, an internal node has to be deleted. Let us
B-tree is a special type of self-balancing search tree in first see the steps involved in deleting a leaf node.
which each node can contain more than one key and can 1. Locate the leaf node which has to be deleted.
have more than two children. It is a generalized form of the 2. If the leaf node contains more than the minimum
binary search tree. It is also known as a height-balanced m- number of key values (more than m/2 elements), then
way tree. delete the value.
A B tree of order m can have a maximum of m–1 keys and 3. Else if the leaf node does not contain m/2 elements, then
m pointers to its sub-trees. A B tree may contain a large fill the node by taking an element either from the left or
number of key values and pointers to subtrees. Storing a from the right sibling.
large number of keys in a single node keeps the height of a. If the left sibling has more than the minimum number of
the tree relatively small. A B tree is designed to store sorted key values, push its largest key into its parent’s node and
data and allows search, insertion, and deletion operations pull down the intervening element from the parent node to
to be performed in logarithmic amortized time. A B tree of the leaf node where the key is deleted.
order m (the maximum number of children that each node b. Else, if the right sibling has more than the minimum
can have) is a tree with all the properties of an M-way number of key values, push its smallest key into its parent
search tree. In addition, it has the following properties: node and pull down the intervening element from the
1. Every node in the B tree has at most (maximum) m parent node to the leaf node where the key is deleted.
children. 4. Else, if both left and right siblings contain only the
2. Every node in the B tree except the root node and leaf minimum number of elements, then create a new leaf node
nodes has at least (minimum) m/2 children. This condition by combining the two leaf nodes and the intervening
helps to keep the tree bushy so that the path from the root element of the parent node (ensuring that the number of
node to the leaf is very short, even in a tree that stores a lot elements does not exceed the maximum number of
of data. elements a node can have, that is, m). If pulling the
3. The root node has at least two children if it is not a intervening element from the parent node leaves it with
terminal (leaf) node. less than the minimum number of keys in the node, then
4. All leaf nodes are at the same level. An internal node in propagate the process upwards, thereby reducing the
the B tree can have n number of children, where 0 < n < m. height of the B tree.
Applications of B Trees
It is not necessary that every node has the same number of : 1. Disk access is very slow. We should be able to fetch a
children, but the only restriction is that the node should large amount of data in one disk access.
have at least m/2 children 2. Disk is a block-oriented device. That is, data is organized
and retrieved in terms of blocks. So while using a B tree
(generalized M-way search tree), a large value of m is used
so that one single node of the tree can occupy the entire
block. In other words, m represents the maximum number
OPERATIONS ON B-TREE of data items that can be stored in a single block. m is
Searching for an Element in a B Tree maximized to speed up processing. More the data stored in
Searching for an element in a B tree is similar to that in a block, lesser the time needed to move it into the main
binary search trees. If the value is less then root value then memory.
we will skip right sub-tree and vice versa. Since the running 3. A large value minimizes the height of the tree. So, search
time of the search operation depends upon the height of operation becomes really fast.
the tree, the algorithm to search for an element in a B tree B+ TREE
takes O(logt n) time to execute. A B+ tree is a variant of a B tree which stores sorted data in
Inserting a New Element in a B Tree a way that allows for efficient insertion, retrieval, and
Algorithm: removal of records, each of which is identified by a key.
Step 1: Search the B tree to find the leaf node where the While a B tree can store both keys and records in its
new key value should be inserted. interior nodes, a B+ tree, in contrast, stores all the records
Step 2: If the leaf node is not full, that is, it contains less at the leaf level of the tree; only keys are stored in the
than m–1 key values, then insert the new element in the interior nodes. The leaf nodes of a B+ tree are often linked
node keeping the node’s elements ordered. to one another in a linked list. This has an added advantage
Step 3: If the leaf node is full, that is, the leaf node already of making the queries simpler and more efficient. Typically,
contains m–1 key values, then B+ trees are used to store large amounts of data that
1.insert the new value in order into the existing set of keys, cannot be stored in the main memory. With B+ trees, the
2. split the node at its median into two nodes (note that the secondary storage (magnetic disk) is used to store the leaf
split nodes are half full), and nodes of trees and the internal nodes of trees are stored in
3. push the median element up to its parent’s node. If the the main memory. B+ trees store data only in the leaf
parent’s node is already full, then split the parent node by nodes. All other nodes (internal nodes) are called index
following the same steps. nodes or i-nodes and store index values. This allows us to
Deleting an Element from a B Tree There are two cases of traverse the tree from the root down to the leaf node that
deletion. In the first case, a leaf node has to be deleted. In stores the desired data item.
COLLISION RESOLUTION hashed to l. However, if no key value hashes to l, then
Collisions occur when the hash function maps two location l in the hash table contains NULL.
different keys to the same location. Two records cannot be Applications of Hashing
stored in the same location. Therefore, a method used to 1. Message Digest
solve the problem of collision, also called collision 2. Password Verification
resolution technique, is applied. The two most popular 3. Data Structures (Programming Languages)
methods of resolving collisions are: 4. Compiler Operation
1.Open Addressing Once a collision takes place, open 5. Rabin-Karp Algorithm
addressing computes new positions using a probe sequence 6. Linking File name and path together
and the next record is stored in that position. In this 7. Game Boards
technique, all the values are stored in the hash table. The 8. Graphics
hash table contains two types of values: sentinel values
(e.g., –1) and data values. The presence of a sentinel value
indicates that the location contains no data value at
present but can be used to hold a value.
When a key is mapped to a particular memory location,
then the value it holds is checked. If it contains a sentinel
value, then the location is free and the data value can be
stored in it. However, if the location already has some data
value stored in it, then other slots are examined
systematically in the forward direction to find a free slot. If
even a single free location is not found, then we have an
OVERFLOW condition. The process of examining memory
locations in the hash table is called probing. Open
addressing technique can be implemented using linear
probing, quadratic probing, double hashing, and rehashing.
Linear Probing
The simplest approach to resolve a collision is linear
probing. In this technique, if a value is already stored at a
location generated by hash function, then the following
hash function is used to resolve the collision: h (k, i) = [h’(k)
+ i] mod m Where k is key value, i is iteration, M is size of
table and h’ (k) is hash function.
Quadratic Probing
In this technique, if a value is already stored at a location
generated by h(k), then the following hash function is used
to resolve the collision: h(k, i) = [h’ (k) + c1 i + c2 i2 ] mod m
where m is the size of the hash table, h’ (k) = (k mod m), i is
the probe number that varies from 0 to m– 1, and c1 and c2
are constants such that c1 and c2 ≠ 0.
Rehashing OR Double Hashing
To start with, double hashing uses one hash value and then
repeatedly steps forward an interval until an empty
location is reached. The interval is decided using a second,
independent hash function, hence the name double
hashing. In double hashing, we use two hash functions
rather than a single function. The hash function in the case
of double hashing can be given as: h(k, i) = [h1 (k) + ih2 (k)]
mod m where m is the size of the hash table, h1 (k) and h2
(k) are two hash functions given as h1 (k) = k mod m, h2 (k)
= k mod m', i is the probe number that varies from 0 to m–
1, and m' is chosen to be less than m.
2.Closed Addressing
A key is always stored in the hash table it's hashed to.
Collisions are dealt with using separate data structures on a
per-hash basis.
Chaining
In chaining, each location in a hash table stores a pointer to
a linked list that contains all the key values that were
hashed to that location. That is, location l in the hash table
points to the head of the linked list of all the key values that

You might also like