0% found this document useful (0 votes)
14 views62 pages

LEARNING MATERIALS-Algorithm-UNIT 3 Modified On 28.08.2024

Uploaded by

SUDAM DUTTA
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)
14 views62 pages

LEARNING MATERIALS-Algorithm-UNIT 3 Modified On 28.08.2024

Uploaded by

SUDAM DUTTA
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/ 62

DH SIR’S CLASSROOM

LEARNING MATERIAL
Department: Computer Science and Technology
Semester: 3rd
Subject: ALGORITHM
UNIT: (3) Searching
Teacher: Debasish Hati
__________________________________________________________________________________________________________

CONTENTS:
1. Linear search algorithm
2. Binary search algorithm
3. Computation of best, average and worst case time complexity of linear and binary search.
4. Binary search tree: algorithm, searching time and space complexity.
5. Balanced search trees: what is the signification and advantage of height balancing? Insertion, deletion and
searching algorithm of deferent types of balanced search trees and their comparative study.
6. Hashing, hash tables, hash function, collision resolving techniques..
7. Symbol table

__________________________________________________________________________________________________________

 Searching
 Searching is a process of finding a particular element among several given elements.
 The search is successful if the required element is found.
 Otherwise, the search is unsuccessful.

 Searching Algorithms-
▫ Searching Algorithms are a family of algorithms used for the purpose of searching.
▫ The searching of an element in the given array may be carried out in the following two ways-

1. LINEAR SEARCH :-
 Linear Search is the simplest searching algorithm.
 It traverses the array sequentially to locate the required element.
 It searches for an element by comparing it with each element of the array one by one.
 So, it is also called as Sequential Search.
 Linear Search Algorithm is applied when-
 No information is given about the array.
 The given array is unsorted or the elements are unordered.
 The list of data items is smaller.

 Linear Search Algorithm-


Consider-
▫ There is a linear array ‘a’ of size ‘n’.
▫ Linear search algorithm is being used to search an element ‘item’ in this linear array.
▫ If search ends in success, it sets loc to the index of the element otherwise it sets loc to -1.
Then, Linear Search Algorithm is as follows-
Linear_Search (a , n , item , loc)
1. Begin
2. for i = 0 to (n - 1) by 1 do
3. if (a[i] = item) then
4. set loc = i
5. Exit
6. endif
7. endfor
8. set loc = -1
9. End

 Time Complexity Analysis-


Linear Search time complexity analysis is done below-

a. Best Case:
 The Best Case will take place if: The element to be search is on the first index.
 The number of comparisons in this case is 1. Thereforce, Best Case Time Complexity of Linear
Search is O(1).

b. Average Case:
 Let there be N distinct numbers: a1, a2, ..., a(N-1), aN
 We need to find element P.
 There are two cases:
Case 1: The element P can be in N distinct indexes from 0 to N-1.
Case 2: There will be a case when the element P is not present in the list.

 There are N case 1 and 1 case 2. So, there are N+1 distinct cases to consider in total.
 If element P is in index K, then Linear Search will do K+1 comparisons.
 Number of comparisons for all cases in case 1 = Comparisons if element is in index 0 +
Comparisons if element is in index 1 + ... + Comparisons if element is in index N-1
= 1 + 2 + ... + N
= N * (N+1) / 2 comparisons
 If element P is not in the list, then Linear Search will do N comparisons.
 Number of comparisons for all cases in case 2 = N
 Therefore, total number of comparisons for all N+1 cases = N * (N+1) / 2 + N
= N * ((N+1)/2 + 1)
 Average number of comparisons = ( N * ((N+1)/2 + 1) ) / (N+1)
= N/2 + N/(N+1)
 The dominant term in "Average number of comparisons" is N/2. So, the Average Case Time
Complexity of Linear Search is O(N).

c. Worst Case:
 The worst case will take place if:
The element to be search is in the last index
The element to be search is not present in the list

 In both cases, the maximum number of comparisons take place in Linear Search which is equal to
N comparisons.
 Hence, the Worst Case Time Complexity of Linear Search is O(N).
 Number of Comparisons in Worst Case: N

 Linear Search Efficiency-


 Linear Search is less efficient when compared with other algorithms like Binary Search & Hash
tables.
 The other algorithms allow significantly faster searching.

 Linear Search Example-


Consider-
▫ We are given the following linear array.
▫ Element 15 has to be searched in it using Linear Search Algorithm.
Now,
▫ Linear Search algorithm compares element 15 with all the elements of the array one by one.
▫ It continues searching until either the element 15 is found or all the elements are searched.
Linear Search Algorithm works in the following steps-
▫ Step-01:
 It compares element 15 with the 1st element 92.
 Since 15 ≠ 92, so required element is not found.
 So, it moves to the next element.
▫ Step-02:
 It compares element 15 with the 2nd element 87.
 Since 15 ≠ 87, so required element is not found.
 So, it moves to the next element.
▫ Step-03:
 It compares element 15 with the 3rd element 53.
 Since 15 ≠ 53, so required element is not found.
 So, it moves to the next element.
▫ Step-04:
 It compares element 15 with the 4th element 10.
 Since 15 ≠ 10, so required element is not found.
 So, it moves to the next element.
▫ Step-05:
 It compares element 15 with the 5th element 15.
 Since 15 = 15, so required element is found.
 Now, it stops the comparison and returns index 4 at which element 15 is present.

2. BINARY SEARCH :-
 Binary Search is one of the fastest searching algorithms.
 It is used for finding the location of an element in a linear array.
 It works on the principle of divide and conquer technique.
 Binary Search Algorithm can be applied only on Sorted arrays.
 So, the elements must be arranged in-
▫ Either ascending order if the elements are numbers.
▫ Or dictionary order if the elements are strings.
 To apply binary search on an unsorted array,
▫ First, sort the array using some sorting technique.
▫ Then, use binary search algorithm.

 Binary Search Algorithm-


Consider-

 There is a linear array ‘a’ of size ‘n’.


 Binary search algorithm is being used to search an element ‘item’ in this linear array.
 If search ends in success, it sets loc to the index of the element otherwise it sets loc to -1.
 Variables beg and end keeps track of the index of the first and last element of the array or sub
array in which the element is being searched at that instant.
 Variable mid keeps track of the index of the middle element of that array or sub array in which
the element is being searched at that instant.
 Then, Binary Search Algorithm is as follows-
1. Begin
2. Set beg = 0
3. Set end = n-1
4. Set mid = (beg + end) / 2
5. while ( (beg <= end) and (a[mid] ≠ item) ) do
6. if (item < a[mid]) then
7. Set end = mid - 1
8. else
9. Set beg = mid + 1
10. endif
11. Set mid = (beg + end) / 2
12. endwhile
13. if (beg > end) then
14. Set loc = -1
15. else
16. Set loc = mid
17. endif
18. End

 Explanation
▫ Binary Search Algorithm searches an element by comparing it with the middle most element of
the array.
▫ Then, following three cases are possible-
▫ Case-01
 If the element being searched is found to be the middle most element, its index is returned.
▫ Case-02
 If the element being searched is found to be greater than the middle most element, then its
search is further continued in the right sub array of the middle most element.
▫ Case-03
 If the element being searched is found to be smaller than the middle most element, then its
search is further continued in the left sub array of the middle most element.
 This iteration keeps on repeating on the sub arrays until the desired element is found or size
of the sub array reduces to zero.

 Time Complexity:
a. Best Case:
 The best case of Binary Search occurs when: The element to be search is in the middle of the list
 In this case, the element is found in the first step itself and this involves 1 comparison.
 Therefore, Best Case Time Complexity of Binary Search is O(1).
