Data Structure Unit-3-4 For 2019-20
Data Structure Unit-3-4 For 2019-20
Tree
A Tree is a recursive data structure containing the set of one or more data
nodes where one node is designated as the root of the tree while the
remaining nodes are called as the children of the root.
The nodes other than the root node are partitioned into the non empty
sets where each one of them is to be called sub-tree.
In a general tree, A node can have any number of children nodes but it
can have only a single parent.
The above figure shows a tree, where the node A is the root node of the
tree while the other nodes can be seen as the children of A.
Basic terminology
o Root Node: - The root node is the topmost node in the tree
hierarchy. In other words, the root node is the one which doesn't
have any parent.
o Sub Tree: - If the root node is not null, the tree T1, T2 and T3 is
called sub-trees of the root node.
o Leaf Node: - The node of tree, which doesn't have any child node,
is called leaf node. Leaf node is the bottom most node of the tree.
There can be any number of leaf nodes present in a general tree.
Leaf nodes can also be called external nodes.
General Tree
A Tree in which each node having either 0 or more child nodes is
called general tree.
In General Tree, each node can have infinite number of children. This tree
is the super-set of all other types of trees.
2
Page
Binary Tree
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.
1. Linked Representation
In the above figure, a tree is seen as the collection of nodes where each
node contains three parts : left pointer, data element and right pointer.
3
Left pointer stores the address of the left child while the right pointer
Page
stores the address of the right child. The leaf node contains null in its left
and right pointers.
Location number of an array is used to store the size of the tree. The first
index of an array that is '0', stores the total number of nodes. All nodes
are numbered from left to right level by level from top to bottom. In a
tree, each node having an index i is put into the array as its ith element.
Value '7' is the total number of nodes. If any node does not have any of
its child, null value is stored at the corresponding index of the array.
Tree Traversal
Tree Traversal refers to the process of visiting each node in a tree data
structure exactly once.
1. Preorder Traversal
2. Inorder Traversal
3. Postorder Traversal
1. Preorder Traversal
In this traversal, the root node is visited first, then its left child and later
its right child.
Algorithm-
2. Traverse the left sub tree i.e. call Preorder (left sub tree)
3. Traverse the right sub tree i.e. call Preorder (right sub tree)
Example-
Traverse the entire tree starting from the root node keeping yourself to
Page
the left.
2. Inorder Traversal
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.
Algorithm-
1. Traverse the left sub tree i.e. call Inorder (left sub tree)
3. Traverse the right sub tree i.e. call Inorder (right sub tree)
Example-
Keep a plane mirror horizontally at the bottom of the tree and take the
projection of all the nodes.
6
Page
3. Postorder Traversal
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 node
is visited.
Algorithm-
1. Traverse the left sub tree i.e. call Postorder (left sub tree)
2. Traverse the right sub tree i.e. call Postorder (right sub tree)
Example-
7
Page
Postorder Traversal Shortcut
• Breadth First Traversal of a tree prints all the nodes of a tree level
by level.
Example-
1. In a binary search tree(BST), the value of all the nodes in the left
sub-tree is less than the value of the root.
8
Page
2. Similarly, value of all the nodes in the right sub-tree is greater than
or equal to the value of the root.
3. This rule will be recursively applied to all the left and right sub-trees
of the root.
Example-
Insert 50-
Insert 70-
Insert 20-
Insert 90-
Insert 40-
Insert 100-
1. Search Operation
2. Insertion Operation
3. Deletion Operation
Search Operation
Search Operation is performed to search a particular element in the
Binary Search Tree.
Rules-
1. Compare the element with the root of the tree.
2. If the item is matched then return the location of the node.
3. Otherwise check if item is less than the element present on root, if
so then move to the left sub-tree.
4. If not, then move to the right sub-tree.
5. Repeat this procedure recursively until match found.
6. If element is not found then return NULL.
Example-
Consider key = 45 has to be searched in the given BST-
Rules-
The insertion of a new key always takes place as the child of some 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-
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-
Just remove / disconnect the leaf node that is to be deleted from the tree.
Example-
Just make the child of the deleting node, the child of its grandparent.
Example-
A node with two children may be deleted from the BST in the following
two ways-
Method-01:
Example-
Example-
Consider the following example where node with value = 15 is deleted
from the BST-
Heap Tree
Heap is a tree-based data structure in which all nodes in the tree are in
the specific order.
There are two types of heap data structures and they are as follows...
1. Max Heap
2. Min Heap
1. Max Heap
Max heap is a specialized full binary tree in which every parent node
contains greater or equal value than its child nodes.
2. Min-Heap –In mean heap, the value of the root node is less than
or equal to either of its children.
AVL Tree:
16
AVL tree is a height balanced binary search tree. That means, an AVL tree
Page
The AVL tree was introduced in the year 1962 by G.M. Adelson-Velsky
and E.M. Landis.
Example:
17
Page
The above tree is a binary search tree and every node is satisfying
balance factor condition. So this tree is said to be an AVL tree.
Every AVL Tree is a binary search tree but every Binary Search Tree need not
be AVL tree.
M-Way
Way Search Tree
The m-way search trees are multi multi-way
way trees which are generalised
versions of binary trees,, where each node contains multiple elements.
In an m-Way
Way tree of order m,, each node contains a maximum of
m – 1 elements (key) and m childrens (pointers).
M-way
way Search tree Properties:
Properties
Representation of M-way
M Search tree:
In the structure, P0, P1, P2, .... , Pn are pointers to the node’s sub-trees
sub and
K0, K1, K2, .... , Kn-1 are the key values of the node. All the key values are
stored in ascending order. That is Ki < Ki+1 for 0 ≤ i ≤ n-2.
Example of 3-way
way search tree:
18
Page
UNIT-4
What is sorting?
Importance of sorting
Bubble Sort
In Bubble sort, Each element of the array is compared with its adjacent
element. The algorithm processes the list in passes. A list with n elements
requires n-1 passes for sorting.
Algorithm :
o Step 1: Repeat Step 2 For i = 0 to N-1
o Step 2: Repeat For J = i + 1 to N - 1
o Step 3: IF A[J] > A[i]
SWAP A[J] and A[i]
[END OF INNER LOOP]
[END OF OUTER LOOP
o Step 4: EXIT
Example 2:
Exchange Sort
Example:
Let's examine our same table of elements again using an exchange sort for
descending order.
Remember, a "pass" is defined as one full trip through the array comparing and if
necessary, swapping elements
Array at beginning: 84 69 76 86 94 91
After Pass #1: 94 69 76 84 86 91
After Pass #2: 94 91 69 76 84 86
After Pass #3: 94 91 86 69 76 84
After Pass #4: 94 91 86 84 69 76
After Pass #5 (done): 94 91 86 84 76 69
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.
Algorithm:
Step 1 - Select the first element of the list (i.e., Element at first position
in the list).
Step 2: Compare the selected element with all the other elements in the
21
list.
Page
Step 3: In every comparison, if any element is found smaller than the
selected element (for Ascending order), then both are swapped.
Step 4: Repeat the same procedure with element in the next position in
the list till the entire list is sorted.
Example:
Insertion Sort
Insertion sort works in the similar way as we sort cards in our hand in a
card game.
This is less efficient than the other sort algorithms like quick sort, merge
sort, etc.
Algorithm:
Step 1 - Assume that first element in the list is in sorted portion and all
the remaining elements are in unsorted portion.
Step 2: Take first element from the unsorted portion and insert that
element into the sorted portion in the order specified.
Step 3: Repeat the above process until all the elements from the
unsorted portion are moved into the sorted portion.
22
Page
23
Page
Example:
Merge Sort
Merge sort is one of the most efficient sorting algorithms. It works on the
principle of Divide and Conquer.
Merge sort repeatedly breaks down a list into several sublists until each
sublist consists of a single element and merging those sublists in a
manner that results into a sorted list.
4. Once we have divided the main array into subarrays with single
Page
Quick sort is a fast sorting algorithm used to sort a list of elements. Quick
sort algorithm is invented by C. A. R. Hoare.
The quick sort algorithm attempts to separate the list of elements into
two parts and then sort each part recursively. That means it use divide
and conquer strategy. In quick sort, the partition of the list is performed
based on the element called pivot. Here pivot element is one of the
elements in the list.
The list is divided into two partitions such that "all elements to the left
of pivot are smaller than the pivot and all elements to the right of
pivot are greater than or equal to the pivot".
• Step 2 - Define two variables i and j. Set i and j to first and last
elements of the list respectively.
Example:
26
Page
Page 27
Radix Sort
Radix sort is one of the sorting algorithms used to sort a list of integer
numbers in order. In radix sort algorithm, a list of integer numbers will be
sorted based on the digits of individual numbers. Sorting is performed
from least significant digit to the most significant digit.
Radix sort algorithm requires the number of passes which are equal to the
number of digits present in the largest number among the list of
numbers. For example, if the largest number is a 3 digit number then that
list is sorted with 3 passes.
Step 1 - Define 10 queues each representing a bucket for each digit from
0 to 9.
Step 2 - Consider the least significant digit of each number in the list
which is to be sorted.
Step 3 - Insert each number into their respective queue based on the
least significant digit.
Step 4 - Group all the numbers from queue 0 to queue 9 in the order they
have inserted into their respective queues.
Step 5 - Repeat from step 3 based on the next least significant digit.
Step 6 - Repeat from step 2 until all the numbers are grouped based on
the most significant digit.
Example:
28
Page
Heap Sort
Heap sort is one of the sorting algorithms used to arrange a list of
elements in order. Heapsort algorithm uses one of the tree concepts
called Heap Tree.
order.
Page
Step by Step Process
Descending----(Max Heap)
Ascending----(Min Heap)
Step 2 - Delete root node and replace it with last leaf node of tree.
Step-1:
Spep-2: Delete root node (80) and replace it with the last leaf node (3).
Step 3:
30
Page
Step-4: Heapify Tree
Delete root node (45) and replace it with the last leaf node (25).
Heapify Tree
Delete root node (33) and replace it with the last leaf node (17).
31
Delete root node (17) and replace it with the last leaf node (3).
Now we have a single element left in heap tree, So directly We put into
the sorted list.
What is Search?
Search is a process of finding a value in a list of values. In other words,
searching is the process of locating given value position in a list of values.
32
Page
Linear Search
Linear search is a very basic and simple search algorithm. In Linear
search, we search an element or value in a given array by traversing the
array from the starting, till the desired element or value is found.
It compares the element to be searched with all the elements present in
the array and when the element is matched successfully, it returns the
index of the element in the array, else it return -1.
Linear Search is applied on unsorted or unordered lists, when there are
fewer elements in a list.
Linear Search Algorithm:
Step 2 - Compare the search element with the first element in the list.
Step 3 - If both are matched, then display "Given element is found!!!" and
terminate the function
Step 4 - If both are not matched, then compare search element with the
next element in the list.
Step 5 - Repeat steps 3 and 4 until search element is compared with last
element in the list.
Step 6 - If last element in the list also doesn't match, then display
"Element is not found!!!" and terminate the function.
Example
Consider the following list of elements and the element to be searched...
33
Page
Page 34
Binary Search
Binary search is a fast search algorithm. This search algorithm works on
the principle of divide and conquer. Binary Search is used with sorted
array or list.
Binary Search Algorithm:
Step 3 - Compare the search element with the middle element in the
sorted list.
Step 5 - If both are not matched, then check whether the search element
is smaller or larger than the middle element.
Step 8 - Repeat the same process until we find the search element in the
list or until sublist contains only one element.
Step 9 - If that element also doesn't match with the search element, then
display "Element is not found in the list!!!" and terminate the function.
Example
Consider the following list of elements and the element to be searched...
35
Page
Page 36