0% found this document useful (0 votes)
4 views

Data Structure Algorithm Lecture_02

The document provides an overview of various tree and graph data structures, including Binary Trees, Binary Search Trees, AVL Trees, and B-Trees, along with their properties and applications. It also covers graph terminology, types of graphs, and methods for graph representation and traversal techniques such as Depth First Search (DFS) and Breadth First Search (BFS). Additionally, it discusses Polish notation and conversions between infix, prefix, and postfix expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data Structure Algorithm Lecture_02

The document provides an overview of various tree and graph data structures, including Binary Trees, Binary Search Trees, AVL Trees, and B-Trees, along with their properties and applications. It also covers graph terminology, types of graphs, and methods for graph representation and traversal techniques such as Depth First Search (DFS) and Breadth First Search (BFS). Additionally, it discusses Polish notation and conversions between infix, prefix, and postfix expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Data Structure and Algorithm Lecture-02

Md. Sohag Hossain


Maintenance Engineer (Principal Officer)
Rajshahi Krishi Unnayan Bank (RAKUB)

Types of Tree Data Structure

The following are the different types of trees data structures:

 Binary Tree
 Binary Search Tree (BST)
 AVL Tree
 B-Tree

Binary Tree

A binary tree is a tree data structure in which each node can have 0, 1, or 2 children – left and right
child.

Properties of a Binary tree

 The maximum number of nodes at any level ‘L’ in a binary tree is 2


 The minimum number of nodes in a binary tree of height H is H + 1
 The maximum number of nodes in a binary tree of height H is 2H+1 – 1
 Total Number of leaf nodes in a Binary Tree = Total Number of nodes with two children + 1
 The maximum number of nodes at each level of i is 2i.
 Searching operation takes O(log2N)

Binary trees can be divided into the following types:

 Perfect binary tree: Every internal node has two child nodes. All the leaf nodes are at the same
level.
 Full binary tree: Every parent node or an internal node has either exactly two children or no child
nodes.
 Complete binary tree: All levels except the last one are full of nodes.
 Degenerate binary tree: All the internal nodes have only one child.
 Balanced binary tree: The left and right trees differ by either 0 or 1.

Applications of Binary trees

 Decision Tree – Machine learning Algorithm

A supervised learning algorithm is used both in the case of a classification or regression-based


problem. It starts with a root node and ends with a decision or prediction made by leaves. All the
nodes in the tree represent a condition based on which the split occurs.

 Working with Morse Code

The organization of Morse code is done in the form of a binary tree

 Binary Expression Trees

A binary tree is used to evaluate an expression. The operators are stored at the interior node and the
operands are stored at the leaf node.
Binary Search Tree (BST)

A binary search tree (BST) is also called an ordered or sorted binary tree in which the value at the
left sub-tree is lesser than that of the root and the right sub-tree has a value greater than that of the
root.

Every binary search tree is a binary tree. However, not every binary tree is a binary search tree.
What’s the difference between a binary tree and a binary search tree? The most important difference
between the two is that in a BST, the left child node’s value must be less than the parent’s while the
right child node’s value must be higher.

AVL Tree

AVL trees are a special kind of self-balancing binary search tree where the height of every node’s
left and right sub-tree differs by at most one.

An AVL tree is given in the following figure. We can see that, balance factor associated with each node is
in between -1 and +1. Therefore, it is an example of AVL tree.
Applications of AVL trees

 In-memory sorts of sets and dictionaries


 Database applications that require frequent lookups for data

১। (ক) B-tree data structure কী? এর প্রয় োগ ব্যোখ্যো করুন।

B-Tree

B tree is a self-balancing search tree wherein each node can contain more than one key and more
than two children. It is a special type of m-way tree and a generalized binary search tree. B-tree can
store many keys in a single node and can have multiple child nodes. This reduces the height and
enables faster disk access.

