Data Structure Algorithm Lecture_02
Data Structure Algorithm Lecture_02
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.
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.
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
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
Application of B-trees
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.
5 Leaf nodes cannot be linked together. Leaf nodes are linked together to make
the search operations more efficient.
k not found
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.
(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.
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...
Conceptual BFS builds the tree level by DFS builds the tree sub-tree by sub-
Difference level. tree.