0% found this document useful (0 votes)
25 views

Data Structure Notes 3to 5 Modules

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)
25 views

Data Structure Notes 3to 5 Modules

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/ 34

DATA STRUCTURES

1
MOUDLE -III
SORTING AND SEARCHING

Hashing: Refer the PPT

SORTING-INTRODUCTION

1. Whai is sorting?

Sorting is a technique of organizing the data. It is a process of arranging the records,


either in ascending or descending order i.e. bringing some order lines in the data. Sort
methods are very important in Data structures.

In numerical sorting, the records arranged in ascending or descending order according to


the numeric value of the key.
Let A be a list of n elements A1, A2, A3.................................... An in memory. Sorting A refers to the
operation of rearranging the contents of A so that they are increasing in order, that is, so
that A1 <=A2 <=A3 <=…………….<=An.
example

Ex: suppose an array DATA contains 8elements as follows:


DATA: 70, 30,40,10,80,20,60,50.
After sorting DATA must appear in memory as follows:
DATA: 10 20 30 40 50 60 70 80
2.Factors to be considered while choosing sorting techniques ?
• Programming Time
• Execution Time
• Number of Comparisons
• Memory Utilization
• Computational Complexity

3. list the Types of Sorting Techniques


Sorting techniques are categorized into 2 types. They are Internal Sorting and External
Sorting.
Internal Sorting: Internal sorting method is used when small amount of data has to be
sorted. In this method , the data to be sorted is stored in the main memory (RAM).Internal
sorting method can access records randomly. EX: Bubble Sort, Insertion Sort, Selection Sort,
Shell sort, Quick Sort, Radix Sort, Heap Sort etc.
External Sorting: Extern al sorting method is used when large amount of data has to be
sorted. In this method, the data to be sorted is stored in the main memory as well as in the
secondary memory such as disk. External sorting methods an access records only in a
sequential order. Ex: Merge Sort, Multi way Mage Sort.

Complexity of sorting Algorithms: The complexity of sorting algorithm measures the


running time as a function of the number n of items to be stored. Each sorting algorithm S
will be made up of the following operations, where A1, A2, A3.................................... An contain the
items to be sorted and B is an auxiliary location.
2
• Comparisons, which test whether Ai < Aj or test whether Ai <B.
• Interchanges which switch the contents of Ai and Aj or of Ai and B.
• Assignment which set B: Ai and then set Aj := B or Aj:= Ai
Normally, the complexity function measures only the number of comparisons, since the
number of other operations is at most a constant factor of the number of comparisons.

Types of sorting
1. Bubble Sort
2. Selection Sort
3. insertion sort
4. Merge sort
5. Quick sort

4. Explain the Bubble sort, selection sort with example

SELECTION SORT
In selection sort, the smallest value among the unsorted elements of the array is
selected in every pass and inserted to its appropriate position into the array.
First, find the smallest element of the array and place it on the first position. Then,
find the second smallest element of the array and place it on the second position.
The process continues until we get the sorted array. The array with n elements is
sorted by using n-1 pass of selection sort algorithm.

• In 1st pass, smallest element of the array is to be found along with its index
pos. then, swap A[0] and A[pos]. Thus A[0] is sorted, we now have n -1
elements which are tobe sorted.
• In 2nd pas, position pos of the smallest element present in the sub-array A[n-
1] is found. Then, swap, A[1] and A[pos]. Thus A[0] and A[1] are sorted, we
now left with n-2 unsorted elements.
• In n-1th pass, position pos of the smaller element between A[n-1] and A[n-2]
is to be found. Then, swap, A[pos] and A[n-1].

Therefore, by following the above explained process, the elements A[0],


A[1], A[2], ... , A[n-1] are sorted.

Example: Consider the following array with 6 elements. Sort the elements of the array by
using selection sort.
A = {10, 2, 3, 90, 43, 56}.

Pass Pos A[0] A[1] A[2] A[3] A[4] A[5]


1 1 2 10 3 90 43 56
2 2 2 3 10 90 43 56
3 3 2 3 10 90 43 56
4 4 2 3 10 43 90 56
5 5 2 3 10 43 56 90

3
Sorted A = {2, 3, 10, 43, 56, 90}