Properties of a B-Tree

 Every node contains at most m children.


 Every node contains at least m/2 children (except the root node and the leaf node).
 The root nodes should have a minimum of 2 nodes.
 All leaf nodes should be at the same level.

Application of B-trees

 Databases and file systems


 Multilevel indexing
 For quick access to the actual data stored on the disks
 To store blocks of data

B+ Tree
B+ Tree is an extension of B Tree which allows efficient insertion, deletion and search operations.

In B Tree, Keys and records both can be stored in the internal as well as leaf nodes. Whereas, in
B+ tree, records (data) can only be stored on the leaf nodes while internal nodes can only store the
key values.

The leaf nodes of a B+ tree are linked together in the form of a singly linked lists to make the
search queries more efficient.

B+ Tree are used to store the large amount of data which cannot be stored in the main memory.
Due to the fact that, size of main memory is always limited, the internal nodes (keys to access
records) of the B+ tree are stored in the main memory whereas, leaf nodes are stored in the
secondary memory.

The internal nodes of B+ tree are often called index nodes. A B+ tree of order 3 is shown in the
following figure.
Advantages of B+ Tree
1. Records can be fetched in equal number of disk accesses.
2. Height of the tree remains balanced and less as compare to B tree.
3. We can access the data stored in a B+ tree sequentially as well as directly.
4. Keys are used for indexing.
5. Faster search queries as the data is stored only on the leaf nodes.

B Tree VS B+ Tree

SN B Tree B+ Tree

1 Search keys cannot be repeatedly stored. Redundant search keys can be present.

2 Data can be stored in leaf nodes as well as Data can only be stored on the leaf
internal nodes nodes.

3 Searching for some data is a slower Searching is comparatively faster as


process since data can be found on data can only be found on the leaf
internal nodes as well as on the leaf nodes. nodes.

4 Deletion of internal nodes are so Deletion will never be a complexes


complicated and time consuming. process since element will always be
deleted from the leaf nodes.

5 Leaf nodes cannot be linked together. Leaf nodes are linked together to make
the search operations more efficient.

Searching Example on a B+ Tree

Let us search k = 45 on the following B+ tree.


B+ tree

1. Compare k with the root node.

k is not found at the root

2. Since k > 25, go to the right child.

Go to right of the root


3. Compare k with 35. Since k > 30, compare k with 45.

k not found

4. Since k ≥ 45, so go to the right child.

go to the right

5. k is found.

k is found

Binary Tree
What is Graph Data Structure?
A Graph is a non-linear data structure consisting of vertices and edges. The vertices
are sometimes also referred to as nodes and the edges are lines or arcs that connect
any two nodes in the graph. More formally a Graph is composed of a set of vertices
( V ) and a set of edges( E ). The graph is denoted by G(E, V).
Components of a Graph
• Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices
are also known as vertex or nodes. Every node/vertex can be labeled or
unlabeled.
• Edges: Edges are drawn or used to connect two nodes of the graph. It can be
ordered pair of nodes in a directed graph. Edges can connect any two nodes
in any possible way. There are no rules. Sometimes, edges are also known as
arcs. Every edge can be labeled/unlabeled.
Polish Notation

Polish notation also known as prefix notation is defined as: In this notation system operator is written
before the operands. Examples of polish notation or prefix notation are +AB and /CD. In these two
examples we can see that here A and B are two operands and + is an operator which is written before
the operands, similarly we can say for the second example.

Reverse Polish Notation


Reverse polish notation is also known as postfix notation is defined as: In postfix notation operator is
written after the operands. Examples of postfix notation are AB + and CD –. Here A and B are two
operands and the operator is written after these two operands.

Prefix, Postfix and Infix Notation


