M4 Notes - PART-1
M4 Notes - PART-1
TREES: Binary Search trees, Selection Trees, Forests, Representation of Disjoint sets,
Counting Binary Trees,
GRAPHS: The Graph Abstract Data Types, Elementary Graph Operations
For the given data draw a binary search tree and show the array and linked
representation of the same 100, 85, 45, 55, 110 , 20, 70, 65
Operations on a Binary Search Tree
The following operations are performed on a binary search tree...
Search
- Recursive
- Iterative
Insertion
Deletion
Traversal
Recursive search
struct node *search(struct node *root, int key)
{
if(root==NULL)
{
printf("key not found\n");
}
else if(key < root->data) /*search in left subtree*/
return search(root->left, key);
else if(key > root->data) /*search in right subtree*/
return search(root->right,key);
else
printf("Key found\n");
return root;
}
Iterative search
Firstly create a node using malloc function and assign a key value to it.
If tree is empty initially then the newly created node becomes the first
node in the tree.
Deleting a node from Binary search tree has following three cases...
Case 1: Deleting a Leaf node (A node with no children)
Case 2: Deleting a node with one child
Case 3: Deleting a node with two children
Joining and splitting binary tree
1. Databases: Many database systems use BSTs for indexing data, which allows
for fast retrieval of records based on keys.
2. Symbol Tables: Compilers and interpreters use BSTs to store and search for
identifiers (e.g., variable names) efficiently.
3. File Systems: BSTs are employed in file systems to organize directory
structures and quickly locate files.
4. Caches: In computer architecture, BSTs are used in caches to efficiently
manage and search for cached data.
5. Network Routing: Routing tables in network routers often use BSTs for efficient
routing decisions.
Example: -2
Preorder : 5-3-2-4-8-6-10
Inorder : 2-3-4-5-6-8-10
Solution:
Graph
Graph is a pair of sets (V, E), where V is the set of vertices and E is
the set of edges, connecting the pairs of vertices. G=(V,E)
Ex:
Graph Classifications
There are several common kinds of graphs
Weighted or un weighted
Directed or For undirected
Cyclic or acyclic
Multi graphs
Directed Graphs
Digraph: A graph whose edges are directed
o Edge drawn as arrow
o Edge can only be traversed in direction of arrow
o Example: E = {(A,B), (A,C), (A,D), (B,C), (D,C)}
Degree of a Node
The degree of a node is the number of edges incident on it.
In-degree: Number of edges pointing to a node
Out-degree: Number of edges pointing from a node
inDegree of B is 1
Out Degree of b is 1
Self edge(self loops) : A selft loop is an edge which starts and ends
are the same vertex.
Representing graphs:
Adjacency lists
Adjacency matrix
Adjacency lists
BFS Algorithm :
DFS Algorithm :