Complexity
Complexity Best Average Case Worst Case
Case
Time Ω(n) θ(n2) o(n2)

Space o(1)

4
Algorithm

SELECTION SORT (ARR, N)

Step 1: Repeat Steps 2 and 3 for K = 1 to N-1

Step 2: CALL SMALLEST(A, K, N, POS)

Step 3: SWAP A[K] with


A[POS] [END OF LOOP]
Step 4: EXIT

BUBBLE SORT
Bubble Sort: This sorting technique is also known as exchange sort, which arranges
values by iterating over the list several times and in each iteration the larger value gets
bubble up to the end of the list. This algorithm uses multiple passes and in each pass the
first and second data items are compared. if the first data item is bigger than the second,
then the two items are swapped. Next the items in second and third position are compared
and if the first one is larger than the second, then they are swapped, otherwise no change in
their order. This process continues for each successive pair of data items until all items are
sorted.
Bubble Sort Algorithm:
Step 1: Repeat Steps 2 and 3 for i=1 to 10
Step 2: Set j=1
Step 3: Repeat while j<=n
(A)
if a[i] < a[j] Then
interchange a[i] and a[j]
[End of if]
(B) Set j = j+1
[End of Inner Loop]
[End of Step 1 Outer Loop]
Step 4: Exit

5
5. Explain Insertion sort

INSERTION SORT

Insertion sort is a very simple method to sort numbers in an ascending or


descending order. This method follows the incremental method. It can be
compared with the technique how cards are sorted at the time of playing a
game.

This is an in-place comparison-based sorting algorithm

Procedure

The simple steps of achieving the insertion sort are listed as follows -

Step 1 - If the element is the first element, assume that it is already sorted.

Step2 - Pick the next element and store it separately in a key.

6
Step3 - Now, compare the key with all elements in the sorted array.

Step 4 - If the element in the sorted array is smaller than the current element,
then move to the next element. Else, shift greater elements in the array
towards the right.

Example:

The steps to sort the values stored in the array in ascending order using Insertion sort are
given below:
7 33 20 11 6

Step 1: The first value i.e; 7 is trivially sorted by itself.


Step 2: the second value 33 is compared with the first value 7. Since 33 is greater than 7, so
no changes are made.
Step 3: Next the third element 20 is compared with its previous element (towards left).Here
20 is less than 33.but 20 is greater than 7. So it is inserted at second position. For this 33 is
shifted towards right and 20 is placed at its appropriate position.

7 33 20 11 6

7 20 33 11 6

Step 4: Then the fourth element 11 is compared with its previous elements. Since 11 is less
than 33 and 20 ; and greater than 7. So it is placed in between 7 and 20. For this the
elements 20 and 33 are shifted one position towards the right.

7 20 33 11 6

7 11 20 33 6
Step5: Finally the last element 6 is compared with all the elements preceding it. Since it is
smaller than all other elements, so they are shifted one position towards right and 6 is
inserted at the first position in the array. After this pass, the Array is sorted.

7 11 20 33 6

6 7 11 20 33
Step 6: Finally the sorted Array is as follows:

6 7 11 20 33

ALGORITHM:
7
void insertion_sort ( int A[ ] , int n)
{
for( int i = 0 ;i < n ; i++ )
{
/*storing current element whose left side is checked for its correct position .*/
int temp = A[ i ];
int j = i;
/* check whether the adjacent element in left side is greater or less than the current element. */
while( j > 0 && temp < A[ j -1])
{
// moving the left side element to one position forward.
A[ j ] = A[ j-1];
j= j - 1;
}
// moving current element to its correct position.
A[ j ] = temp;
}
}
Advantages of Insertion Sort:
• It is simple sorting algorithm, in which the elements are sorted by considering one
item at a time. The implementation is simple.
• It is efficient for smaller data set and for data set that has been substantially sorted
before.
• It does not change the relative order of elements with equal keys
• It reduces unnecessary travels through the array
• It requires constant amount of extra memory space.

Disadvantages:-
• It is less efficient on list containing more number of elements.
• As the number of elements increases the performance of program would be slow