• Infix: The typical mathematical form of expression that we encounter
generally is known as infix notation. In infix form, an operator is written in
between two operands.
• For example:
• An expression in the form of A * ( B + C ) / D is in infix form. This expression
can be simply decoded as: “Add B and C, then multiply the result by A, and
then divide it by D for the final answer.”
• Prefix: In prefix expression, an operator is written before its operands. This
notation is also known as “Polish notation”.
• For example, the above expression can be written in the prefix form as / * A
+ B C D. This type of expression cannot be simply decoded as infix
expressions.
• Postfix: In postfix expression, an operator is written after its operands. This
notation is also known as “Reverse Polish notation”.
• For example, the above expression can be written in the postfix form as A B
C + * D /. This type of expression cannot be simply decoded as infix
expressions.
Infix to Postfix Conversion

(A+B↑D)/ (E-F) +G =T4 +G


= (A+BD↑)/ (E-F) +G =T4G+
T1 Putting the values of T1, T2, T3 and T4
= (A+T1)/ (E-F) +G we get,
= (AT1+) / (EF-) +G = T2T3/G+
T2 T3 =AT1+EF-/G+
=T2/T3 +G =ABD↑+EF-/G+
=T2T3/ +G
T4

Infix to Prefix Conversion

(A+B*D)/(E-F) +G = T4+ G
= (A+ *BD)/ (E-F) +G = +T4G
T1 Putting the values of T1, T2, T3 and
= (A+T1) / (E-F) +G T4 we get,
= (+AT1)/ (-EF) +G = +/T2T3G
T2 T3 =+/+AT1-EFG
= T2/T3 +G = +/+A*BD-EFG
= /T2T3 +G
T4

Graph
A graph can be defined as group of vertices and edges that are used to connect
these vertices. A graph can be seen as a cyclic tree, where the vertices (Nodes)
maintain any complex relationship among them instead of having parent child
relationship.
Definition
A graph G can be defined as an ordered set G(V, E) where V(G) represents the set
of vertices and E(G) represents the set of edges which are used to connect these
vertices.
A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B), (B,C),
(C,E), (E,D), (D,B), (D,A)) is shown in the following figure.

Directed and Undirected Graph


A graph can be directed or undirected. However, in an undirected graph, edges are
not associated with the directions with them. An undirected graph is shown in the
above figure since its edges are not attached with any of the directions. If an edge
exists between vertex A and B then the vertices can be traversed from B to A as
well as A to B.
In a directed graph, edges form an ordered pair. Edges represent a specific path
from some vertex A to another vertex B. Node A is called initial node while node
B is called terminal node.
A directed graph is shown in the following figure.
Graph Terminology
Path
A path can be defined as the sequence of nodes that are followed in order to reach
some terminal node V from the initial node U.
Closed Path
A path will be called as closed path if the initial node is same as terminal node. A
path will be closed path if V0=VN.
Simple Path
If all the nodes of the graph are distinct with an exception V0=VN, then such path P
is called as closed simple path.
Cycle
A cycle can be defined as the path which has no repeated edges or vertices except
the first and last vertices.
Connected Graph
A connected graph is the one in which some path exists between every two vertices
(u, v) in V. There are no isolated nodes in connected graph.
Complete Graph
A complete graph is the one in which every node is connected with all other nodes.
A complete graph contain n(n-1)/2 edges where n is the number of nodes in the
graph.
Weighted Graph
In a weighted graph, each edge is assigned with some data such as length or
weight. The weight of an edge e can be given as w(e) which must be a positive (+)
value indicating the cost of traversing the edge.
Digraph
A digraph is a directed graph in which each edge of the graph is associated with
some direction and the traversing can be done only in the specified direction.
Loop
An edge that is associated with the similar end points can be called as Loop.
Adjacent Nodes
If two nodes u and v are connected via an edge e, then the nodes u and v are called
as neighbours or adjacent nodes.
Degree of the Node
A degree of a node is the number of edges that are connected with that node. A
node with degree 0 is called as isolated node.
Types of Graphs in Data Structure
The most common types of graphs in the data structure are below:
1. Undirected: A graph in which all the edges are bi-directional. The edges do not
point in a specific direction.
2. Directed: A graph in which all the edges are uni-directional. The edges point in
a single direction.

