Trees: Binary Search (BST), Insertion and Deletion in BST: Unit V
Trees: Binary Search (BST), Insertion and Deletion in BST: Unit V
Trees: Binary Search (BST), Insertion and Deletion in BST: Unit V
If you observe the Fig. carefully, you will find that the left value < parent value < right
value is followed throughout the tree. Various operations that can be performed on
binary search tree are:
Figure Binary search tree
1. Insertion of a node in a binary tree
2. Deletion of element from the binary search tree.
3. Searching of an element in the binary tree.
4. Display of binary tree.
Algorithm:
1. Read the value for the node which is to be created, and store it in a node called
New.
2. Initially if (root==NULL) then root=New.
3. Again read the next value of node created in New.
For deletion of any node from binary search tree there are three cases which are
possible.
i. Deletion of leaf node.
ii. Deletion of a node having one child.
iii. Deletion of a node having two children.
ii. Deletion of a node having one child - To explain this kind of deletion, consider
a tree as given below.
iii. The node having two children - Again, let us take some example for discussing
this kind of deletion. Let us consider that we want to delete node having value 7. We
will then find out the inorder
successor of node 7. The inorder
successor will be simply copied at
location of node 7. That means copy
8 at the position where value of node
is 7. Set left pointer of 9 as NULL.
This completes the deletion
procedure.
Algorithm:
1. Read the value for the node which is to be deleted, let delete node is x.
2. Find the node to be deleted and check the condition
2.1. If delete node (x) is leaf node, simply delete it and place NULL.
In searching, the node which we want to search is called a key node. The key node will
be compared with each node starting from root node if value of key node is greater
than current node then we search for it on right sub-branch otherwise on left sub-
branch. If we reach to leaf node and still we do not get the
value of key node then we declare "node is not present in
the tree".
Figure Binary search tree
In the above tree, if we want to search for value 9. Then we
will compare 9 with root node10. As 9 is less than 10 we will
search on left sub-branch. Now compare 9 with 5 but 9 is
greater than 5. So we will move on right sub-branch. Now
compare 9 with 8 but as 9 is greater than 8 we will move on
right sub-branch as the node we will get holds the value 9.
Thus the desired node can be searched.
Algorithm:
1. Read the value for the node which is to be searched, let search node value is x.
2. Compare the search node value x with root value
2.1. If root value == search node value x, print item searched and exit.
2.2. Elseif root value > search node value x, go to left child of root at left subtree
and repeat 2.1 until required value is not found. If node value == search
node value x, print item searched and exit.
2.3. Elseif root value < search node value x, go to right child of root at right
subtree and repeat 2.1 until required value is not found. If node value ==
search node value x, print item searched and exit.
2.4. Else print item is not present in BST and exit.
Here a is a linear array with n elements. and item is a given item of information. This
algorithm finds the location be of item in c. or sets ioc = 0 if the search is
unsuccessful.
#inc1ude<stdio.h>
#inc1ude<conio.h>
void main( )
{
int a[100], n, i, item, loc = -1 ;
c1rscr( ) ;
printf(“\nEnter the number of element : ") ;
scanf (“%d",&n) ;
printf(“Enter the numbers : \n”) ;
for (i=0 ; i<=n-1; i++)
{
scanf (“%d”, &a[i]) ;
}
printf(“\n enter the number to be search”);
scanf(“%d”, &item);
for(i=0; i<= n-1 ; i++)
{
if (item ==a[i])
{
loc = i;
break;
}
}
if (loc>=0)
printf (“\n%d is found in position %d”, item, loc + 1);
else
printf ("\n1tem does not exist") ;
getch( ) ;
}
Repeat the same steps until an element is found or exhausts in the search area. In
this algorithm every time we are reducing the search area. 50 number of comparisons
keep on decreasing. In worst case the number of comparisons is atmost log(N + 1). So
it is an efficient algorithm when compared to linear search but the array has to be
sorted before doing binary Search.
Here a is sorted array with lower bound LB and upper bound UB and Item is a given
Item Information. The variables beg, end and mid denoted, respectively, the beginning
and middle location of a segment of element of a. This algorithm finds the location loc
of item in or sets loc = NULL.
1. [Initialize segment variables]
set beg = LB end = UB and mid = int (( beg + end))/2
2. Repeat steps 3 and 4 while beg <= end and a[mid]!= item
3. If item < a[mid] then
Set end = mid -1
else
Set beg = mid + 1
[End of if structure]
4. set mid = int ((beg + end ))/2
[End of step 2 loop]
5. If a[mid]= item then
Set loc = mid
else
Set loc = NULL
[End of if structure}
6. Exit
Program to search an element from list of elements using binary search technique.
#inc1ude<stdio.h>
#include<conio.h>
void main( )
{
int a[100], i, loc. mid, beg, end, n, flag=0, item ;
clrscr ( ) ;
printf (“How many elements") ;
scanf (“%d",&n) ;
printf (“Enter the e1ement of the array\n") :
for(i=0; i<=n-1; i++)
{
scanf ("%d", &a [i]);
}
printf ("Enter the element to be search\n") ;
scanf ("%d", &item) ;
loc = 0 ;
beg = 0 ;
end = n-1 ;
whi1e ((beg <= end) && (item != a[mid]))
Sorting
Systematic arrangement of data is called SORTING. For example – in telephone
directory the person surname are arranged in increasing or decreasing order. The
sorting is a technique by which the elements are arranged in some particular order.
Usually the sorting order is of two types-
Ascending order: It is the sorting order in which the elements are arranged from low
value to high value. In other words elements are in increasing order. For example:
10, 50, 40, 20, 30 can be arranged in awarding order after applying some sorting
technique as 10, 20. 30, 40, 50
Descending Order: It is the sorting order in which the elements are arranged from
high value to low value. In other words elements are in decreasing order. It is reverse
of the ascending order. For example: 10, 50, 40, 20, 30 can be arranged in
descending order after applying some sorting technique as 50,40,30,20,10
External sorting - In external sorting, the data stored on secondary memory is part
by part loaded into main memory, sorting can be done over there. The sorted data can
be then stored in the intermediate files. Finally these intermediate files can be merge d
repeatedly to get sorted data. Thus huge amount of data can be sorted using this
technique.
The external merge sort is a technique in which the data is loaded in intermediate
files. Each intermediate file is sorted independently and then combined or merged to
get the sorted data. For example: Consider that there are 10,000 records that has to
be sorted- Clearly we need to apply external sorting method. Suppose main memory
has a capacity to store 500 records in blocks, with each block size of 100 records.
Fig.
The sorted 5 blocks (i.e. 500 records) are stored in intermediate file. This process will
be repeated 20 times to get all the records sorted in chunks. In the second step, we
start merging a pair of intermediate files in the main memory to get output file.
Various internal sorting techniques are - bubble sort, insertion sort, selection sort.
Difference between internal and external sorting
Sorting Techniques
Sorting is an important activity and every time we insert or delete the data at the time
of sorting, the remaining data retain in queue to sort. Various algorithms that are
developed for sorting are as follows -
1. Insertion Sort 2.Bubble sort 3. Selection Sort 4. Merge Sort
5. Quick Sort 6. Radix Sort 7. Shell Sort 8. Bucket Sort
1. Insertion Sort: In this method the elements are inserted at their appropriate
place. Hence is the name insertion sort. Consider the following example to
understand insertion sort.For Example: Consider a list of elements as 30, 70, 20,
50, 40, 10, 60
Step 1
Step 2
Step 3
Step 4
Step 6
Similarly now compare 10 with
the element of sorted zone i.e. 20.
30, 40, 50, 70 and insert it on
desired position with respect to it
Prepared by: Prof Dheresh Soni CSE Dept. SIRT Page 8
Step 7
Step 8
2. Bubble Sorting - This is the simplest kind of sorting method. In bubble sort
procedure we perform several iterations in groups which are called passes. In
this procedure first each element is filled in array. Now in each pass we compare
a[0] element of array with next element i.e. a[1]. If a[0] is greater than a[1] we
interchange the value and if it is not greater then value remain same and we move
to next step and compare a[1] with a[2]. Similarly if a[1] is greater than a[2] we
interchange the value otherwise not and move to next step. Like this when we reach
at last position, largest element comes at last position in array and 1 st pass is over.
Now list is sorted upto some extend. Similarly passes is repeated from a[0] to a[n]
till all element get sort.
Example: Consider 5 unsorted elements are 45, -40, 190, 99, 11
Pass 1: In this pass each element will be compared with its neighbouring element.
After first pass the array will hold the elements which are sorted to some
extent.
Pass2:
This is end of pass 3. This process will be thus continued till pass 4.
Finally at the [end of last pass the array will hold all the sorted elements
like this, Since the comparison positions look like bubbles, therefore it is
called bubble sort.
Algorithm:
Generally, on pass i (0 <= i <= n-2), the smallest element is searched among last n-i
elements and is swapped with A[ i ]. The list gets sorted after n-l passes.
Example: Consider the elements 70, 30, 20, 50, 60, 10, 40 We can store these
elements in array A as :
1st Pass:
2nd Pass:
3 rd Pass:
4th Pass:
5th Pass:
The above algorithm can be analysed mathematically. We will apply a general plan
for non recursive mathematical analysis. The input size is n i.e. total number of
elements in the list. In the algorithm the basic operation is key comparison. if A[i] <
A[min]. This basic operation depends only on array size n. Thus time complexity of
selection sort is (n2) for all input, but total number of key swaps is only (n)
4. Merge Sorting - The merge sort is a sorting algorithm that uses the divide and
conquer strategy. In this method division is dynamically carried out. Merge sort on
an input array with n elements consists of three steps:
1. Divide: Partition array into two sublists S1 and S2 with n/2 elements each
2. Conquer: Then sort sub list S1 and sublist S2.
3. Combine: Merge S1 and S2 into a unique sorted group.
Analysis: In merge sort algorithm two recursive calls are made. Each recursive call
focuses on n/ 2 elements of the list. After two recursive calls one call is made to
combine two sublists i.e. to merge all the elements. We can write it as –
T(n) = T(n/2) + T(n/2) + Cn
Time taken Time taken Time for
by left by right combining
sublist to sublist to two soblists
get sorted get sorted
T(n) = O(nlog2n)
The average and worst case time complexity of merge sort is O(nlog2 n)
Example 1: Consider the elements as 70, 20, 30, 40, 10, 50, 60
1. Divide: Split the array into two sub arrays that each element in the left sub
array is less than or equal the middle element and each element in the right sub
array is greater than the middle element. The splitting of the array into two sub
arrays is based on pivot element. All the elements that are less than pivot
should be in left sub array and all the elements that are more than pivot should
be in right sub array.
2. Conquer: Recursively sort the two sub arrays. A
3. Combine: Combine all the sorted elements in a group to form a list of sorted
elements.
In merge sort the division of array is based on the positions of array elements, but
quick sort this division is based on actual value of the element. Consider an array
a[i] where i is ranging from 0 to n - 1 then we can formulize the division of array
elements as
let us understand this algorithm with the help of some example. Example
We will now split the array in two parts. The left sublist will contain the elements
less than Pivot (i.e. 50) and right sublist contains elements greater than pivot.
Step 2:
We will increment i. If A[i] <= Pivot, we will continue to increment it until the
element pointed by i is greater than A[Low].
Step 3:
Step 4:
Step 5:
As A[j] > Pivot (i.e. 70 > 50). We will decrement j. We will continue to decrement j
until the element pointed by j is less than A [Low].
Step 6:
Now we cannot decrement j because 40 < 50. Hence we will swap A[i] and A[j] i.e. 90
and 40.
As A[i] is less than A[Low] and A[j] is greater than A[Low] we will continue
incrementing i and decrementing j. until the false conditions are obtained.
Step 8:
Step 9:
As A[i] < A[Low] and A[j] > A[Low], we will continue incrementing i and decrementing
j.
Step 10:
As A[j] < A[Low] and j has crossed i that is j < i, we will swap A[low] and A[j].
Step 11:
We will now start sorting left sublist, assuming the first element of left sublist as
pivot element. Thus now new pivot = 20.
Step 12:
Step 13:
As A[i] > A[Pivot]. hence stop incrementing i. Now as A[i] > A[Pivot]. hence decrement
j.
Step 14:
Now j can not be decremented because 10 < 20. Hence we will swap A[i] and A[j].
Step 15:
Step 16:
Step 17:
As A[j] < A[Low] we cannot decrement j now. We will now swap A[Low] and A[i] as 1
has crossed i and i > j
Step 18:
Step 19:
As left sublist is sorted completely we will sort right sublist, assuming first element
of right sublist as pivot.
Step 20:
As A[i] > A[Pivot]. hence we will stop incrementing i. Similarly A[j] < A[Pivot]. Hence
we stop decrementing j. Swap A[i] and A[j].
Step 21:
Step 22:
Step 23:
Step 24:
Hence list is
The partition function is called to arrange the elements such that all the elements
that are less than pivot are at the left side of pivot and all the elements that are
greater than pivot are all at the right of pivot. In other words pivot is occupying its
proper position and the partitioned list is obtained in an ordered manner.
Analysis: When pivot is chosen such that the array gets divided at the mid then it
gives the best case time complexity. The best case time complexity of quick sort is
O(nlog2 n). The worst case for quick sort occurs when the pivot is minimum or
maximum of all the elements in the list. This can be graphically represented as -
This ultimately results in 0(n2) time complexity. When array elements are randomly
distributed then it results in average case time complexity, and it is O(nlog2n).
6. Radix Sorting
In this method sorting can be done digit by digit from ones place to tens then
hundred and so on. Thus all the elements get sort. Example for Radix sort -
Consider the unsorted array of 8 elements. 45, 37, 05, 09, 06, 11, 18, 27
Step 1: Now sort the element according to the last digit (ones place).
Since the list of element is of two digits that is why, we will stop comparing. Now
whatever list we have got (shown in above array) is of sorted elements. Thus finally
the sorted list by radix sort method will be 05 06 09 11 18 27 37 45
Algorithm:
7. Bucket Sorting
Bucket sort is a sorting technique in which array is partitioned into buckets. Each
bucket is then sorted individually, using some other sorting algorithm such as
insertion
Algorithm
1. Set up an array of initially empty buckets.
2. Put each element in corresponding bucket.
3. Sort each non empty bucket.
4. Visit the buckets in order and put all the elements into a sequence and print
them.
Example: Sort the element using bucket sort 56, 12, 84, 56, 28, 0, -13, 47, 94,
31, 12, -2
Solution: We will set up an array as follows
This is the sorted list: -13, -2, 0, 12, 12, 28, 31, 47, 56, 56, 84, 94.
8. Shell Sorting
This method is an improvement over the simple insertion sort. In this method the
elements at fixed distance are compared. The distance will then be
decremented by some fixed amount and again the comparison will be made.
Finally, individual elements will be compared. Consider the following example to
understand shell sort.
For Example: Consider a list of elements as 25, 57, 48, 37, 12, 92, 86, 33
( x[0], x[3] )
( x[1], x[4] )
( x[2], x[5] )
( x[3], x[6] )
( x[4], x[7] )
After
Second iteration
Now after above sort, elements are sorted by simple insertion sort because simple
insertion sort is highly efficient on sorted file. So we get
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
12 27 33 37 48 57 86 92
Analysis:
Best Case: The best case in the shell sort is when the array is already sorted in the
right order. The number of comparisons is less. In that case the inner loop does not
need to any work and a simple comparison will be sufficient to skip the inner sort
loop. The other loops gives O(n logn). The best case of O(n) is reached by using a
constant number of increments. Hence the best case time complexity of shell sort is
O (n logn)
Worst Case and Average Case: The running time of Shell sort depends on the
choice of increment sequence. The problem with Shell’s increments is that pairs of
increments l are not necessarily relatively prime and smaller increments can have
little effect. The worst case and average case time complexity is O(n).
Two-Way Merge Sort - The two way merge sort makes use of two input tapes and
two output tapes for sorting the records. Input tape is memory to store items. It
works in two stages –
Stage 1: Break the records into blocks. Sort individual records with the help of two
input tapes.
Let us understand it with the help of an example. Sort the following list of elements
using two way merge sort with M=3 and elements are 20, 47, 15, 8, 9, 4, 40, 30, 12,
17, 11, 56, 28, 35
Solution: As M = 3, we will break the records in the group of 3 and will store them
on tape. We will store data on alternate tapes.
Stage I: Sorting Phase
1. Read the 3 item from list and sort them and store them on Tape Tb1
20, 47, 15 15, 20, 47
2. Read next three records sort them and store them on Tape Tb2
8, 9, 4 4, 8, 9
4. Read next three records, sort them and store on tape Tb2.
17, 11, 56 11, 17, 56
5. Read next two remaining records, sort them and store on Tape Tbl
28,35 28,35
The input tapes Tbl and Tb2 will use two more output tapes Ta1 and Ta2, for
sorting. Finally the sorted data will be on tape Tal or Ta2. We will read the elements
from both the tapes Tbl and Tb2, compare them, and store on Tal in sorted order.
Comparing is done block by block i.e. compare 4 and 15 so 4 come in Ta1 then, 15
with 8 then, 8 comes out now again next element 15 with 9, again 9 come out in Ta1.
Now Tb2 one block is empty, so remaining element of Tb1 comes in Ta1.
Now similarly we will read second block from Tb1 and Tb2, compare them and store in
Ta2
Finally read the third block from Tbl and store in sorted manner on Tal. We will not
compare this block with Ta2 as there is no third block Hence we will get following
blocks
Now compare Ta1 and Tb1, perform sorting and store elements in Ta1. Finally we get
Algorithm:
Step 1: Divide the elements into the blocks of size M. Sort each block and write runs
on disk.
Step 2: Merge two runs
i) Read first value on each of two runs
ii) Compare and sort
iii) Write on output tape.
Step 3: Repeat the step 2 and get longer and longer runs on alternate tapes. Finally
we will get single sorted list.
Analysis: - The number of passes for multiway merging is log (N/M)