b. Average Case:
 Let there be N distinct numbers: a1, a2, ..., a(N-1), aN
 We need to find element P.
 There are two cases:
Case 1: The element P can be in N distinct indexes from 0 to N-1.
Case 2: There will be a case when the element P is not present in the list.

 There are N case 1 and 1 case 2. So, there are N+1 distinct cases to consider in total.
 If element P is in index K, then Binary Search will do K+1 comparisons.
 This is because: The element at index N/2 can be found in 1 comparison as Binary Search starts
from middle.
 Similarly, in the 2nd comparisons, elements at index N/4 and 3N/4 are compared based on the
result of 1st comparison.
 On this line, in the 3rd comparison, elements at index N/8, 3N/8, 5N/8, 7N/8 are compared based
on the result of 2nd comparison.
 Based on this, we know that:
Elements requiring 1 comparison: 1
Elements requiring 2 comparisons: 2
Elements requiring 3 comparisons: 4
 Therefore, Elements requiring I comparisons: 2^(I-1)
 The maximum number of comparisons = Number of times N is divided by 2 so that result is 1 =
Comparisons to reach 1st element = logN comparisons
 I can vary from 0 to logN
 Total number of comparisons = 1 * (Elements requiring 1 comparison) + 2 * (Elements requiring 2
comparisons) + ... + logN * (Elements requiring logN comparisons)
 Total number of comparisons = 1 * (1) + 2 * (2) + 3 * (4) + ... + logN * (2^(logN-1))
 Total number of comparisons = 1 + 4 + 12 + 32 + ... = 2^logN * (logN - 1) + 1
 Total number of comparisons = N * (logN - 1) + 1
 Total number of cases = N+1
 Therefore, average number of comparisons = ( N * (logN - 1) + 1 ) / (N+1)
 Average number of comparisons = N * logN / (N+1) - N/(N+1) + 1/(N+1)
 The dominant term is N * logN / (N+1) which is approximately logN. Therefore, Average Case
Time Complexity of Binary Search is O(logN).
c. Worst Case:
 The worst case of Binary Search occurs when: The element is to search is in the first index or last
index
 In this case, the total number of comparisons required is logN comparisons.
 Therefore, Worst Case Time Complexity of Binary Search is O(logN).

 Binary Search Example-


Consider-

 We are given the following sorted linear array.


 Element 15 has to be searched in it using Binary Search Algorithm.

Binary Search Algorithm works in the following steps-


▫ Step-01:
 To begin with, we take beg=0 and end=6.
 We compute location of the middle element as-
Mid = (beg + end) / 2 = (0 + 6) / 2 = 3

 Here, a[mid] = a[3] = 20 ≠ 15 and beg < end.


 So, we start next iteration.
▫ Step-02:
 Since a[mid] = 20 > 15, so we take end = mid – 1 = 3 – 1 = 2 whereas beg remains unchanged.
 We compute location of the middle element as-
Mid = (beg + end) / 2 = (0 + 2) / 2 = 1

 Here, a[mid] = a[1] = 10 ≠ 15 and beg < end.


 So, we start next iteration.
▫ Step-03:
 Since a[mid] = 10 < 15, so we take beg = mid + 1 = 1 + 1 = 2 whereas end remains unchanged.
 We compute location of the middle element as-
Mid = (beg + end) / 2 = (2 + 2) / 2 = 2

 Here, a[mid] = a[2] = 15 which matches to the element being searched.


 So, our search terminates in success and index 2 is returned.

 Binary Search Algorithm Advantages-


The advantages of binary search algorithm are-

 It eliminates half of the list from further searching by using the result of each comparison.
 It indicates whether the element being searched is before or after the current position in the list.
 This information is used to narrow the search.
 For large lists of data, it works significantly better than linear search.

 Binary Search Algorithm Disadvantages-


The disadvantages of binary search algorithm are-

 It employs recursive approach which requires more stack space.


 Programming binary search algorithm is error prone and difficult.
 The interaction of binary search with memory hierarchy i.e. caching is poor.
(because of its random access nature)
 Important Note-
For in-memory searching, if the interval to be searched is small,

 Linear search may exhibit better performance than binary search.


 This is because it exhibits better locality of reference

LINEAR SEARCH VS BINARY SEARCH

Linear Search and Binary Search are two fundamental algorithms used to find elements in a list or array.
Here's a comparison between the two:

1. Definition:

 Linear Search: A straightforward search algorithm that checks each element in a list one by one from the start
to the end until the desired element is found or the list is exhausted.
 Binary Search: A more efficient search algorithm that works on sorted lists by repeatedly dividing the search
interval in half. It compares the target value with the middle element and eliminates half of the list from
further consideration.

2. Time Complexity:

 Linear Search: O(n)


o In the worst case, it requires checking each element, leading to a linear time complexity. For example,
searching for an element in a list of size n requires up to n comparisons.
 Binary Search: O(log n)
o Binary Search significantly reduces the number of comparisons by halving the search space with each
step, leading to a logarithmic time complexity.

3. Space Complexity:

 Linear Search: O(1)


o Linear Search requires constant space, as it only uses a single variable to track the current index or
element being compared.
 Binary Search: O(1) (Iterative) or O(log n) (Recursive)
o The iterative version of Binary Search also requires constant space. However, the recursive version
requires additional space for the call stack, proportional to the logarithm of the number of elements.

4. Input Requirements:

 Linear Search: No special requirements; it works on both sorted and unsorted lists.
 Binary Search: Requires the list to be sorted beforehand. If the list is unsorted, it must first be sorted, which
would take additional time.

5. Efficiency:

 Linear Search: Less efficient for large lists because it potentially needs to check every element.
 Binary Search: Much more efficient for large sorted lists, as it reduces the number of elements to check
significantly with each step.

6. Use Cases:

 Linear Search: Useful when dealing with small or unsorted datasets, or when the overhead of sorting the list
for Binary Search is not justified.
 Binary Search: Preferred for large, sorted datasets where the time savings from the logarithmic complexity
outweigh the costs of sorting (if needed).

7. Example:

 Linear Search:

int linearSearch(int arr[], int n, int x) {


for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i; // Return the index of the found element
}
}
return -1; // Return -1 if the element is not found
}

 Binary Search:

int binarySearch(int arr[], int n, int x) {


int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == x) {
return mid; // Return the index of the found element
}
if (arr[mid] < x) {
left = mid + 1; // Search in the right half
} else {
right = mid - 1; // Search in the left half
}
}
return -1; // Return -1 if the element is not found
}
Summary:

 Linear Search is simple and works on any list but is less efficient for large datasets.
 Binary Search is much faster but requires a sorted list and is more complex to implement.

 Binary Search Tree


 Binary Search Tree is a special kind of binary tree in which nodes are arranged in a specific order.
 In a binary search tree (BST), each node contains-
▫ Only smaller values in its left sub tree
▫ Only larger values in its right sub tree
 Example-

 Number of Binary Search Trees-


Example-
Number of distinct binary search trees possible with 3 distinct keys

 2×3C3 / 3+1
 6C3 / 4
 5
If three distinct keys are A, B and C, then 5 distinct binary search trees are-

 Binary Search Tree Construction-


Let us understand the construction of a binary search tree using the following example-
Example-
▫ Construct a Binary Search Tree (BST) for the following sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
▫ When elements are given in a sequence,
 Always consider the first element as the root node.
 Consider the given elements and insert them in the BST one by one.
▫ The binary search tree will be constructed as explained below-
1. Insert 50-

2. Insert 70-
As 70 > 50, so insert 70 to the right of 50.
3. Insert 60-
 As 60 > 50, so insert 60 to the right of 50.
 As 60 < 70, so insert 60 to the left of 70.

4. Insert 20-
 As 20 < 50, so insert 20 to the left of 50.

