Module 4
Module 4
MODULE 4
BINARY SEARCH TREE: Binary Search Tree is a binary tree.it may be empty. If it is not
empty then it satisfies the following properties.
1. Each node has exactly one key and keys are distinct.
2. The keys in the left sub-tree are smaller than the key in the root.
3. The keys in the right sub-tree are greater than the key in the root.
4. The left and right sub-tree are also binary search trees.
Example of BST
Searching a Binary Search Tree: Write a recursive C function of a binary Search Tree
Searching a Binary Search Tree: Write a Iteraive C function of a binary Search Tree
else
*node=ptr;
}
}
Deleting a node from a BST:
In a binary search tree, we must delete a node from the tree by keeping in mind that the property
of BST is not violated. To delete a node from BST, there are three possible situations occur -
Case 2: delete a node which contains one leaf(79): in place of parent node leaf node will be
replaced.
After deleting a parent node having one child tree look like as follows.
GRAPHS
A graph is an abstract data structure that is used to implement the mathematical concept of
graphs. It is basically a collection of vertices (also called nodes) and edges that connect these
vertices. A graph is often viewed as a generalization of the tree structure, where instead of
having a purely parent-to-child relationship between tree nodes, any kind of complex
relationship can exist.
Vertices:
Representation:
Graph Classifications
here are several common kinds of graphs
Weighted or unweighted
Directed or undirected
Cyclic or acyclic
Multigraphs
Kinds of Graphs: Weighted and Unweighted :Graphs can be classified by whether or not
their edges have weights
Weighted graph:
edges have a weight
Weight typically shows cost of traversing
Example: weights are distances between cities
Unweighted graph:
edges have no weight Edges simply show connections
Example: course prerequisites
Kinds of Graphs: Directed and Undirected
Graphs can be classified by whether or their edges are have direction
Undirected Graphs: each edge can be traversed in either direction
Directed Graphs: each edge can be traversed only in a specified direction
Undirected Graphs
Undirected Graph: no implied direction on edge between nodes
The example from above is an undirected graph
fig 1
Directed Graphs
Digraph: A graph whose edges are directed (ie have a direction)
fig 2
• A directed graph is strongly connected if every pair of vertices has a path between them,
in both directions Data Structures for Representing Graphs
• Two common data structures for representing graphs:
o Adjacency lists o
Adjacency matrix
• It is easy to follow and clearly shows the adjacent nodes of a particular node.
• It is often used for storing graphs that have a small-to-moderate number of edges. That
is, an adjacency list is preferred for representing sparse graphs in the computer’s
memory; otherwise, an adjacency matrix is a good choice.
• Adding new nodes in G is easy and straightforward when G is represented using an
adjacency list. Adding new nodes in an adjacency matrix is a difficult task, as the size
of the matrix needs to be changed and existing nodes may have to be reordered. Each
node has a list of adjacent nodes
A B C D
B A D
C A D
D A B C
fig 3
0 otherwise
ABCD
A∞111
B ∞∞∞1
C ∞∞∞∞
D∞∞1∞
Disadv:Adjacency matrix representation is easy to represent and feasible as long as the graph is
small and connected. For a large graph ,whose matrix is sparse, adjacency matrix representation
wastes a lot of memory. Hence list representation is preferred over matrix representation.
While breadth-first search uses a queue as an auxiliary data structure to store nodes for further
processing, the depth-first search scheme uses a stack.
Breadth-first search algorithm
Breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores
all the neighbouring nodes. Then for each of those nearest nodes, the algorithm explores their
unexplored neighbour nodes, and so on, until it finds the goal. That is, we start examining the
node A and then all the neighbours of A are examined. In the next step, we examine the
neighbours of neighbours of A, so on and so forth. This means that we need to track the
neighbours of the node and guarantee that every node in the graph is processed and no node is
processed more than once. This is accomplished by using a queue that will hold the nodes that
are waiting for further processing.
Algorithm for BFS traversal
• Step 1: Define a Queue of size total number of vertices in the graph.
• Step 2: Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.
• Step 3: Visit all the adjacent vertices of the verex which is at front of the Queue which
is not visited and insert them into the Queue.
• Step 4: When there is no new vertex to be visit from the vertex at front of the Queue
then delete that vertex from the Queue.
• Step 5: Repeat step 3 and 4 until queue becomes empty.
• Step 6: When queue becomes Empty, then the enqueue or dequeue order gives the BFS
traversal order.
PTO
Applications OF graphs
Graphs are constructed for various types of applications such as:
In circuit networks where points of connection are drawn as vertices and component
wires become the edges of the graph.
In transport networks where stations are drawn as vertices and routes become the edges
of the graph.
In maps that draw cities/states/regions as vertices and adjacency relations as edges.
In program flow analysis where procedures or modules are treated as vertices and calls
to these procedures are drawn as edges of the graph.
Once we have a graph of a particular concept, they can be easily used for finding shortest
paths, project planning, etc.
In flowcharts or control-flow graphs, the statements and conditions in a program are
represented as nodes and the flow of control is represented by the edges.
In state transition diagrams, the nodes are used to represent states and the edges represent
legal moves from one state to the other.
Graphs are also used to draw activity network diagrams. These diagrams are extensively
used as a project management tool to represent the interdependent relationships between
groups, steps, and tasks that have a significant impact on the project.