Cds Unit 5 Notes
Cds Unit 5 Notes
Unit – 5
What are Trees?
Properties of Tree:
Every tree has a specific root node. A root node can cross each tree node. It is called
root, as the tree was the only root. Every child has only one parent, but the parent can
have many children.
1. General Tree
If no constraint is placed on the tree’s hierarchy, a tree is called a general tree. Every
node may have infinite numbers of children in General Tree. The tree is the super-set
of all other trees.
2. Binary Tree
The binary tree is the kind of tree in which most two children can be found for each
parent. The kids are known as the left kid and right kid. This is more popular than most
other trees. When certain constraints and characteristics are applied in a Binary tree,
a number of others such as AVL tree, BST (Binary Search Tree), RBT tree, etc. are
also used. When we move forward, we will explain all these styles in detail.
.
.
3. Binary Search Tree
Binary Search Tree (BST) is a binary tree extension with several optional restrictions.
The left child value of a node should in BST be less than or equal to the parent value,
and the right child value should always be greater than or equal to the parent’s value.
This Binary Search Tree property makes it ideal for search operations since we can
accurately determine at each node whether the value is in the left or right sub-tree.
This is why the Search Tree is named.
4. AVL Tree
5. Red-Black Tree
6. N-ary Tree, etc..,
Tree Terminology
Advantages of Tree
A binary tree is a tree data structure composed of nodes, each of which has at most,
two children, referred to as left and right nodes. The tree starts off with a single node
known as the root.
Definition: 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 binary tree with height h and 2h + 1 - 1 nodes (or 2h leaves) is called a full
binary tree
Example:
1. Insertion:
2. Deletion:
3. Tree traversal:
Another frequently used tree operation is traversal. Tree traversal is the process of
visiting each node present in a tree. There are three methods of tree traversal:
Example:
Implementation:
1. Define a graph.
A graph is a non-linear data structure that represents less relationship between its
adjacent elements. There is no hierarchical relationship between the adjacent elements
in case of graphs.
b. Adjacency list.
1. Start by putting any one of the graph's vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to
the back of the queue.
4. Keep repeating steps 2 and 3 until the queue is empty.
The graph might have two different disconnected parts so to make sure that we cover every vertex,
we can also run the BFS algorithm on every node
BFS example
Let's see how the Breadth First Search algorithm works with an example. We use an undirected
graph with 5 vertices.
We start from vertex 0, the BFS algorithm starts by putting it in the Visited list and putting all its
adjacent vertices in the stack.
Visit
start vertex and add its adjacent vertices to queue
Next, we visit the element at the front of queue i.e. 1 and go to its adjacent nodes. Since 0 has
already been visited, we visit 2 instead.
Visit
the first neighbour of start node 0, which is 1
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the back of the queue and visit 3,
which is at the front of the queue.
4
remains in the queue
Only 4 remains in the queue since the only adjacent node of 3 i.e. 0 is already visited. We visit it.
Visit
last remaining item in the stack to check if it has unvisited neighbours
Since the queue is empty, we have completed the Breadth First Traversal of the graph.
Let's see how the Depth First Search algorithm works with an example. We use an undirected
graph with 5 vertices.
We start from vertex 0, the DFS algorithm starts by putting it in the Visited list and putting all its
adjacent vertices in the stack.
Visit
the element and put it in the visited list
Next, we visit the element at the top of stack i.e. 1 and go to its adjacent nodes. Since 0 has
already been visited, we visit 2 instead.
Visit
the element at the top of stack
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack and visit it.
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack and visit it.
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack and visit it.
After we visit the last element 3, it doesn't have any unvisited adjacent nodes, so we have
completed the Depth First Traversal of the graph.
0 After
we visit the last element 3, it doesn't have any unvisited adjacent nodes, so we have completed the
Depth First Traversal of the graph.
BFS pseudocode:
1. create a queue Q
2. mark v as visited and put v into Q
3. while Q is non-empty
4. remove the head u of Q
5. mark and enqueue all (unvisited) neighbours of u
DFS pseudocode:
1. DFS(G, u)
2. u.visited = true
3. for each v ∈ G.Adj[u]
4. if v.visited == false
a. DFS(G,v)
5. init() {
6. For each u ∈ G
7. u.visited = false
8. For each u ∈ G
9. DFS(G, u)
10. }
1
UNIT 5
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
return 0;
}
Input : 10 4 5 6 2 1 9 7 8
Output : 1 2 3 4 5 6 7 8 9 10
2
UNIT 5
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
d - - 0;
}
}
return 0;
}
4
UNIT 5
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
return 0;
}
5
UNIT 5
Consider for example you want to search for 85 which is the sixth element (index
position 5) in the array. In this case, the recursive calls to the binary search
function will be performed as shown in the figure below.
As you can see the above diagram, the search range becomes smaller and smaller
with each recursive call of binary search function. The first call to binary
search function tries to find the search element in array position 0 to 9. In the second
call you chop the array in two slices and perform the search on elements from 5 to 9.
Search range on the third call is from position 5 to 6.
6
UNIT 5
Program
#include <stdio.h>
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l)/2;
// If the element is present at the middle itself
if (arr[mid] == x) return mid;
// If element is smaller than mid, then it can only be present
// in left subarray
if (arr[mid] > x) return binarySearch(arr, l, mid-1, x);
// Else the element can only be present in right subarray
return binarySearch(arr, mid+1, r, x);
}
// We reach here when element is not present in array
return -1;
}
int main(void)
{
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr)/ sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n-1, x);
(result == -1)? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}