5. Insert 90-
 As 90 > 50, so insert 90 to the right of 50.
 As 90 > 70, so insert 90 to the right of 70.

6. Insert 10-
 As 10 < 50, so insert 10 to the left of 50.
 As 10 < 20, so insert 10 to the left of 20.
7. Insert 40-
 As 40 < 50, so insert 40 to the left of 50.
 As 40 > 20, so insert 40 to the right of 20.

8. Insert 100-
 As 100 > 50, so insert 100 to the right of 50.
 As 100 > 70, so insert 100 to the right of 70.
 As 100 > 90, so insert 100 to the right of 90.

 PRACTICE PROBLEMS BASED ON BINARY SEARCH TREES-


A. Problem-01:
 A binary search tree is generated by inserting in order of the following integers-
50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24

 The number of nodes in the left subtree and right subtree of the root respectively is _____.
a. (4, 7)
b. (7, 4)
c. (8, 3)
d. (3, 8)
Solution-

 Using the above discussed steps, we will construct the binary search tree.
 The resultant binary search tree will be-
 Clearly,
▫ Number of nodes in the left subtree of the root = 7
▫ Number of nodes in the right subtree of the root = 4
 Thus, Option (b) is correct.
B. Problem-02:
 How many distinct binary search trees can be constructed out of 4 distinct keys?
a. 5
b. 14
c. 24
d. 35
Solution-

 Number of distinct binary search trees possible with 4 distinct keys


 2nCn / n+1
 2×4C4 / 4+1
 8C4 / 5
 14
 Thus, Option (b) is correct.
C. Problem-03:
The numbers 1, 2, …, n are inserted in a binary search tree in some order. In the resulting tree, the right
subtree of the root contains p nodes. The first number to be inserted in the tree must be-
a. p
b. p+1
c. n-p
d. n-p+1
Solution-

 Let n = 4 and p = 3.
 Then, given options reduce to-
a. 3
b. 4
c. 1
d. 2
 Our binary search tree will be as shown-

 Clearly, first inserted number = 1.


 Thus, Option (C) is correct.
D. Problem-04:
We are given a set of n distinct elements and an unlabeled binary tree with n nodes. In how many ways can
we populate the tree with given set so that it becomes a binary search tree?
a. 0
b. 1
c. n!
d. C(2n, n) / n+1
Solution-
Option (B) is correct.

 BST Traversal-
 A binary search tree is traversed in exactly the same way a binary tree is traversed.
 In other words, BST traversal is same as binary tree traversal.
Example- Consider the following binary search tree-
Now, let us write the traversal sequences for this binary search tree-
1. Preorder Traversal- 100 , 20 , 10 , 30 , 200 , 150 , 300
2. Inorder Traversal- 10 , 20 , 30 , 100 , 150 , 200 , 300
3. Postorder Traversal- 10 , 30 , 20 , 150 , 300 , 200 , 100
 Important Notes-
Note-01: Inorder traversal of a binary search tree always yields all the nodes in increasing order.
Note-02: Unlike Binary Trees

 A binary search tree can be constructed using only preorder or only postorder traversal result.
 This is because inorder traversal can be obtained by sorting the given result in increasing order.

 PRACTICE PROBLEMS BASED ON BST TRAVERSAL-


Problem-01: Suppose the numbers 7 , 5 , 1 , 8 , 3 , 6 , 0 , 9 , 4 , 2 are inserted in that order into an initially
empty binary search tree. The binary search tree uses the usual ordering on natural numbers.

 What is the inorder traversal sequence of the resultant tree?


8. 7 , 5 , 1 , 0 , 3 , 2 , 4 , 6 , 8 , 9
9. 0 , 2 , 4 , 3 , 1 , 6 , 5 , 9 , 8 , 7
10. 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
11. 9 , 8 , 6 , 4 , 2 , 3 , 0 , 1 , 5 , 7
Solution- This given problem may be solved in the following two ways-
1. Method-01:
 We construct a binary search tree for the given elements.
 We write the inorder traversal sequence from the binary search tree so obtained.
 Following these steps, we have-
Thus, Option (C) is correct.
2. Method-02:
 We know, inorder traversal of a binary search tree always yields all the nodes in increasing order.
 Using this result,
 We arrange all the given elements in increasing order.
 Then, we report the sequence so obtained as inorder traversal sequence.
 Inorder Traversal : 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
Thus, Option (C) is correct.
Problem-02: The preorder traversal sequence of a binary search tree is- 30 , 20 , 10 , 15 , 25 , 23 , 39 , 35 ,
42
What one of the following is the postorder traversal sequence of the same tree?
12. 10 , 20 , 15 , 23 , 25 , 35 , 42 , 39 , 30
13. 15 , 10 , 25 , 23 , 20 , 42 , 35 , 39 , 30
14. 15 , 20 , 10 , 23 , 25 , 42 , 35 , 39 , 30
15. 15 , 10 , 23 , 25 , 20 , 35 , 42 , 39 , 30
Solution-
In this question,
 We are provided with the preorder traversal sequence.
 We write the inorder traversal sequence by arranging all the numbers in ascending order.
Then-
 Postorder Traversal : 30 , 20 , 10 , 15 , 25 , 23 , 39 , 35 , 42
 Inorder Traversal : 10 , 15 , 20 , 23 , 25 , 30 , 35 , 39 , 42
Now,
 We draw a binary search tree using these traversal results.
 The binary search tree so obtained is as shown-
Now, we write the postorder traversal sequence-
Postorder Traversal : 15 , 10 , 23 , 25, 20, 35, 42, 39, 30

 Binary Search Tree Operations-


Commonly performed operations on binary search tree are-

1. Search Operation-
▫ Search Operation is performed to search a particular element in the Binary Search Tree.
▫ Rules- For searching a given key in the BST,
 Compare the key with the value of root node.
 If the key is present at the root node, then return the root node.
 If the key is greater than the root node value, then recur for the root node’s right subtree.
 If the key is smaller than the root node value, then recur for the root node’s left subtree.
▫ Example-
Consider key = 45 has to be searched in the given BST-
 We start our search from the root node 25.
 As 45 > 25, so we search in 25’s right subtree.
 As 45 < 50, so we search in 50’s left subtree.
 As 45 > 35, so we search in 35’s right subtree.
 As 45 > 44, so we search in 44’s right subtree but 44 has no subtrees.
 So, we conclude that 45 is not present in the above BST.
2. Insertion Operation-
▫ Insertion Operation is performed to insert an element in the Binary Search Tree.
▫ Rules- The insertion of a new key always takes place as the child of some leaf node.
▫ For finding out the suitable leaf node,
 Search the key to be inserted from the root node till some leaf node is reached.
 Once a leaf node is reached, insert the key as child of that leaf node.
▫ Example-
Consider the following example where key = 40 is inserted in the given BST-

 We start searching for value 40 from the root node 100.


 As 40 < 100, so we search in 100’s left subtree.
 As 40 > 20, so we search in 20’s right subtree.
 As 40 > 30, so we add 40 to 30’s right subtree.
3. Deletion Operation-
▫ Deletion Operation is performed to delete a particular element from the Binary Search Tree.
▫ When it comes to deleting a node from the binary search tree, following three cases are possible-
▫ Case-01: Deletion Of A Node Having No Child (Leaf Node)- Just remove / disconnect the leaf node that
is to deleted from the tree.
Example- Consider the following example where node with value = 20 is deleted from the BST-

▫ Case-02: Deletion Of A Node Having Only One Child- Just make the child of the deleting node, the child
of its grandparent.
Example-
Consider the following example where node with value = 30 is deleted from the BST-

▫ Case-02: Deletion Of A Node Having Two Children- A node with two children may be deleted from the
BST in the following two ways-
Method-01:

 Visit to the right subtree of the deleting node.


 Pluck the least value element called as inorder successor.
 Replace the deleting element with its inorder successor.
Example-
Consider the following example where node with value = 15 is deleted from the BST-
Method-02:

 Visit to the left subtree of the deleting node.


 Pluck the greatest value element called as inorder predecessor.
 Replace the deleting element with its inorder predecessor.
Example-
Consider the following example where node with value = 15 is deleted from the BST-

 Time Complexity analysis:


 Best case: When we get the root node as the node which is supposed to be searched then in that case
we have to make onle one comparison so time taken would be constant. Time complexity in best case
would be O(1).
 Average case: When there is a balanced binary search tree(a binary search tree is called balanced if
height difference of nodes on left and right subtree is not more than one), so height becomes logN
where N is number of nodes in a tree.
In search operation we will keep on traversing through nodes one by one, suppose if we find the
element in second level so for doing so we have done 2 comparisons, if we get element in third level
we will be doing 3 comparisons so in this way we can say that the time taken to search for a key in
binary search tree is same as the height of the tree which is logN, so time complexity for searching is
O(logN) in average case.
Note: Average Height of a Binary Search Tree is 4.31107 ln(N) - 1.9531 lnln(N) + O(1) that is O(logN).
 Worst case: If the tree is unbalanced or if it is skewed binary search tree(skewed binary search tree is
a tree in which there are no nodes available in left subtree or right subtree see the image below to
get better understanding)

In this case we have to traverse from root to the deepest leaf node and in that case height of the tree
becomes n and as we have seen above time taken is same as the height of the tree so time
complexity in worst case becomes O(n).

 Space complexity: The space complexity of searching a node in a BST would be O(n) with 'n' being the
depth of the tree(number of nodes present in a tree) since at any point of time maximum number of
stack frames that could be present in memory is 'n'.

Balanced search tree:


 Height Balancing:
A height-balanced binary tree is defined as a binary tree in which the height of the left and the right subtree
of any node differ by not more than 1. AVL tree, red-black tree are examples of height-balanced trees.
 Conditions for Height-Balanced Binary Tree:
Following are the conditions for a height-balanced binary tree:

 The difference between the heights of the left and the right subtree for any node is not more than
one.
 The left subtree is balanced.
 The right subtree is balanced.
Note: An empty tree is also height-balanced.

 What is the Height Balance of a node?


To check if the binary tree is height-balanced or not, you have to check the height balance of each node. For
this, you need to calculate the heights of the two subtrees for each node making this impractical. Instead, we
store the height balance information of every subtree in the root node of that subtree. Thus, each node not
only maintains its data and children’s information but also a height balance value.

 The height balance of a node is calculated as follows:


height balance of node = height of right subtree – height of left subtree

 The above formula means that:


▫ If the right subtree is taller, the height balance of the node will be positive.
▫ If the left subtree is taller, the balance of the node will be negative.
 Why do we need a Height-Balanced Binary Tree?
Let’s understand the need for a balanced binary tree through an example.

 The above tree is a binary search tree and also a height-balanced tree.
 Suppose we want to want to find the value 79 in the above tree. First, we compare the value of the
root node. Since the value of 79 is greater than 35, we move to its right child, i.e., 48. Since the value
79 is greater than 48, so we move to the right child of 48.
 The value of the right child of node 48 is 79. The number of hops required to search the element 79 is
2.
 Similarly, any element can be found with at most 2 jumps because the height of the tree is 2.
 So it can be seen that any value in a balanced binary tree can be searched in O(logN) time where N is
the number of nodes in the tree. But if the tree is not height-balanced then in the worst case, a
search operation can take O(N) time.

 Applications of Height-Balanced Binary Tree:


 Balanced trees are mostly used for in-memory sorts of sets and dictionaries.
 Balanced trees are also used extensively in database applications in which insertions and deletions
are fewer but there are frequent lookups for data required.
 It is used in applications that require improved searching apart from database applications.
 It has applications in storyline games as well.
 It is used mainly in corporate sectors where they have to keep the information about the employees
working there and their change in shifts.

 Advantages of Height-Balanced Binary Tree:


 It will improve the worst-case lookup time at the expense of making a typical case roughly one
lookup less.
 As a general rule, a height-balanced tree would work better when the request frequencies across the
data set are more evenly spread,
 It gives better search time complexity.

 Disadvantages of Height-Balanced Binary Tree:


 Longer running times for the insert and remove operations.
 Must keep balancing info in each node.
 To find nodes to balance, must go back up in the tree.

 How to check if a given tree is height-balanced:


You can check if a tree is height-balanced using recursion based on the idea that every subtree of the tree
will also be height-balanced. To check if a tree is height-balanced perform the following operations:

 Use recursion and visit the left subtree and right subtree of each node:
o Check the height of the left subtree and right subtree.
o If the absolute difference between their heights is at most 1 then that node is height-balanced.
o Otherwise, that node and the whole tree is not balanced.

 AVL Tree-
 AVL trees are special kind of binary search trees.
 In AVL trees, height of left subtree and right subtree of every node differs by at most one.
 AVL trees are also called as self-balancing binary search trees.
 Example- Following tree is an example of AVL tree-

 This tree is an AVL tree because-


▫ It is a binary search tree.
▫ The difference between height of left subtree and right subtree of every node is at most one.
 Following tree is not an example of AVL Tree-

 This tree is not an AVL tree because-


▫ The difference between height of left subtree and right subtree of root node = 4 – 2 = 2.
▫ This difference is greater than one.

 Balance Factor-
 Balance factor is defined for every node.
 Balance factor of a node = Height of its left subtree – Height of its right subtree
 Balance factor of every node is either 0 or 1 or –1

 Kinds of Rotations-
There are 4 kinds of rotations possible in AVL Trees-

1. LL Rotation:
2. RR Rotation:

3. LR Rotation:
4. RL Rotation:

 AVL Tree Operations-


Like BST Operations, commonly performed operations on AVL tree are-
1. Search Operation
2. Insertion Operation
3. Deletion Operation.
1. Insertion in AVL Tree-
 Insertion Operation is performed to insert an element in the AVL Tree.
 To insert an element in the AVL tree, follow the following steps-
▫ Insert the element in the AVL tree in the same way the insertion is performed in BST.
▫ After insertion, check the balance factor of each node of the resulting tree.
 Now, following two cases are possible-
▫ Case-01:
 After the insertion, the balance factor of each node is either 0 or 1 or -1.
 In this case, the tree is considered to be balanced.
 Conclude the operation.
 Insert the next element if any.
▫ Case-02:
 After the insertion, the balance factor of at least one node is not 0 or 1 or -1.
 In this case, the tree is considered to be imbalanced.
 Perform the suitable rotation to balance the tree.
 After the tree is balanced, insert the next element if any.
 Rules To Remember-
a. Rule-01: After inserting an element in the existing AVL tree, Balance factor of only those nodes
will be affected that lies on the path from the newly inserted node to the root node.
b. Rule-02: To check whether the AVL tree is still balanced or not after the insertion, There is no
need to check the balance factor of every node. Check the balance factor of only those nodes that
lies on the path from the newly inserted node to the root node.
c. Rule-03: After inserting an element in the AVL tree, If tree becomes imbalanced, then there
exists one particular node in the tree by balancing which the entire tree becomes balanced
automatically. To re balance the tree, balance that particular node.
 To find that particular node,
▫ Traverse the path from the newly inserted node to the root node.
▫ Check the balance factor of each node that is encountered while traversing the path.
▫ The first encountered imbalanced node will be the node that needs to be balanced.
 To balance that node