3. Weighted Graph: A graph with a value associated with every edge. The values
corresponding to the edges are called weights. A value in a weighted graph can
represent quantities such as cost, distance, and time, depending on the graph. We
typically use weighted graphs in modelling computer networks.
An edge in a weighted graph is represented as (u, v, w), where:
 u is the source vertex
 v is the destination vertex
 w represents the weight associated with going from u to v

4. Unweighted Graph: A graph with no value or weight associated with the edge.
All the graphs are unweighted by default unless there is a value associated.
An edge of an unweighted graph is represented as (u, v), where:
 u represents the source vertex
 v is the destination vertex

Representation of Graphs
There are two ways to store a graph:
 Adjacency Matrix
 Adjacency List
Adjacency Matrix
In this method, the graph is stored in the form of the 2D matrix where rows and
columns denote vertices. Each entry in the matrix represents the weight of the edge
between those vertices.

Adjacency List
This graph is represented as a collection of linked lists. There is an array of pointer
which points to the edges connected to that vertex.
There are two graph traversal techniques and they are as follows...

1. DFS (Depth First Search)


2. BFS (Breadth First Search)
DFS (Depth First Search)
DFS traversal of a graph produces a spanning tree as final result. Spanning
Tree is a graph without loops. We use Stack data structure with maximum size
of total number of vertices in the graph to implement DFS traversal.

We use the following steps to implement DFS traversal...


 Step 1 - Define a Stack of size total number of vertices in the graph.
 Step 2 - Select any vertex as starting point for traversal. Visit that vertex
and push it on to the Stack.
 Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which
is at the top of stack and push it on to the stack.
 Step 4 - Repeat step 3 until there is no new vertex to be visited from the
vertex which is at the top of the stack.
 Step 5 - When there is no new vertex to visit then use back tracking and
pop one vertex from the stack.
 Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
 Step 7 - When stack becomes Empty, then produce final spanning tree by
removing unused edges from the graph
DFS Example: http://www.btechsmartclass.com/data_structures/graph-traversal-
dfs.html
BFS (Breadth First Search)
BFS traversal of a graph produces a spanning tree as final result. Spanning
Tree is a graph without loops. We use Queue data structure with maximum size
of total number of vertices in the graph to implement BFS traversal.

We use the following steps to implement 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 non-visited adjacent vertices of the vertex which is at
front of the Queue and insert them into the Queue.
 Step 4 - When there is no new vertex to be visited from the vertex which is
at front of the Queue then delete that vertex.
 Step 5 - Repeat steps 3 and 4 until queue becomes empty.
 Step 6 - When queue becomes empty, then produce final spanning tree by
removing unused edges from the graph
Example: http://www.btechsmartclass.com/data_structures/graph-traversal-
bfs.html
Difference Between BFS and DFS:

Parameters BFS DFS

BFS stands for Breadth First


DFS stands for Depth First Search.
Stands for Search.

BFS(Breadth First Search) uses


DFS(Depth First Search) uses Stack
Data Queue data structure for finding
data structure.
Structure the shortest path.

DFS is also a traversal approach in


BFS is a traversal approach in
which the traverse begins at the root
which we first walk through all
node and proceeds through the nodes
nodes on the same level before
as far as possible until we reach the
moving on to the next level.
Definition node with no unvisited nearby nodes.

Conceptual BFS builds the tree level by DFS builds the tree sub-tree by sub-
Difference level. tree.

Approach It works on the concept of FIFO It works on the concept of LIFO


used (First In First Out). (Last In First Out).

BFS is more suitable for


DFS is more suitable when there are
searching vertices closer to the
solutions away from source.
Suitable for given source.

BFS is used in various DFS is used in various applications


applications such as bipartite such as acyclic graphs and finding
Applications graphs, shortest paths, etc. strongly connected components etc.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy