Professional Documents
Culture Documents
Unit 6
Unit 6
2. Edge
It is the link between any two nodes.
3. Root
It is the topmost node of a tree.
4. Height of a Node
The height of a node is the number of edges from the node
to the deepest leaf (ie. the longest path from the node to a
leaf node).
5. Depth of a Node
The depth of a node is the number of edges from the root to
the node.
6. Height of a Tree
The height of a Tree is the height of the root node or the
depth of the deepest node.
7. Degree of a Node
The degree of a node is the total number of branches of
that node.
8. Forest
A collection of disjoint trees is called a forest.You
can create a forest by cutting the root of a tree.
struct node {
int data;
struct node* left;
struct node*
right;
}
The struct node pointed to by left and right might have
other left and right children so we should think of them as
sub-trees instead of sub-nodes.
According to this structure, every tree is a combination of
➢ A node carrying data
➢ Two subtrees
inorder(root->left)
display(root->data)
inorder(root->right)
Preorder traversal
display(root->data)
preorder(root->left)
preorder(root->right)
POSTORDER TRAVERSAL
postorder(root->left)
postorder(root->right)
display(root->data)
TYPES OF TREE
1. Binary Tree
2. Binary Search
Tree 3. AVL Tree
4. B-Tree
1. Binary Tree
In a binary tree, every node can have either 0
children or 1 child or 2 children but not more than 2
children.
Example
There are different types of binary trees and they are…
➢ 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.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node
*right;
};
/* newNode() allocates a new node with the given data and
NULL left and
right pointers. */
struct node* newNode(int data)
{
// Allocate memory for new node
struct node* node = (struct node*)malloc(sizeof(struct node));
//OUTPUT
123
BINARY SEARCH TREE(BST)
● Binary search tree is a data structure that quickly allows
us to maintain a sorted list of numbers.
● It is called a binary tree because each tree node has a
maximum of two children.
● It is called a search tree because it can be used to search
for the presence of a number in O(log(n)) time.
● The properties that separate a binary search tree from a
regular binary tree is
○ All nodes of left subtree are less than the root node
○ All nodes of right subtree are more than the root node
○ Both subtrees of each node are also BSTs i.e. they have
the above two properties
A tree having a right subtree with one value smaller than the root is shown to
demonstrate that it is not a valid binary search tree
The binary tree on the right isn't a binary search tree because the right subtree of the node
"3" contains a value smaller than it.
Binary Search Tree Operations-
Rules-
For searching a given key in the BST,
If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
return search(root->right)
2. Insertion Operation-
Insertion Operation is performed to insert an element
in the Binary Search Tree.
Rules-
1) The insertion of a new key always takes place as the
child of some leaf node.
2) For finding out the suitable leaf node,
If node == NULL
return createNode(data)
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
3. Deletion Operation-
When it comes to deleting a node from the binary search tree, following
three cases are possible-
Example-
Consider the following example where node with value = 15
is deleted from the BST-
Method-02:
Method-02:
child is given by 2i + 2.
rightChildIndex as largest.
MaxHeap(array, size)
loop from the first index of non-leaf node down to zero call
heapify
For Min Heap, the above algorithm is modified so that parentNode is always
smaller than newNode.
DELETE ELEMENT FROM HEAP
ALGORITHM FOR DELETION IN MAX HEAP
If nodeToBeDeleted is the leafNode
remove the node
Else swap nodeToBeDeleted with the lastLeafNode
remove noteToBeDeleted
return rootNode
EXTRACT-MAX/MIN
➢ 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.
AVL Tree Rotations
In LL Rotation, every node moves one position to left from the current
position.
Single Right Rotation (RR Rotation)
In RR Rotation, every node moves one position to right from the current
position.
Left Right Rotation (LR Rotation)
1. Search
2. Insertion
3. Deletion
SEARCH OPERATION IN AVL TREE
In an AVL tree, the search operation is performed with O(log n) time complexity. The search
operation in the AVL tree is similar to the search operation in a Binary search tree.
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with the value of root node in the tree.
Step 3 - If both are matched, then display "Given node is found!!!" and terminate the function
Step 4 - If both are not matched, then check whether search element is smaller or larger than
that node value.
Step 5 - If search element is smaller, then continue the search process in left
subtree.
Step 6 - If search element is larger, then continue the search process in right
subtree.
Step 7 - Repeat the same until we find the exact element or until the search element is
compared with the leaf node.
Step 8 - If we reach to the node having the value equal to the search value, then display
"Element is found" and terminate the function.
Step 9 - If we reach to the leaf node and if it is also not matched with the search element, then
display "Element is not found" and terminate the function.
INSERTION OPERATION IN AVL TREE
In an AVL tree, the insertion operation is performed with O(log n) time complexity. In
AVL Tree, a new node is always inserted as a leaf node. The insertion operation is
performed as follows...
Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.
Step 2 - After insertion, check the Balance Factor of every node.
Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next
operation.
Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said
to be imbalanced. In this case, perform suitable Rotation to make it balanced and go
for next operation.
EXAMPLE: CONSTRUCT AN AVL TREE BY
INSERTING NUMBERS FROM 1 TO 8.
DELETION OPERATION IN AVL TREE
In the graph,
V = {0, 1, 2, 3}
E = {(0,1), (0,2), (0,3), (1,2)}
G = {V, E}
GRAPH TERMINOLOGY
1. Adjacency Matrix
2. Adjacency List
1. ADJACENCY MATRIX
➢ An adjacency matrix is a 2D array of V x V vertices.
➢ Each row and column represent a vertex.
➢ If the value of any element a[i][j] is 1, it represents that
there is an edge connecting vertex i and vertex j.
➢ The adjacency matrix for the graph we created above is
➢ Since it is an undirected graph, for edge (0,2), we also
need to mark edge (2,0); making the adjacency matrix
symmetric about the diagonal.
➢ Edge lookup(checking if an edge exists between vertex
A and vertex B) is extremely fast in adjacency matrix
representation but we have to reserve space for every
possible link between all vertices(V x V), so it requires
more space.
2. ADJACENCY LIST REPRESENTATION
➢ An adjacency list represents a graph as an array of linked lists.
➢ The index of the array represents a vertex and each element in
its linked list represents the other vertices that form an edge
with the vertex.
➢ The adjacency list for the graph we made in the first example is
as follows:
➢ An adjacency list is efficient in terms of storage because we only
need to store the values for the edges. For a graph with millions
of vertices, this can mean a lot of saved space.
GRAPH OPERATIONS
Normal graph
Some of the possible spanning trees that can be
created from the above graph are:
MINIMUM SPANNING TREE
➢ A minimum spanning tree is a spanning tree in
which the sum of the weight of the edges is as
minimum as possible.
➢ Example of a Spanning Tree
➢ Let's understand the above definition with the help of
the example below.
➢ The initial graph is:
Weighted graph
THE POSSIBLE SPANNING TREES FROM THE ABOVE
GRAPH ARE:
THE MINIMUM SPANNING TREE FROM THE
ABOVE SPANNING TREES IS:
GRAPH TRAVERSAL - BFS & DFS
➢ Graph traversal is a technique used for searching
a vertex in a graph.
➢ The graph traversal is also used to decide the order of
vertices is visited in the search process.
➢ A graph traversal finds the edges to be used in
the search process without creating loops.
➢ That means using graph traversal we visit all the
vertices of the graph without getting into looping
path.
There are two graph traversal techniques and they are as
follows...
Fill each cell with the distance between ith and jth vertex
2. Now, create a matrix A1 using matrix A0. The elements in
the first column and the first row are left as they are. The
remaining cells are filled in the following way.
For example: For A1[2, 4], the direct distance from vertex 2 to 4 is 4 and
the sum of the distance from vertex 2 to 4 through vertex (ie. from vertex 2
to 1 and from vertex 1 to 4) is 7. Since 4 < 7, A0[2, 4] is filled with 4.
3. Similarly, A2 is created using A3. The elements in the
second column and the second row are left as they are.
Calculate the distance from the source vertex to destination vertex through this vertex 2
4. Similarly, A^3 and A^4 is also created.
Calculate the distance from the source vertex to destination vertex through this
vertex 3
Calculate the distance from the source vertex to destination vertex through
this vertex 4
Time Complexity
There are three loops. Each loop has
constant complexities. So, the time
complexity of the Floyd-Warshall algorithm
is O(n^3).
Space Complexity
The space complexity of the Floyd-Warshall algorithm
is O(n^2).
FLOYD WARSHALL ALGORITHM APPLICATIONS