▫ Count three nodes in the direction of leaf node.
▫ Then, use the concept of AVL tree rotations to re balance the tree.
 PRACTICE PROBLEM BASED ON AVL TREE INSERTION-
▫ Problem- Construct AVL Tree for the following sequence of numbers- 50 , 20 , 60 , 10 , 8 , 15 , 32
, 46 , 11 , 48

▫ Solution-
1. Step-01: Insert 50

2. Step-02: Insert 20
As 20 < 50, so insert 20 in 50’s left sub tree.

3. Step-03: Insert 60
As 60 > 50, so insert 60 in 50’s right sub tree.
4. Step-04: Insert 10
As 10 < 50, so insert 10 in 50’s left sub tree.
As 10 < 20, so insert 10 in 20’s left sub tree.

5. Step-05: Insert 8

 As 8 < 50, so insert 8 in 50’s left sub tree.


 As 8 < 20, so insert 8 in 20’s left sub tree.
 As 8 < 10, so insert 8 in 10’s left sub tree.

▫ To balance the tree,


 Find the first imbalanced node on the path from the newly inserted node (node 8) to the root
node.
 The first imbalanced node is node 20.
 Now, count three nodes from node 20 in the direction of leaf node.
 Then, use AVL tree rotation to balance the tree.
 Following this, we have-
6. Step-06: Insert 15

 As 15 < 50, so insert 15 in 50’s left sub tree.


 As 15 > 10, so insert 15 in 10’s right sub tree.
 As 15 < 20, so insert 15 in 20’s left sub tree.

▫ To balance the tree,


 Find the first imbalanced node on the path from the newly inserted node (node 15) to the root
node.
 The first imbalanced node is node 50.
 Now, count three nodes from node 50 in the direction of leaf node.
 Then, use AVL tree rotation to balance the tree.
 Following this, we have-
7. Step-07: Insert 32
 As 32 > 20, so insert 32 in 20’s right sub tree.
 As 32 < 50, so insert 32 in 50’s left sub tree.

8. Step-08: Insert 46
 As 46 > 20, so insert 46 in 20’s right sub tree.
 As 46 < 50, so insert 46 in 50’s left sub tree.
 As 46 > 32, so insert 46 in 32’s right sub tree.

9. Step-09: Insert 11
 As 11 < 20, so insert 11 in 20’s left sub tree.
 As 11 > 10, so insert 11 in 10’s right sub tree.
 As 11 < 15, so insert 11 in 15’s left sub tree.

10. Step-10: Insert 48

 As 48 > 20, so insert 48 in 20’s right sub tree.


 As 48 < 50, so insert 48 in 50’s left sub tree.
 As 48 > 32, so insert 48 in 32’s right sub tree.
 As 48 > 46, so insert 48 in 46’s right sub tree.

▫ To balance the tree,


 Find the first imbalanced node on the path from the newly inserted node (node 48) to the root node.
 The first imbalanced node is node 32.
 Now, count three nodes from node 32 in the direction of leaf node.
 Then, use AVL tree rotation to balance the tree.
 Following this, we have-
This is the final balanced AVL tree after inserting all the given elements.

 Deletion in AVL Tree


 Deleting a node from an AVL tree is similar to that in a binary search tree. Deletion may disturb the
balance factor of an AVL tree and therefore the tree needs to be rebalanced in order to maintain the
AVLness. For this purpose, we need to perform rotations. The two types of rotations are L rotation and
R rotation. Here, we will discuss R rotations. L rotations are the mirror images of them.
 If the node which is to be deleted is present in the left sub-tree of the critical node, then L rotation
needs to be applied else if, the node which is to be deleted is present in the right sub-tree of the critical
node, the R rotation will be applied.
 Let us consider that, A is the critical node and B is the root node of its left sub-tree. If node X, present
in the right sub-tree of A, is to be deleted, then there can be three different situations:

 R0 rotation (Node B has balance factor 0 ):


▫ If the node B has 0 balance factor, and the balance factor of node A disturbed upon deleting the
node X, then the tree will be rebalanced by rotating tree using R0 rotation.
▫ The critical node A is moved to its right and the node B becomes the root of the tree with T1 as its
left sub-tree. The sub-trees T2 and T3 becomes the left and right sub-tree of the node A. the process
involved in R0 rotation is shown in the following image.

▫ Example: Delete the node 30 from the AVL tree shown in the following image.
▫ Solution: In this case, the node B has balance factor 0, therefore the tree will be rotated by using
R0 rotation as shown in the following image. The node B(10) becomes the root, while the node A
is moved to its right. The right child of node B will now become the left child of node A.

 R1 Rotation (Node B has balance factor 1)


▫ R1 Rotation is to be performed if the balance factor of Node B is 1. In R1 rotation, the critical node A is
moved to its right having sub-trees T2 and T3 as its left and right child respectively. T1 is to be placed
as the left sub-tree of the node B.
▫ The process involved in R1 rotation is shown in the following image.
▫ Example: Delete Node 55 from the AVL tree shown in the following image

▫ Solution : Deleting 55 from the AVL Tree disturbs the balance factor of the node 50 i.e. node A
which becomes the critical node. This is the condition of R1 rotation in which, the node A will be
moved to its right (shown in the image below). The right of B is now become the left of A (i.e. 45).
The process involved in the solution is shown in the following image.

 R-1 Rotation (Node B has balance factor -1)


▫ R-1 rotation is to be performed if the node B has balance factor -1. This case is treated in the same way
as LR rotation. In this case, the node C, which is the right child of node B, becomes the root node of
the tree with B and A as its left and right children respectively.
▫ The sub-trees T1, T2 becomes the left and right sub-trees of B whereas, T3, T4 become the left and
right sub-trees of A.
▫ The process involved in R-1 rotation is shown in the following image.
▫ Example: Delete the node 60 from the AVL tree shown in the following image.

▫ Solution: in this case, node B has balance factor -1. Deleting the node 60, disturbs the balance factor
of the node 50 therefore, it needs to be R-1 rotated. The node C i.e. 45 becomes the root of the tree
with the node B(40) and A(50) as its left and right child.
 Hashing in Data Structure
 Hashing is a well-known technique to search any particular element among several elements.
 It minimizes the number of comparisons while performing the search.

 Advantage-
 Hashing is extremely efficient.
 The time taken by it to perform the search does not depend upon the total number of elements.
 It completes the search with constant time complexity O(1).

 Hashing Mechanism-
 An array data structure called as Hash table is used to store the data items.
 Based on the hash key value, data items are inserted into the hash table.

 Hash Key Value-


 Hash key value is a special value that serves as an index for a data item.
 It indicates where the data item should be be stored in the hash table.
 Hash key value is generated using a hash function.
 Hash Function-
Hash function is a function that maps any big number or string to a small integer value

 Hash function takes the data item as an input and returns a small integer value as an output.
 The small integer value is called as a hash value.
 Hash value of the data item is then used as an index for storing it into the hash table.

 Types of Hash Functions-


There are various types of hash functions available such as-
1. Mid Square Hash Function
2. Division Hash Function
3. Folding Hash Function
It depends on the user which hash function he wants to use.

 Properties of Hash Function-


The properties of a good hash function are-

 It is efficiently computable.
 It minimizes the number of collisions.
 It distributes the keys uniformly over the table.

 Collision in Hashing-
In hashing,

 Hash function is used to compute the hash value for a key.


 Hash value is then used as an index to store the key in the hash table.
 Hash function may return the same hash value for two or more keys.
 When the hash value of a key maps to an already occupied bucket of the hash table, it is called as
a Collision.

 Collision Resolution Techniques-


 Collision Resolution Techniques are the techniques used for resolving or handling the collision.
 Collision resolution techniques are classified as-