QUICK SORT
The Quick Sort algorithm follows the principal of divide and Conquer. It first picks
up the partition element called ‘Pivot’, which divides the list into two sub lists such that all
the elements in the left sub list are smaller than pivot and all the elements in the right sub
list are greater than the pivot. The same process is applied on the left and right sub lists
separately. This process is repeated recursively until each sub list containing more than one
element.

The steps to sort the values stored in the array in the ascending order using Quick Sort are
given below.

8 33 6 21 4
Step 1: Initially the index ‘0’ in the list is chosen as Pivot and the index variable Beg and End
are initiated with index ‘0’ and (n-1) respectively.

8
Step 2: The scanning of the element starts from the end of the list.
A[Pivot]>A[End]
i.e; 8>4
so they are swapped.

Step 3: Now the scanning of the elements starts from the beginning of the list. Since
A[Pivot]>A[Beg]. So Beg is incremented by one and the list remains unchanged.

Step 4: The element A[Pivot] is smaller than A[Beg].So they are swapped.

Step 5: Again the list is scanned form right to left. Since A[Pivot] is smaller than A[End], so
the value of End is decreased by one and the list remains unchanged.

Step 6: Next the element A[Pivot] is smaller than A[End], the value of End is increased by
one. and the list remains unchanged.

9
Step 7: A[Pivot>>A[End] so they are swapped.

Step 8: Now the list is scanned from left to right. Since A[Pivot]>A[Beg],value of Beg is
increased by one and the list remains unchanged.

At this point the variable Pivot, Beg, End all refers to same element, the first pass is
terminated and the value 8 is placed at its appropriate position. The elements to its left are
smaller than 8 and the elements to its right are greater than 8.The same process is applied
on left and right sub lists.

ALGORITHM
Step 1: Select first element of array as Pivot
Step 2: Initialize i and j to Beg and End elements respectively
Step 3: Increment i until A[i]>Pivot.
Stop
Step 4: Decrement j until A[ j]>Pivot
Stop
Step 5: if i<j interchange A[i] with A[j].
Step 6: Repeat steps 3,4,5 until i>j i.e: i crossed j.
Step 7: Exchange the Pivot element with element placed at j, which is correct place for
Pivot.
Advantages of Quick Sort:
• This is fastest sorting technique among all.
• It efficiency is also relatively good.
• It requires small amount of memory

Disadvantages:
• It is somewhat complex method for sorting.
• It is little hard to implement than other sorting methods
• It does not perform well in the case of small group of elements.

Complexities of Quick Sort:


Average Case: The running time complexity is O(n log n).
Worst Case : Input array is not evenly divided. So the running time complexity is O(n2 ).
Best Case: Input array is evenly divided. So the running time complexity is O(n logn).
10
MERGE SORT

• Merge sort is the sorting technique that follows the divide and conquer approach.
• It also called stable sort
• It divides the given list into two equal halves, calls itself for the two halves and then merges the
two sorted halves.
• The sub-lists are divided again and again into halves until the list cannot be divided further. Then
we combine the pair of one element lists into two-element lists, sorting them in the process.
Let us apply the Merge Sort to sort the following list:

13 42 36 20 63 23 12
Step 1: First divide the combined list into two sub lists as follows.

Step 2: Now Divide the left sub list into smaller sub list

Step 3: Similarly divide the sub lists till one element is left in the sub list.

Step 4: Next sort the elements in their appropriate positions and then combined the sub
lists.

Step 5: Now these two sub lists are again merged to give the following sorted sub list of size
4.

Step 6: After sorting the left half of the array, containing the same process for the right sub
list also. Then the sorted array of right half of the list is as follows.

Step 7: Finally the left and right halves of the array are merged to give the sorted array as
follows.

11
Advantages:
• Merge sort is stable sort
• It is easy to understand
• It gives better performance.

Disadvantages:
• It requires extra memory space
• Copy of elements to temporary array
• It requires additional array
• It is slow process.

Complexity of Merge Sort: The merge sort algorithm passes over the entire list and requires
at most log n passes and merges n elements in each pass. The total number of comparisons
required by the merge sort is given by O(n log n).

External searching: When the records are stored in disk, tape, any secondary storage then
that searching is known as ‘External Searching’.
Internal Searching: When the records are to be searched or stored entirely within the
computer memory then it is known as ‘Internal Searching’.

12
LINEAR SEARCH
The Linear search or Sequential Search is most simple searching method. It does
not expect the list to be sorted. The Key which to be searched is compared with each
element of the list one by one. If a match exists, the search is terminated. If the end of the
list is reached, it means that the search has failed and the Key has no matching element in
the list.
Ex: consider the following Array A
23 15 18 17 42 96 103
Now let us search for 17 by Linear search. The searching starts from the first position.
Since A[0] ≠17.
The search proceeds to the next position i.e; second position A[1] ≠17.
The above process continuous until the search element is found such as A[3]=17.
Here the searching element is found in the position 4.

Algorithm: LINEAR(DATA, N,ITEM, LOC)


Here DATA is a linear Array with N elements. And ITEM is a given item of information. This
algorithm finds the location LOC of an ITEM in DATA. LOC=-1 if the search is unsuccessful.
Step 1: Set DATA[N+1]=ITEM
Step 2: Set LOC=1
Step 3: Repeat while (DATA [LOC] != ITEM)
Set LOC=LOC+1
Step 4: if LOC=N+1 then
Set LOC= -1.
Step 5: Exit

Advantages:
• It is simplest known technique.
• The elements in the list can be in any order.
Disadvantages:
This method is in efficient when large numbers of elements are present in list because time
taken for searching is more.
Complexity of Linear Search: The worst and average case complexity of Linear search is
O(n), where ‘n’ is the total number of elements present in the list.

BINARY SEARCH
Pseudocode

Step 1: Pick an index q in the middle range [i, l] i.e. q =


ë(n +1)/2û

and compare x with aq.

Step 2: if x = aq i.e key element is equal to mid element, the problem is immediately solved.

Step 3: if x < aq in this case x has to be searched for only in the sub-list ai, ai+1, ……, aq-1.
Therefore problem reduces to (q- i, ai…aq-1, x).

Step 4: if x > aq , x has to be searched for only in the sub-list aq+1, ...,., al . Therefore problem
reduces to (l-i, aq+1…al, x).
13
Define sorting? What is the difference between internal and external sorting methods?
Ans:- Sorting is a technique of organizing data. It is a process of arranging the elements
either may be ascending or descending order, ie; bringing some order lines with data.

Internal sorting External sorting


1. Internal Sorting takes place in the main 1. External sorting is done with additional
memory of a computer. external memory like magnetic tape or hard
disk
2. The internal sorting methods are applied 2. The External sorting methods are applied
to small collection of data. only when the number of data elements to
be sorted is too large.
3. Internal sorting takes small input 3. External sorting can take as much as large
input.
4. It means that, the entire collection of data 4. External sorting typically uses a sort-
to be sorted in small enough that the sorting merge strategy, and requires auxiliary
can take place within main memory. storage.
5. For sorting larger datasets, it may be 5. In the sorting phase, chunks of data small
necessary to hold only a chunk of data in enough to fit in main memory are read,
memory at a time, since it wont all fit. sorted, and written out to a temporary file.
6. Example of Internal Sorting algorithms are 6. Example of External sorting algorithms
:- Bubble Sort, Internal Sort, Quick Sort, are: - Merge Sort, Two-way merge sort.
Heap Sort, Binary Sort, Radix Sort, Selection
sort.
7. Internal sorting does not make use of 7. External sorting make use of extra
extra resources. resources.

14
UNIT-IV
TREES AND BINARY TREES

TREES
INTRODUCTION
In linear data structure data is organized in sequential order and in non-linear data structure
data is organized in random order. A tree is a very popular non-linear data structure used in
a wide range of applications. Tree is a non-linear data structure which organizes data in
hierarchical structure and this is a recursive definition.

1. what is Tree. Explain some types of tree :


Tree is collection of nodes (or) vertices and their edges (or) links. In tree data structure,
every individual element is called as Node. Node in a tree data structure stores the actual
data of that particular element and link to next element in hierarchical structure.

Note: 1. In a Tree, if we have N number of nodes then we can have a maximum of N-


1 number of links or edges.
2. Tree has no cycles.

TREE TERMINOLOGIES:
1. Root Node: In a Tree data structure, the first node is called as Root Node. Every tree
must have a root node. We can say that the root node is the origin of the tree data
structure. In any tree, there must be only one root node. We never have multiple root
nodes in a tree.

2. Edge: In a Tree, the connecting link between any two nodes is called as EDGE. In a tree
with 'N' number of nodes there will be a maximum of 'N-1' number of edges.

15
3. Parent Node: In a Tree, the node which is a predecessor of any node is called as PARENT
NODE. In simple words, the node which has a branch from it to any other node is called a
parent node. Parent node can also be defined as "The node which has child / children".

Here, A is parent of B&C. B is the parent of D,E&F and so on…


4. Child Node: In a Tree data structure, the node which is descendant of any node is called
as CHILD Node. In simple words, the node which has a link from its parent node is called as
child node. In a tree, any parent node can have any number of child nodes. In a tree, all the
nodes except root are child nodes.

5. Siblings: In a Tree data structure, nodes which belong to same Parent are called
as SIBLINGS. In simple words, the nodes with the same parent are called Sibling nodes.

16
6. Leaf Node: In a Tree data structure, the node which does not have a child is called
as LEAF Node. In simple words, a leaf is a node with no child. In a tree data structure, the
leaf nodes are also called as External Nodes. External node is also a node with no child. In a
tree, leaf node is also called as 'Terminal' node.

7. Internal Nodes: In a Tree data structure, the node which has atleast one child is called
as INTERNAL Node. In simple words, an internal node is a node with atleast one child.

In a Tree data structure, nodes other than leaf nodes are called as Internal Nodes. The root
node is also said to be Internal Node if the tree has more than one node. Internal nodes are
also called as 'Non-Terminal' nodes.

8. Degree: In a Tree data structure, the total number of children of a node is called
as DEGREE of that Node. In simple words, the Degree of a node is total number of children it
has. The highest degree of a node among all the nodes in a tree is called as 'Degree of Tree'

Degree of Tree is: 3

9. Level: In a Tree data structure, the root node is said to be at Level 0 and the children of
root node are at Level 1 and the children of the nodes which are at Level 1 will be at Level 2

17
and so on... In simple words, in a tree each step from top to bottom is called as a Level and
the Level count starts with '0' and incremented by one at each level (Step).

10. Height: In a Tree data structure, the total number of edges from leaf node to a particular
node in the longest path is called as HEIGHT of that Node. In a tree, height of the root node
is said to be height of the tree. In a tree, height of all leaf nodes is '0'.

11. Depth: In a Tree data structure, the total number of egdes from root node to a
particular node is called as DEPTH of that Node. In a tree, the total number of edges from
root node to a leaf node in the longest path is said to be Depth of the tree. In simple words,
the highest depth of any leaf node in a tree is said to be depth of that tree. In a tree, depth
of the root node is '0'.

12. Path: In a Tree data structure, the sequence of Nodes and Edges from one node to
another node is called as PATH between that two Nodes. Length of a Path is total number
of nodes in that path. In below example the path A - B - E - J has length 4.

18
13. Sub Tree: In a Tree data structure, each child from a node forms a subtree recursively.
Every child node will form a subtree on its parent node.

TREE REPRESENTATIONS:
A tree data structure can be represented in two methods. Those methods are as follows...

1. List Representation
2. Left Child - Right Sibling Representation

Consider the following tree...

3. . Representation of tree using list


In this representation, we use two types of nodes one for representing the node with data
called 'data node' and another for representing only references called 'reference node'. We
start with a 'data node' from the root node in the tree. Then it is linked to an internal node

19
through a 'reference node' which is further linked to any other node directly. This process
repeats for all the nodes in the tree.
The above example tree can be represented using List representation as follows...

1. Left Child - Right Sibling Representation


In this representation, we use a list with one type of node which consists of three fields
namely Data field, Left child reference field and Right sibling reference field. Data field
stores the actual value of a node, left reference field stores the address of the left child and
right reference field stores the address of the right sibling node. Graphical representation of
that node is as follows...

In this representation, every node's data field stores the actual value of that node. If that node has
left a child, then left reference field stores the address of that left child node otherwise stores NULL.
If that node has the right sibling, then right reference field stores the address of right sibling node
otherwise stores NULL.

The above example tree can be represented using Left Child - Right Sibling representation as
follows...

4. what is BINARY Tree and types of binary tree

In a normal tree, every node can have any number of children. A binary tree is a special type
of tree data structure in which every node can have a maximum of 2 children. One is known
as a left child and the other is known as right child.

A tree in which every node can have a maximum of two children is called Binary Tree.

20
In a binary tree, every node can have either 0 children or 1 child or 2 children but not more
than 2 children.

In general, tree nodes can have any number of children. In a binary tree, each node can
have at most two children. A binary tree is either empty or consists of a node called the root
together with two binary trees called the left subtree and the right subtree. A tree with no
nodes is called as a null tree

Example:

TYPES OF BINARY TREE:


1. Strictly Binary Tree:
In a binary tree, every node can have a maximum of two children. But in strictly binary tree,
every node should have exactly two children or none. That means every internal node must
have exactly two children. A strictly Binary Tree can be defined as follows...
A binary tree in which every node has either two or zero number of children is called
Strictly Binary Tree
Strictly binary tree is also called as Full Binary Tree or Proper Binary Tree or 2-Tree.

Strictly binary tree data structure is used to represent mathematical expressions.

21
Example

2. Complete Binary Tree:


In a binary tree, every node can have a maximum of two children. But in strictly binary tree,
every node should have exactly two children or none and in complete binary tree all the
nodes must have exactly two children and at every level of complete binary tree there must
be 2level number of nodes. For example at level 2 there must be 22 = 4 nodes and at level 3
there must be 23 = 8 nodes.
A binary tree in which every internal node has exactly two children and all leaf nodes are
at same level is called Complete Binary Tree.

Complete binary tree is also called as Perfect Binary Tree.

3. Extended Binary Tree:


A binary tree can be converted into Full Binary tree by adding dummy nodes to existing
nodes wherever required.
The full binary tree obtained by adding dummy nodes to a binary tree is called as
Extended Binary Tree.

22
In above figure, a normal binary tree is converted into full binary tree by adding dummy
nodes.
4. Skewed Binary Tree:
If a tree which is dominated by left child node or right child node, is said to be a Skewed
Binary Tree.
In a skewed binary tree, all nodes except one have only one child node. The remaining
node has no child.

In a left skewed tree, most of the nodes have the left child without corresponding right
child.
In a right skewed tree, most of the nodes have the right child without corresponding left
child.
Properties of binary trees:
Some of the important properties of a binary tree are as follows:
1. If h = height of a binary tree, then
a. Maximum number of leaves = 2h
b. Maximum number of nodes = 2h + 1 - 1
2. If a binary tree contains m nodes at level l, it contains at most 2m nodes at level l + 1.
3. Since a binary tree can contain at most one node at level 0 (the root), it can contain at
most 2l node at level l.
4. The total number of edges in a full binary tree with n node is n –
BINARY TREE REPRESENTATIONS:
A binary tree data structure is represented using two methods. Those methods are as
follows...
1. Array Representation
2. Linked List Representation
Consider the following binary tree...

23
5. give the Array Representation of Binary Tree
In array representation of a binary tree, we use one-dimensional array (1-D Array) to
represent a binary tree.
Consider the above example of a binary tree and it is represented as follows...

To represent a binary tree of depth 'n' using array representation, we need one dimensional
array with a maximum size of 2n + 1.
1. Linked List Representation of Binary Tree
We use a double linked list to represent a binary tree. In a double linked list, every node
consists of three fields. First field for storing left child address, second for storing actual
data and third for the right child address.
In this linked list representation, a node has the following structure...

The above example of the binary tree represented using Linked list representation is shown
as follows...

24
5. Explain BINARY TREE TRAVERSALS with example

In any binary tree, displaying order of nodes depends on the traversal method.

Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree Traversal.
There are three types of binary tree traversals.
1. In - Order Traversal
2. Pre - Order Traversal
3. Post - Order Traversal

Tree traversal in C

#include <stdio.h>
#include <stdlib.h>

struct node {
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
// preorderTraversal traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// postorderTraversal traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
25
postorderTraversal(root->right);
printf("%d ->", root->item);
}

Example:

1. In - Order Traversal (left Child - root - right Child):


In In-Order traversal, the root node is visited between the left child and right child. In this
traversal, the left child node is visited first, then the root node is visited and later we go for
visiting the right child node. This in-order traversal is applicable for every root node of all
sub trees in the tree. This is performed recursively for all nodes in the tree.
Algorithm:
Step-1: Visit the left subtree, using inorder.
Step-2: Visit the root.
Step-3: Visit the right subtree, using inorder.

That means here we have visited in the order of I - D - J - B - F - A - G - K - C - H using In-


Order Traversal.
2. Pre - Order Traversal ( root - leftChild - rightChild ):
In Pre-Order traversal, the root node is visited before the left child and right child nodes. In
this traversal, the root node is visited first, then its left child and later its right child. This pre-
order traversal is applicable for every root node of all subtrees in the tree. Preorder search
is also called backtracking.
Algorithm:
Step-1: Visit the root.
Step-2: Visit the left subtree, using preorder.
Step-3: Visit the right subtree, using preorder.

26
That means here we have visited in the order of A-B-D-I-J-F-C-G-K-H using Pre-Order
Traversal.

3. Post - Order Traversal ( leftChild - rightChild - root ):


In Post-Order traversal, the root node is visited after left child and right child. In this
traversal, left child node is visited first, then its right child and then its root node. This is
recursively performed until the right most nodes are visited.
Algorithm:
Step-1: Visit the left subtree, using postorder.
Step-2: Visit the right subtree, using postorder
Step-3: Visit the root.

Here we have visited in the order of I - J - D - F - B - K - G - H - C - A using Post-Order


Traversal.

27
6. write short notes on 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-
1.Only smaller values in its left sub tree
2.Only larger values in its right sub tree.

Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree and can be
defined as −
left_subtree (keys) < node (key) ≤ right_subtree (keys)

Basic Operations

Following are the basic operations of a tree


• Search − Searches an element in a tree.
• Insert − Inserts an element in a tree.
• Pre-order Traversal − Traverses a tree in a pre-order manner.
• In-order Traversal − Traverses a tree in an in-order manner.
Post-order Traversal − Traverses a tree in a post-order manner

7. Construct a Binary Search Tree (BST) for the following sequence of numbers-
10,12,5,4,20,8,7,15 and 13

28
7. Construct a Binary Search Tree (BST) for the following sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100

As 0 0, so insert 0 to the right of 0.


As 0 < 0, so insert 0 to the le of 0.

As 0 0, so insert 0 to the right of 0.

As 0 < 0, so insert 0 to the le of 0. As 0 0, so insert 0 to the right of 0.


As 0 0, so insert 0 to the right of 0.

29
As 0 < 0, so insert 0 to the le of 0.
As 10 < 0, so insert 10 to the le of 0.
As 0 0, so insert 0 to the right of 0.
As 10 < 0, so insert 10 to the le of 0.

As 100 0, so insert 100 to the right of 0.


As 100 0, so insert 100 to the right of 0.
As 100 0, so insert 100 to the right of 0.

Hence the BST tree is constructed..

8. describe the AVL TREE

The term AVL tree was introduced by Adelson-Velsky and EM Landis.


• AVL trees are special kind of binary search trees. It is a balanced binary search tree . It also called
as self-balancing binary search trees
• Every node in the AVL tree , the height of the left subtree, right subtree can differ by atmost 1 (-
1,0,+1) with HL-HR<=1
• Balance factor= HL-HR

Balance Factor-

In AVL tree,
• Balance factor is defined for every node.
• Balance factor of a node = Height of the left subtree – Height of the right subtree
In AVL tree,
Balance factor of every node is either 0 or 1 or -1.

30
9. Explain the types of Rotation in AVL

31
32
7. What is the maximum height of any AVL-tree with 5 node?

N(h)= 1+N(h-1)+N(H-2)

8. Consturct AVL tree for the following 28,73,89,75,74,13,10,5

9. What is B-tree

B tree is specialized Multiway tree used to store records in a disk. B-Tree is Binary search Tree ,but not a binary
tree
Properties of B-Tree

Every node has at most m children.


Every node, except for the root and the leaves, has at least ⌈m/2⌉ children.
The root node has at least two children unless it is a leaf.
All leaves appear on the same level.

33
34

You might also like