Unit 5 - NC
Unit 5 - NC
Tree is a non-linear data structure which organizes data in a hierarchical structure, and this is a
recursive definition.
OR
A tree is a connected graph without any circuits.
OR
If in a graph, there is one and only one path between every pair of vertices, then graph is called as
a tree.
Example-
Properties-
The important properties of tree data structure are-
• There is one and only one path between every pair of vertices in a tree.
• A tree with n vertices has exactly (n-1) edges.
• A graph is a tree if and only if it is minimally connected.
• Any connected graph with n vertices and (n-1) edges is a tree.
1. Root-
• The first node from where the tree originates is called as a root node.
• In any tree, there must be only one root node.
• We can never have multiple root nodes in a tree data structure.
Example-
Example-
3. Parent-
• The node which has a branch from it to any other node is called as a parent node.
• In other words, the node which has one or more children is called as a parent node.
• In a tree, a parent node can have any number of child nodes.
Example-
Here,
• Node A is the parent of nodes B and C
• Node B is the parent of nodes D, E and F
• Node C is the parent of nodes G and H
• Node E is the parent of nodes I and J
• Node G is the parent of node K
Example-
Here,
• Nodes B and C are the children of node A
• Nodes D, E and F are the children of node B
• Nodes G and H are the children of node C
• Nodes I and J are the children of node E
• Node K is the child of node G
5. Siblings-
• Nodes which belong to the same parent are called as siblings.
• In other words, nodes with the same parent are sibling nodes.
Example-
6. Degree-
• Degree of a node is the total number of children of that node.
• Degree of a tree is the highest degree of a node among all the nodes in the tree.
Example-
Here,
• Degree of node A = 2
• Degree of node B = 3
• Degree of node C = 2
• Degree of node D = 0
• Degree of node E = 2
• Degree of node F = 0
• Degree of node G = 1
• Degree of node H = 0
• Degree of node I = 0
• Degree of node J = 0
• Degree of node K = 0
7. Internal Node-
• The node which has at least one child is called as an internal node.
• Internal nodes are also called as non-terminal nodes.
• Every non-leaf node is an internal node.
8. Leaf Node-
• The node which does not have any child is called as a leaf node.
• Leaf nodes are also called as external nodes or terminal nodes.
Example-
9. Level-
• In a tree, each step from top to bottom is called as level of a tree.
• The level count starts with 1 and increments by 1 at each level or step.
Example-
Here,
• Height of node A = 3
• Height of node B = 2
• Height of node C = 2
• Height of node D = 0
• Height of node E = 1
• Height of node F = 0
• Height of node G = 1
• Height of node H = 0
• Height of node I = 0
• Height of node J = 0
• Height of node K = 0
11. Depth-
• Total number of edges from root node to a particular node is called as depth of that node.
• Depth of a tree is the total number of edges from root node to a leaf node in the longest path.
• Depth of the root node = 0
• The terms “level” and “depth” are used interchangeably.
Here,
• Depth of node A = 0
• Depth of node B = 1
• Depth of node C = 1
• Depth of node D = 2
• Depth of node E = 2
• Depth of node F = 2
• Depth of node G = 2
• Depth of node H = 2
• Depth of node I = 3
• Depth of node J = 3
• Depth of node K = 3
12. Subtree-
• In a tree, each child from a node forms a subtree recursively.
• Every child node forms a subtree on its parent node.
Example-
Binary Tree
Binary trees are a fundamental data structure in computer science, and there are several types
of binary trees, each with specific properties. Let's go over the different types of binary trees
with examples for each.
1. Full Binary Tree (also called a Proper Binary Tree or Strict Binary Tree)
A full binary tree is a tree in which every node has either 0 or 2 children. No node has only
one child.
Example:
In this example, each node either has two children or no children at all, making it a full binary
tree.
A perfect binary tree is a type of full binary tree where all the internal nodes have two children,
and all the leaf nodes are at the same level.
Example:
Here, all the leaf nodes (4, 5, 6, 7) are at the same level, and every internal node has exactly
two children. This is a perfect binary tree.
A complete binary tree is a binary tree in which all the levels are completely filled except
possibly for the last level, which is filled from left to right.
Example:
A balanced binary tree is a binary tree where the height of the left and right subtrees of every
node differs by at most 1.
Example:
A degenerate tree is a tree where each parent node has only one child, making it effectively a
linked list.
Example:
This tree behaves like a linked list, as each node has only one child.
A skewed binary tree is a type of degenerate tree where all nodes are skewed to one side
(either left or right).
In this tree:
- It satisfies the binary search tree property (all left children are smaller, and right children are
larger).
A binary search tree (BST) is a binary tree where each node has a key, and it satisfies the
following properties:
- The left subtree contains only nodes with keys less than the node’s key.
- The right subtree contains only nodes with keys greater than the node’s key.
This is a binary search tree because all nodes follow the property that the left child is smaller
and the right child is larger than the parent node.
9. Height-Balanced Tree (AVL Tree)
An AVL tree is a self-balancing binary search tree where the difference between heights of left
and right subtrees cannot be more than 1 for any node.
Example:
Here, the height difference between the left and right subtrees is at most 1, so it is an AVL tree.
A red-black tree is a balanced binary search tree where each node has an extra bit to store color
(red or black) and satisfies the following properties:
- Red nodes cannot have red children (no two consecutive red nodes).
- Every path from a node to its descendant NULL nodes must have the same number of black
nodes.
A Binary Tree as an Abstract Data Type (ADT) refers to a conceptual structure that defines
the behavior and operations that can be performed on a binary tree. The Binary Tree ADT
describes the properties and the fundamental operations on a binary tree, regardless of how it
is implemented.
• A value or key.
• A pointer/reference to the left child (which is itself a binary tree or NULL if no child).
• A pointer/reference to the right child (which is itself a binary tree or NULL if no child).
In this ADT, nodes can be inserted, deleted, or traversed, and different operations can be
performed based on specific rules.
Binary Tree ADT Operations:
1. Create an Empty Binary Tree:
o Example:
BinaryTree* createBinaryTree() {
return NULL;
}
2. Insert a Node in the Binary Tree:
o Description: Adds a new node to the tree at a specific position based on a rule
(e.g., in the left child or right child of a node).
o Operation: Insert(value) adds a new node with the given value to the binary
tree.
o Example: Inserting 5 into an empty binary tree results in a root node with the
value 5.
o Rules for Placement:
▪ In Binary Search Trees (BSTs), values smaller than the current node go
to the left, and values greater go to the right.
o Operation: Delete(value) removes the node with the specified value while
maintaining the tree structure.
o Example: Deleting the node with value 5 removes it from the tree and
restructures the connections of its children.
o Description: Finds if a node with a given value exists in the binary tree.
o Operation: Search(value) returns true if the node is present and false otherwise.
o Example: Searching for 7 in a tree may traverse several nodes before finding
it.
o Description: Traverses the binary tree in a specific order to visit each node.
▪ In-order Traversal: Visit left subtree, then root, then right subtree.
▪ InOrderTraverse(root)
▪ Post-order Traversal: Visit left subtree, then right subtree, then root.
▪ PostOrderTraverse(root)
▪ LevelOrderTraverse(root)
o Operation: Height() returns the height of the binary tree, which is the length of
the longest path from the root to a leaf node.
o Description: Finds the node with the minimum or maximum value in the tree.
o Operation:
o Description: Counts and returns the total number of nodes in the binary tree.
o Operation: CountNodes() returns the total number of nodes.
o Example: For a tree with three nodes (1, 2, 3), CountNodes() returns 3.
Here's a detailed explanation of the Binary Search Tree (BST) ADT with insertion, search,
delete operations, level-wise display, and implementation in C++.
1. Insert Operation
• Algorithm:
o If the new key is less than the current node’s key, recurse on the left subtree.
o If the new key is greater than the current node’s key, recurse on the right subtree.
o Insert the node at the appropriate position once a NULL (empty spot) is found.
C++ Program
#include <iostream>
#include <queue>
class Node {
public:
int key;
Node(int val) {
key = val;
class BST {
public:
Node* insert(Node* root, int key) {
if (root == NULL) {
return root;
};
• Algorithm:
o If the current node’s key is equal to the target key, return true (found).
o If the target key is less than the current node’s key, recurse on the left subtree.
o If the target key is greater than the current node’s key, recurse on the right
subtree.
o If the node is not found and a NULL is reached, return false.
C++ Program
if (root == NULL) {
return false;
if (root->key == key) {
return true;
} else {
• Algorithm:
o If the node has two children, find the in-order successor (smallest node in the
right subtree), replace the node’s key with the successor’s key, and then delete
the successor.
root = root->left;
return root;
if (root == NULL) {
return root;
} else {
if (root->left == NULL) {
Node* temp = root->right;
delete root;
return temp;
delete root;
return temp;
}
root->key = temp->key;
return root;
}
• Algorithm:
o Start from the root node, enqueue it, and then dequeue a node, printing its value.
C++ Program
queue<Node*> q;
q.push(root);
while (!q.empty()) {
q.pop();
Graphs
A Graph is a non-linear data structure that consists of vertices (nodes) and edges.
A vertex, also called a node, is a point or an object in the Graph, and an edge is used to connect
two vertices with each other. Graphs are non-linear because the data structure allows us to have
different paths to get from one vertex to another, unlike with linear data structures like Arrays
or Linked Lists.
• Social Networks: Each person is a vertex, and relationships (like friendships) are the
edges. Algorithms can suggest potential friends.
• Maps and Navigation: Locations, like a town or bus stops, are stored as vertices, and
roads are stored as edges. Algorithms can find the shortest route between two locations
when stored as a Graph.
• Internet: Can be represented as a Graph, with web pages as vertices and hyperlinks as
edges.
• Biology: Graphs can model systems like neural networks or the spread of diseases.
Types of Graphs
Directed Graphs
A directed graph is defined as a type of graph where the edges have a direction associated with
them.
Directed graphs have several characteristics that make them different from undirected graphs.
Here are some key characteristics of directed graphs:
• Directed edges: In a directed graph, edges have a direction associated with them,
indicating a one-way relationship between vertices.
• Indegree and Outdegree: Each vertex in a directed graph has two different degree
measures: indegree and outdegree. Indegree is the number of incoming edges to a
vertex, while outdegree is the number of outgoing edges from a vertex.
• Cycles: A directed graph can contain cycles, which are paths that start and end at the
same vertex and contain at least one edge. Cycles can be important for understanding
feedback loops or other patterns in the graph.
Undirected Graph
An undirected graph is a type of graph where the edges have no specified direction assigned to
them.
• An undirected graph may contain loops, which are edges that connect a vertex to itself.
• Degree of each vertex is the same as the total no of edges connected to it.
Weighted Graph
A weighted graph is defined as a special type of graph in which the edges are assigned some
weights which represent cost, distance, and many other relative measuring units.
Complete Graph
A complete graph is an undirected graph in which every pair of distinct vertices is connected
by a unique edge. In other words, every vertex in a complete graph is adjacent to all other
vertices. A complete graph is denoted by the symbol K_n, where n is the number of vertices in
the graph.
2. Count of edges: Every vertex in a complete graph has a degree (n-1), where n is the
number of vertices in the graph. So total edges are n*(n-1)/2.
3. Symmetry: Every edge in a complete graph is symmetric with each other, meaning that
it is un-directed and connects two vertices in the same way.
5. Regularity: A complete graph is a regular graph, meaning that every vertex has the
same degree.
Disconnected Graph
A graph is disconnected if at least two vertices of the graph are not connected by a path. If a
graph G is disconnected, then every maximal connected subgraph of G is called a connected
component of the graph G.
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
2. Take the top item of the stack 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 top of the stack.
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.
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack and visit
it.
BFS algorithm
A standard BFS implementation puts each vertex of the graph into one of two categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
The algorithm works as follows:
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.
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 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.
Only 4 remains in the queue since the only adjacent node of 3 i.e. 0 is already visited. We visit
it.
Prims Algorithm
A group of edges that connects two sets of vertices in a graph is called cut in graph theory. So,
at every step of Prim’s algorithm, find a cut, pick the minimum weight edge from the cut, and
include this vertex in MST Set (the set that contains already included vertices).
The working of Prim’s algorithm can be described by using the following steps:
Step 2: Follow steps 3 to 5 till there are vertices that are not included in the MST (known as
fringe vertex).
Step 3: Find edges connecting any tree vertex with the fringe vertices.
Step 5: Add the chosen edge to the MST if it does not form any cycle.
Step 6: Return the MST and exit
Consider the following graph as an example for which we need to find the Minimum Spanning
Tree (MST).
Example of a graph
Step 1: Firstly, we select an arbitrary vertex that acts as the starting vertex of the Minimum
Spanning Tree. Here we have selected vertex 0 as the starting vertex.
Step 4: The edges that connect the incomplete MST with the fringe vertices are {1, 2}, {7, 6}
and {7, 8}. Add the edge {7, 6} and the vertex 6 in the MST as it has the least weight (i.e., 1).
Step 5: The connecting edges now are {7, 8}, {1, 2}, {6, 8} and {6, 5}. Include edge {6, 5} and
vertex 5 in the MST as the edge has the minimum weight (i.e., 2) among them.
Step 7: The connecting edges between the incomplete MST and the other edges are {2, 8}, {2,
3}, {5, 3} and {5, 4}. The edge with minimum weight is edge {2, 8} which has weight 2. So
include this edge and the vertex 8 in the MST.
Note: If we had selected the edge {1, 2} in the third step then the MST would look like the
following.
Structure of the alternate MST if we had selected edge {1, 2} in the MST
Below are the steps for finding MST using Kruskal’s algorithm:
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far.
If the cycle is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.
Illustration:
Input Graph:
The graph contains 9 vertices and 14 edges. So, the minimum spanning tree formed will be
having (9 – 1) = 8 edges.
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
8 0 7
8 1 2
9 3 4
10 5 4
11 1 7
14 3 5
Now pick all edges one by one from the sorted list of edges
Step 7: Pick edge 7-8. Since including this edge results in the cycle, discard it. Pick edge 0-7.
No cycle is formed, include it.
Step 8: Pick edge 1-2. Since including this edge results in the cycle, discard it. Pick edge 3-4.
No cycle is formed, include it.
Since the number of edges included in the MST equals to (V – 1), so the algorithm stops
here.
Dijkstra’s Algorithm
Given a weighted graph and a source vertex in the graph, find the shortest paths from the
source to all the other vertices in the given graph.
Algorithm:
• Create a set sptSet (shortest path tree set) that keeps track of vertices included in the
shortest path tree, i.e., whose minimum distance from the source is calculated and
finalized. Initially, this set is empty.
• Assign a distance value to all vertices in the input graph. Initialize all distance values
as INFINITE. Assign the distance value as 0 for the source vertex so that it is picked
first.
o Include u to sptSet .
o For every adjacent vertex v, if the sum of the distance value of u (from
source) and weight of edge u-v, is less than the distance value of v , then
update the distance value of v .
Note: We use a boolean array sptSet[] to represent the set of vertices included in SPT . If a
value sptSet[v] is true, then vertex v is included in SPT, otherwise not. Array dist[] is used to
store the shortest distance values of all vertices.
Example:
To understand the Dijkstra’s Algorithm let’s take a graph and find the shortest path from source
to all nodes.
• The set sptSet is initially empty and distances assigned to vertices are {0, INF, INF, INF,
INF, INF, INF, INF} where INF indicates infinite.
• Now pick the vertex with a minimum distance value. The vertex 0 is picked, include it
in sptSet . So sptSet becomes {0}. After including 0 to sptSet , update distance values
of its adjacent vertices.
• Adjacent vertices of 0 are 1 and 7. The distance values of 1 and 7 are updated as 4 and
8.
The following subgraph shows vertices and their distance values, only the vertices with finite
distance values are shown. The vertices included in SPT are shown in green colour.
Step 2:
• Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET ). The vertex 1 is picked and added to sptSet .
• So sptSet now becomes {0, 1}. Update the distance values of adjacent vertices of 1.
Step 3:
• Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET ). Vertex 7 is picked. So sptSet now becomes {0, 1, 7}.
• Update the distance values of adjacent vertices of 7. The distance value of vertex 6 and
8 becomes finite ( 15 and 9 respectively).
Step 4:
• Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET ). Vertex 6 is picked. So sptSet now becomes {0, 1, 7, 6} .
• Update the distance values of adjacent vertices of 6. The distance value of vertex 5 and
8 are updated.
n-gl.com