1. Separate Chaining-
 This technique creates a linked list to the slot for which collision occurs.
 The new key is then inserted in the linked list.
 These linked lists to the slots appear like chains.
 That is why, this technique is called as separate chaining.
 Time Complexity-
o For Searching-
 In worst case, all the keys might map to the same bucket of the hash table.
 In such a case, all the keys will be present in a single linked list.
 Sequential search will have to be performed on the linked list to perform the search.
 So, time taken for searching in worst case is O(n).
o For Deletion-
 In worst case, the key might have to be searched first and then deleted.
 In worst case, time taken for searching is O(n).
 So, time taken for deletion in worst case is O(n).
 Load Factor (α)-
Load factor (α) is defined as-

If Load factor (α) = constant, then time complexity of Insert, Search, Delete = Θ(1)
 PRACTICE PROBLEM BASED ON SEPARATE CHAINING-
Problem- Using the hash function ‘key mod 7’, insert the following sequence of keys in the hash table- 50,
700, 76, 85, 92, 73 and 101. Use separate chaining technique for collision resolution.
Solution- The given sequence of keys will be inserted in the hash table as-
1. Step-01:
 Draw an empty hash table.
 For the given hash function, the possible range of hash values is [0, 6].
 So, draw an empty hash table consisting of 7 buckets as-

2. Step-02:
 Insert the given keys in the hash table one by one.
 The first key to be inserted in the hash table = 50.
 Bucket of the hash table to which key 50 maps = 50 mod 7 = 1.
 So, key 50 will be inserted in bucket-1 of the hash table as-

3. Step-03:
 The next key to be inserted in the hash table = 700.
 Bucket of the hash table to which key 700 maps = 700 mod 7 = 0.
 So, key 700 will be inserted in bucket-0 of the hash table as-

4. Step-04:
 The next key to be inserted in the hash table = 76.
 Bucket of the hash table to which key 76 maps = 76 mod 7 = 6.
 So, key 76 will be inserted in bucket-6 of the hash table as-
5. Step-05:
 The next key to be inserted in the hash table = 85.
 Bucket of the hash table to which key 85 maps = 85 mod 7 = 1.
 Since bucket-1 is already occupied, so collision occurs.
 Separate chaining handles the collision by creating a linked list to bucket-1.
 So, key 85 will be inserted in bucket-1 of the hash table as-

6. Step-06:
 The next key to be inserted in the hash table = 92.
 Bucket of the hash table to which key 92 maps = 92 mod 7 = 1.
 Since bucket-1 is already occupied, so collision occurs.
 Separate chaining handles the collision by creating a linked list to bucket-1.
 So, key 92 will be inserted in bucket-1 of the hash table as-
7. Step-07:
 The next key to be inserted in the hash table = 73.
 Bucket of the hash table to which key 73 maps = 73 mod 7 = 3.
 So, key 73 will be inserted in bucket-3 of the hash table as-

8. Step-08:
 The next key to be inserted in the hash table = 101.
 Bucket of the hash table to which key 101 maps = 101 mod 7 = 3.
 Since bucket-3 is already occupied, so collision occurs.
 Separate chaining handles the collision by creating a linked list to bucket-3.
 So, key 101 will be inserted in bucket-3 of the hash table as-

2. Open Addressing-
 Unlike separate chaining, all the keys are stored inside the hash table.
 No key is stored outside the hash table.
 Techniques used for open addressing are-
1. Linear Probing
2. Quadratic Probing
3. Double Hashing
 Operations in Open Addressing-
▫ Let us discuss how operations are performed in open addressing-
▫ Insert Operation-
 Hash function is used to compute the hash value for a key to be inserted
 Hash value is then used as an index to store the key in the hash table.
▫ In case of collision,
 Probing is performed until an empty bucket is found.
 Once an empty bucket is found, the key is inserted.
 Probing is performed in accordance with the technique used for open addressing.
▫ Search Operation-
To search any particular key,

 Its hash value is obtained using the hash function used.


 Using the hash value, that bucket of the hash table is checked.
 If the required key is found, the key is searched.
 Otherwise, the subsequent buckets are checked until the required key or an empty bucket is
found.
 The empty bucket indicates that the key is not present in the hash table.
▫ Delete Operation-
 The key is first searched and then deleted.
 After deleting the key, that particular bucket is marked as “deleted”.
▫ NOTE-
 During insertion, the buckets marked as “deleted” are treated like any other empty bucket.
 During searching, the search is not terminated on encountering the bucket marked as “deleted”.
 The search terminates only after the required key or an empty bucket is found.
 Open Addressing Techniques-
Techniques used for open addressing are-
1. Linear Probing-
 When collision occurs, we linearly probe for the next bucket.
 We keep probing until an empty bucket is found.
 Advantage-
 It is easy to compute.

 Disadvantage-
 The main problem with linear probing is clustering.
 Many consecutive elements form groups.
 Then, it takes time to search an element or to find an empty bucket.

 Time Complexity-
 Worst time to search an element in linear probing is O (table size).
 This is because-
 Even if there is only one element present and all other elements are deleted.
 Then, “deleted” markers present in the hash table makes search the entire table.
2. Quadratic Probing-
 When collision occurs, we probe for i2‘th bucket in ith iteration.
 We keep probing until an empty bucket is found.
3. Double Hashing-
 We use another hash function hash2(x) and look for i * hash2(x) bucket in i th iteration.
 It requires more computation time as two hash functions need to be computed.
 Comparison of Open Addressing Techniques-

Linear Probing Quadratic Probing Double Hashing


Primary Clustering Yes No No
Secondary Yes Yes No
Clustering
Number of Probe m m m2
Sequence
(m = size of table)
Cache performance Best Lies between the Poor
two
 Conclusions-
▫ Linear Probing has the best cache performance but suffers from clustering.
▫ Quadratic probing lies between the two in terms of cache performance and clustering.
▫ Double caching has poor cache performance but no clustering.
 Load Factor (α)-
▫ Load factor (α) is defined as-

▫ In open addressing, the value of load factor always lie between 0 and 1.
▫ This is because-
 In open addressing, all the keys are stored inside the hash table.
 So, size of the table is always greater or at least equal to the number of keys stored in the table.
 PRACTICE PROBLEM BASED ON OPEN ADDRESSING-
Problem- Using the hash function ‘key mod 7’, insert the following sequence of keys in the hash table- 50,
700, 76, 85, 92, 73 and 101. Use linear probing technique for collision resolution.
Solution- The given sequence of keys will be inserted in the hash table as-
1. Step-01:
 Draw an empty hash table.
 For the given hash function, the possible range of hash values is [0, 6].
 So, draw an empty hash table consisting of 7 buckets as-
2. Step-02:
 Insert the given keys in the hash table one by one.
 The first key to be inserted in the hash table = 50.
 Bucket of the hash table to which key 50 maps = 50 mod 7 = 1.
 So, key 50 will be inserted in bucket-1 of the hash table as-

3. Step-03:
 The next key to be inserted in the hash table = 700.
 Bucket of the hash table to which key 700 maps = 700 mod 7 = 0.
 So, key 700 will be inserted in bucket-0 of the hash table as-

4. Step-04:
 The next key to be inserted in the hash table = 76.
 Bucket of the hash table to which key 76 maps = 76 mod 7 = 6.
 So, key 76 will be inserted in bucket-6 of the hash table as-
5. Step-05:
 The next key to be inserted in the hash table = 85.
 Bucket of the hash table to which key 85 maps = 85 mod 7 = 1.
 Since bucket-1 is already occupied, so collision occurs.
 To handle the collision, linear probing technique keeps probing linearly until an empty bucket is
