DSA Module3
DSA Module3
==============
* Recursion is the process which comes into existence when a function calls a
copy of itself to work on a smaller problem.
* Any function which calls itself is called recursive function, and such function
calls are called recursive calls.
* Recursion involves several numbers of recursive calls.
* Recursive function performs the tasks by dividing it into the subtasks.
=================
Example:
========
#include <stdio.h>
int fact(int n);
int main()
{
int n,f;
printf("Enter the number whose factorial you want to calculate?");
scanf("%d",&n);
f = fact(n);
printf("factorial = %d",f);
}
int fact(int n)
{
if (n==0)
{
return 0;
}
else if ( n == 1)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
==========================
Example:
========
#include<stdio.h>
int fibonacci(int);
void main ()
{
int n,f;
printf("Enter the value of n?");
scanf("%d",&n);
f = fibonacci(n);
printf("%d",f);
}
int fibonacci (int n)
{
if (n==0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
return fibonacci(n-1)+fibonacci(n-2);
}
}
=====================
Tree Data Structures and its traversal Techniques:
=================================================
A tree is one of the data structures that represent hierarchical data.
Its structure is similar to the real tree, so it is named a Tree.
In this structure, the root is at the top, and its branches are moving in a
downward direction. Therefore, we can say that the Tree data structure is an
efficient way of storing the data in a hierarchical way.
* A tree data structure is a non-linear data structure because it does not store
in a sequential manner. It is a hierarchical structure as elements in a Tree are
arranged in multiple levels.
* In the Tree data structure, the topmost node is known as a root node. Each
node contains some data, and data can be of any type.
* Each node contains some data and the link or reference of other nodes that
can be called children.
Root: The root node is the topmost node in the tree hierarchy. In other words, the
root node is the one that doesn't have any parent.
Child node: If the node is a descendant of any node, then the node is known as a
child node.
Parent: If the node contains any sub-node, then that node is said to be the parent
of that sub-node.
Sibling: The nodes that have the same parent are known as siblings.
Leaf Node:- The node of the tree, which doesn't have any child node, is called a
leaf node. A 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.
Internal nodes: A node has atleast one child node known as an internal node
Ancestor node:- An ancestor of a node is any predecessor node on a path from the
root to that node. The root node doesn't have any ancestors.
The tree is also known as a recursive data structure. A tree can be defined as
recursively because the distinguished node in a tree data structure is known as a
root node.
Number of edges:
=================
If there are n nodes, then there would n-1 edges. Each arrow in the structure
represents the link or path. Each node, except the root node, will have atleast one
incoming link known as an edge. There would be one link for the parent-child
relationship.
Depth of node x:
==================
The depth of node x can be defined as the length of the path from the root to the
node x. One edge contributes one-unit length in the path. So, the depth of node x
can also be defined as the number of edges between the root node and the node x.
The root node has 0 depth.
Height of node x:
===================
The height of node x can be defined as the longest path from the node x to the leaf
node.
Applications of trees
======================
Storing naturally hierarchical data
Organize data
Heap
B-Tree, B+Tree
Routing table
Binary Tree:
==============
Binary name itself suggests two numbers, i.e., 0 and 1. In a binary tree, each node
in a tree can have utmost two child nodes. Here, utmost means whether the node has
0 nodes, 1 node or 2 nodes.
Node Creation:
==============
struct node
{
int data;
struct node *left;
struct node *right;
}
Traversal Techniques:
======================
Inorder
Preorder
Postorder
========================
Binary Tree and its Traversal:
========================
The process of visiting the nodes is known as tree traversal. There are three types
traversals used to visit a node.
Inorder traversal
Preorder traversal
Postorder traversal
#include<stdio.h>
#include<stdlib.h>
struct list
{
int data;
struct list *left;
struct list *right;
};
typedef struct list *TREE;
TREE createlocation()
{
TREE temp;
temp=(TREE)malloc(sizeof(struct list));
return(temp);
}
TREE insert(TREE root,int item) // recursive function
{
TREE temp;
temp=createlocation();
temp->data = item;
if(root == NULL) // binary tree is completely empty
{
temp->left = NULL;
temp->right = NULL;
root = temp;
}
else
{
if(root->data>item)
root->left = insert(root->left,item);
else
root->right = insert(root->right,item);
}
return (root);
}
void inorder(TREE root)
{
if(root!=NULL)
{
inorder(root->left); //left
printf(",%d",root->data); //root
inorder(root->right); //right
}
}
void preorder(TREE root) 10 20 30 40 50 60 70
{
if(root!=NULL)
{
printf(" ,%d",root->data);//root
preorder(root->left); //left
preorder(root->right); //right
}
}
void postorder(TREE root)
{
if(root!=NULL)
{
postorder(root->left); //left
postorder(root->right); //right
printf("%d\t",root->data); //root
}
}
void main()
{
TREE root=NULL;
int choice,item;
for(;;)
{
printf("Enter the Chocie\n");
printf("1:Insert \t 2: inorder \t 3: preorder \t 4: postorder \t 5: Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the item\n"); //10,5,12,7,3,8,6,14,9
scanf("%d",&item);
root=insert(root,item);
break;
case 2: inorder(root);
break;
case 3: preorder(root);
break;
case 4: postorder(root);
break;
case 5: exit(0);
default: printf("invalid option \n");
}
}
}
output:
======
Enter the Chocie
1:Insert 2: inorder 3: preorder 4: postorder 5: Exit
1
Enter the item
10
Enter the Chocie
1:Insert 2: inorder 3: preorder 4: postorder 5: Exit
1
Enter the item
20
Enter the Chocie
1:Insert 2: inorder 3: preorder 4: postorder 5: Exit
1
Enter the item
30
Enter the Chocie
1:Insert 2: inorder 3: preorder 4: postorder 5: Exit
1
Enter the item
40
Enter the Chocie
1:Insert 2: inorder 3: preorder 4: postorder 5: Exit
1
Enter the item
50
Enter the Chocie
1:Insert 2: inorder 3: preorder 4: postorder 5: Exit
1
Enter the item
60
Enter the Chocie
1:Insert 2: inorder 3: preorder 4: postorder 5: Exit
1
Enter the item
70
Enter the Chocie
1:Insert 2: inorder 3: preorder 4: postorder 5: Exit
2
,10,20,30,40,50,60,70Enter the Chocie
1:Insert 2: inorder 3: preorder 4: postorder 5: Exit
2
,10,20,30,40,50,60,70Enter the Chocie
1:Insert 2: inorder 3: preorder 4: postorder 5: Exit
3
,10 ,20 ,30 ,40 ,50 ,60 ,70Enter the Chocie
1:Insert 2: inorder 3: preorder 4: postorder 5: Exit
4
70 60 50 40 30 20 10 Enter the Chocie
1:Insert 2: inorder 3: preorder 4: postorder 5: Exit
=====================