found.
 The first empty bucket is bucket-2.
 So, key 85 will be inserted in bucket-2 of the hash table as-

6. Step-06:
 The next key to be inserted in the hash table = 92.
 Bucket of the hash table to which key 92 maps = 92 mod 7 = 1.
 Since bucket-1 is already occupied, so collision occurs.
 To handle the collision, linear probing technique keeps probing linearly until an empty bucket is
found.
 The first empty bucket is bucket-3.
 So, key 92 will be inserted in bucket-3 of the hash table as-
7. Step-07:
 The next key to be inserted in the hash table = 73.
 Bucket of the hash table to which key 73 maps = 73 mod 7 = 3.
 Since bucket-3 is already occupied, so collision occurs.
 To handle the collision, linear probing technique keeps probing linearly until an empty bucket is
found.
 The first empty bucket is bucket-4.
 So, key 73 will be inserted in bucket-4 of the hash table as-

8. Step-08:
 The next key to be inserted in the hash table = 101.
 Bucket of the hash table to which key 101 maps = 101 mod 7 = 3.
 Since bucket-3 is already occupied, so collision occurs.
 To handle the collision, linear probing technique keeps probing linearly until an empty bucket is
found.
 The first empty bucket is bucket-5.
 So, key 101 will be inserted in bucket-5 of the hash table as-
 Data structure for symbol table
 A compiler contains two type of symbol table: global symbol table and scope symbol table.
 Global symbol table can be accessed by all the procedures and scope symbol table.
 The scope of a name and symbol table is arranged in the hierarchy structure as shown below:
1. int value=10;
2.
3. void sum_num()
4. {
5. int num_1;
6. int num_2;
7.
8. {
9. int num_3;
10. int num_4;
11. }
12.
13. int num_5;
14.
15. {
16. int_num 6;
17. int_num 7;
18. }
19. }
20.
21. Void sum_id
22. {
23. int id_1;
24. int id_2;
25.
26. {
27. int id_3;
28. int id_4;
29. }
30.
31. int num_5;
32. }
 The above grammar can be represented in a hierarchical data structure of symbol tables:

 The global symbol table contains one global variable and two procedure names. The name mentioned in
the sum_num table is not available for sum_id and its child tables.
 Data structure hierarchy of symbol table is stored in the semantic analyzer. If you want to search the name
in the symbol table then you can search it using the following algorithm:
▫ First a symbol is searched in the current symbol table.
▫ If the name is found then search is completed else the name will be searched in the symbol table of
parent until,
▫ The name is found or global symbol is searched.

 QUESTION ANF ANSWER:


MCQ:
1. Where is linear searching used?
a) When the list has only a few elements
b) When performing a single search in an unordered list
c) Used all the time
d) When the list has only a few elements and When performing a single search in an unordered list
2. What is the best case for linear search?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(1)
3. What is the worst case for linear search?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(1)
4. Which of the following is a disadvantage of linear search?
a) Requires more space
b) Greater time complexities compared to other searching algorithms
c) Not easy to understand
d) Not easy to implement
5. What is the advantage of recursive approach than an iterative approach?
a) Consumes less memory
b) Less code and easy to implement
c) Consumes more memory
d) More code has to be written
6. Given an input arr = {2,5,7,99,899}; key = 899; What is the level of recursion?
a) 5
b) 2
c) 3
d) 4
7. Which of the following is not an application of binary search?
a) To find the lower/upper bound in an ordered sequence
b) Union of intervals
c) Debugging
d) To search in unordered list
8. Binary Search can be categorized into which of the following?
a) Brute Force technique
b) Divide and conquer
c) Greedy algorithm
d) Dynamic programming
9. Binary Search can be categorized into which of the following?
a) Brute Force technique
b) Divide and conquer
c) Greedy algorithm
d) Dynamic programming
10. What is the speciality about the inorder traversal of a binary search tree?
a) It traverses in a non increasing order
b) It traverses in an increasing order
c) It traverses in a random fashion
d) It traverses based on priority of the node
11. What does the following piece of code do?
public void func(Tree root)
{
func(root.left());
func(root.right());
System.out.println(root.data());
}
a) Preorder traversal
b) Inorder traversal
c) Postorder traversal
d) Level order traversal
12. What is an AVL tree?
a) a tree which is balanced and is a height balanced tree
b) a tree which is unbalanced and is a height balanced tree
c) a tree with three children
d) a tree with atmost 3 children
13. Why we need to a binary tree which is height balanced?
a) to avoid formation of skew trees
b) to save memory
c) to attain faster memory access
d) to simplify storing
14. Which of the below diagram is following AVL tree property?

I.

ii.
a) only i
b) only i and ii
c) only ii
d) i is not a binary search tree
15. What is the maximum height of an AVL tree with p nodes?
a) p
b) log(p)
c) log(p)/2
d) p⁄2

SAQ:
1. Construct a binary search tree with the below information.
The preorder traversal of a binary search tree 10, 4, 3, 5, 11, 12.

2. Write a code to find the maximum element in a binary search tree?

 public void max(Tree root)


{
while(root.right() != null)
{
root = root.right();
}
System.out.println(root.data());
}
3. Write a code to find the minimum element in a binary search tree?

 public void min(Tree root)


{
while(root.left() != null)
{
root = root.left();
}
System.out.println(root.data());
}
4. Write a pseudo code to check if a binary search tree is an AVL tree?

 int avl(binarysearchtree root):


if(not root)
return 0
left_tree_height = avl(left_of_root)

if(left_tree_height== -1)
return left_tree_height

right_tree_height= avl(right_of_root)
if(right_tree_height==-1)
return right_tree_height
5. Write a code which uses recursion for linear search.

 public void linSearch(int[] arr, int first, int last, int key)
{
if(first == last)
{
System.out.print("-1");
}
else
{
if(arr[first] == key)
{
System.out.print(first);
}
else
{
linSearch(arr, first+1, last, key);
}
}
}
6. Write a code that does binary search using recursion.

 public static int recursive(int arr[], int low, int high, int key)
{
int mid = low + (high - low)/2;
if(arr[mid] == key)
{
return mid;
}
else if(arr[mid] < key)
{
return recursive(arr,mid+1,high,key);
}
else
{
return recursive(arr,low,mid-1,key);
}
}

7. What is linear search?


 In Linear Search the list is searched sequentially and the position is returned if the key element to be
searched is available in the list, otherwise -1 is returned. The search in Linear Search starts at the
beginning of an array and move to the end, testing for a match at each item.

8. What is binary search?

 Binary search is simpler and faster than linear search. Binary search the array to be searched is divided
into two parts, one of which is ignored as it will not contain the required element
 One essential condition for the binary search is that the array which is to be searched, should be
arranged in order.

9.Define hashing?

 Search from that position for an empty location


 Use a second hash function.
 Use that array location as the header of a linked list of values that hash to this location

10. Define hash table?

 All the large collection of data are stored in a hash table. The size of the hash table is usually fixed and
it is bigger than the number of elements we are going to store.
 The load factor defines the ration of the number of data to be stored to the size of the hash table

11. What are the types of hashing?

 Static hashing-In static hashing the process is carried out without the usage of an index structure.
 Dynamic hashing- It allows dynamic allocation of buckets, i.e. according to the demand of database the
buckets can be allocated making this approach more efficient.

12. Define Rehashing?

 Rehashing is technique also called as double hashing used in hash tables to resolve hash collisions,
cases when two different values to be searched for produce the same hash key.
 It is a popular collision-resolution technique in open-addressed hash tables.

BROAD:
1. Using both linear search and binary search from the following data:
a. 13, 4, 6, 10, 8, 9, 7, 5, 1, 3 – search(10)
b. 50, 40, 10, 30, 38, 92, 46, 32, 87, 24, 50 – search(32)
c. 17, 30, 27, 1, 7, 54, 67, 45, 72, 83 – search(83)
2. Create binary search tree for the following values:
a. 13, 4, 6, 10, 8, 9, 7, 5, 1, 3
b. 50, 40, 10, 30, 38, 92, 46, 32, 87, 24, 50
c. 17, 30, 27, 1, 7, 54, 67, 45, 72, 83
3. Create AVL tree for the following values and if the tree is unbalanced then balanced it:
a. 13, 4, 6, 10, 8, 9, 7, 5, 1, 3. Then delete (7), delete(3), insert(2)
b. 50, 40, 10, 30, 38, 92, 46, 32, 87, 24. Then delete(24), insert(11)
c. 17, 30, 27, 1, 7, 54, 67, 45, 72, 83. Then delete(1), delete(72), insert(17)

SEMESTER QUESTIONS FROM THIS UNIT:

1. Mention the different types of searching techniques in C.


2. Which is the best searching algorithm and why?
3. Write and explain recursive Binary Search Algorithm.
4. State the conditions when Linear search is considered.
5. Write an algorithm for Binary search.Explain why the complexity of Binary search is
O(log2n).
6. What is Hashing ?Briefly discuss the different Collision resolution techniques.

Answer:
1. Different Types of Searching Techniques in C

In C, several searching algorithms can be implemented, each with its specific use cases. The most common
ones are:

 Linear Search: Sequentially checks each element of the list until the desired element is found or the list ends.
 Binary Search: Efficiently searches a sorted list by repeatedly dividing the search interval in half.
 Jump Search: A combination of Linear and Binary Search, where the array is divided into blocks, and Linear
Search is performed within a block.
 Interpolation Search: Similar to Binary Search, but instead of dividing the list in half, it estimates the position
of the target value based on the values of the endpoints.
 Exponential Search: Useful for unbounded or infinite lists, it first finds a range where the element could be
and then applies Binary Search within that range.
 Fibonacci Search: Similar to Binary Search but uses Fibonacci numbers to divide the list.
 Hashing: Searches for an element by transforming the search key into an index in an array using a hash
function.
2. Which is the Best Searching Algorithm and Why?

The best searching algorithm depends on the context:

 For Unsorted Data: Linear Search is the simplest option but not the most efficient for large datasets.
 For Sorted Data: Binary Search is typically the best general-purpose algorithm due to its O(log n) time
complexity, making it highly efficient for large sorted datasets.
 For Constant Time Search: Hashing is the most efficient, with an average time complexity of O(1) for search
operations, assuming a good hash function and proper collision handling.

Binary Search is often considered the best for sorted datasets because of its logarithmic time complexity,
which is significantly faster than Linear Search for large datasets.

3. Recursive Binary Search Algorithm

Here is the recursive implementation of the Binary Search algorithm in C:

int binarySearch(int arr[], int left, int right, int x) {


if (right >= left) {
int mid = left + (right - left) / 2;

// Check if the middle element is the target


if (arr[mid] == x)
return mid;

// If the target is smaller than the mid, it must be in the left subarray
if (arr[mid] > x)
return binarySearch(arr, left, mid - 1, x);

// Else the target is in the right subarray


return binarySearch(arr, mid + 1, right, x);
}

// If the element is not present in the array


return -1;
}

Explanation:

 The function binarySearch is called recursively with updated left and right pointers until the target
element x is found or the subarray has no more elements to check.
 The middle index mid is calculated, and if the middle element is the target, the index is returned.
 If the target is smaller than the middle element, the search continues in the left subarray.
 If the target is larger, the search continues in the right subarray.

4. Conditions When Linear Search is Considered

Linear Search is considered in the following scenarios:

 Unsorted Data: When the list is not sorted, Linear Search is the simplest and most straightforward approach.
 Small Datasets: For small datasets, the overhead of more complex algorithms like Binary Search might not be
justified, making Linear Search a practical choice.
 Single Pass Requirement: When only a single pass through the data is possible or when the data is being
streamed, Linear Search is often the only viable option.
 Data Structures with Non-Contiguous Memory: In data structures like linked lists, where random access is not
possible, Linear Search is typically used.

5. Algorithm for Binary Search and Explanation of Complexity

Binary Search Algorithm:

1. Initialize: Set left = 0 and right = n - 1 where n is the number of elements in the array.
2. Repeat:
o Calculate the middle index: mid = left + (right - left) / 2.
o If arr[mid] == x, return mid.
o If arr[mid] > x, update right = mid - 1.
o If arr[mid] < x, update left = mid + 1.
3. End Condition: If left > right, the element is not in the array, return -1.

Explanation of Time Complexity:

 O(log2n): At each step, Binary Search reduces the search space by half. If you start with n elements, the
number of elements left to search after each step is n/2, then n/4, then n/8, and so on. The process
continues until the search space is reduced to 1 element.
o The number of steps required to reduce n to 1 is log2(n), hence the time complexity is O(log n).

7. What is Hashing? Discuss Different Collision Resolution Techniques.

Hashing is a process in computer science used to map data (usually in the form of keys) to a fixed-size array
or hash table using a hash function. The purpose of hashing is to enable fast data retrieval, making search,
insert, and delete operations efficient, typically achieving O(1) average time complexity.

How Hashing Works:

1. Hash Function: A hash function takes an input (the key) and returns an index in the array where the value
associated with that key should be stored.
o Example: index = hash(key) % table_size
2. Hash Table: The array where the data is stored based on the index generated by the hash function.
3. Collisions: A collision occurs when two keys produce the same index in the hash table.

Collision Resolution Techniques:

Since collisions are inevitable due to the limited size of the hash table and the possibility of different keys
producing the same hash value, various techniques are used to resolve them:

1. Chaining:

 Description: In chaining, each slot of the hash table points to a linked list (or another data structure) of
elements that hash to the same index. When a collision occurs, the new key-value pair is added to the
linked list at the appropriate index.
 Advantages:
o Simple to implement.
o Efficient if the hash function distributes keys uniformly.
o Handles a large number of collisions gracefully.
 Disadvantages:
o Requires additional memory for the pointers in the linked lists.
o Performance degrades if too many elements hash to the same index, leading to longer linked lists.
 Example:
o Hash table index 3 contains elements 15, 25, 35 (all of which produce the same index) stored in a
linked list.

2. Open Addressing:

 Description: In open addressing, all elements are stored directly in the hash table itself. When a
collision occurs, the algorithm searches for the next available slot in the table to place the element.
There are several methods to find this next slot:
o Linear Probing:
 If a collision occurs at index i, the algorithm checks i+1, i+2, ... until an empty slot is
found.
 Advantages: Simple to implement.
 Disadvantages: Clustering can occur, where consecutive elements lead to more collisions,
reducing efficiency.
o Quadratic Probing:
 Instead of checking the next consecutive slot, quadratic probing checks i+1^2, i+2^2, ...
until an empty slot is found.
 Advantages: Reduces clustering compared to linear probing.
 Disadvantages: Can still lead to secondary clustering.
o Double Hashing:
 Uses a second hash function to calculate the step size for probing after a collision. For
example, if a collision occurs at index i, the next index to check would be i + step_size,
where step_size = hash2(key).
 Advantages: Reduces clustering more effectively.
 Disadvantages: Requires careful choice of the second hash function to ensure uniform
distribution.

3. Rehashing:

 Description: When a collision occurs and the hash table becomes too full, a new, larger hash table is created,
and all existing elements are hashed again into the new table.
 Advantages: Helps in maintaining efficient search operations as the load factor (number of elements/table
size) increases.
 Disadvantages: Rehashing can be time-consuming and computationally expensive.

You might also like