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

Data Structures Module 3

Good note
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Data Structures Module 3

Good note
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Structures

Module-3
Module III: Trees: Concept of Trees, Tree terminologies, Binary tree: Complete
and Extended Binary tree, Expression trees, Representation of Binary Tree,
Traversing Binary Trees – Preorder, Inorder, Postorder. Binary Search Tree (BST):
Search, Insertion and Deletion operations, creating a Binary Search Tree. Graph:
Concept of Graph, Graph terminologies, Graph Traversal – BFS, DFS.
What is Tree? Explain various terms related to Trees
Tree: A Tree is a hierarchical collection of nodes or vertices, where each node
contains data as well as information about successor nodes.
Child Node: The successor node of a node is called its child node. In other
words, in a tree structure, a node coming under another node is its child.
Parent Node: The predecessor node of a node is called its parent. In other
words, in a tree structure, a node coming above another node is its parent. Every
ode will have only a single parent.
Root Node: A special node that has no parent node is called the root node.
Leaf node: A special node that has no child node is called a leaf node.
Siblings: The child nodes of the same parent are called siblings
Internal/external nodes: Leaf nodes are known as external nodes and non-leaf
nodes are known as internal nodes.
Depth: The depth of a vertex is the length of the simple path from the root to
that node.
Height of a tree: The height of a tree is the length of the longest path.
Forest: A forest is a set of disjoint trees.
Ordered tree: A rooted tree is a tree that has a root. An ordered tree is a rooted
tree in which there is an order among the children of each node.
Orchard: An orchard is an ordered set of ordered trees.
What are the four different types of Trees in Data Structure?
1. Ordinary tree: A tree in which there is no restriction on the number of
children or in the order of data in nodes.
2. Binary tree: A tree in which each node has a maximum of 2 child nodes.
When there are two child nodes, one of them is left node and the other
one is right node.

3. Binary Search Tree (BST): A binary tree in which each left sub tree has
data less than the current node and each right sub tree has data greater
than the current node.

4. Heaps: A tree in which all nodes have data greater than their child nodes
What is a full binary tree?
A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children. Full binary tree is also called
strictly binary tree.

What is a perfect binary Tree?


A perfect binary tree is a type of binary tree in which every internal node has
exactly two child nodes and all the leaf nodes are at the same level.
What is a complete binary tree?
A complete binary tree is a binary tree in which every level, except possibly the
last, is completely filled, and all nodes in the last level are as far left as possible.
A perfect tree is therefore always complete but a complete tree is not
necessarily perfect.

What is extended binary tree?


An extended binary tree is a modification of a binary tree where every original
node is transformed into an internal node, and new nodes are added to replace
every empty subtree (i.e., missing child). This results in a structure where every
internal node has exactly two children, and every leaf node in the original tree is
represented by an internal node with two external nodes as its children.
What is an Expression tree
The expression tree is a binary tree in which each external or leaf node
corresponds to the operand and each internal or parent node corresponds to the
operators.

Example: Expression tree for 7+ ((1+8)*3) would be:


Explain how binary trees are represented in memory?
Binary trees can be represented in two ways
1. Using arrays : Every node of a binary tree contains data along with
information about the succeeding left node and right node. It can be
represented in a sigle dimensional array as follows
And the corresponding binary tree is as follows

2. Using self-referential structures


To represent a binary tree using a self referential structure, we have to
define a structure as follows:
struct node{
int data;
struct node *left;
struct node *right;
}
We start with dynamically allocating memory for the root node. As the
binary tree grows, left node and right are allocated memory dynamically.
Explain the three different ways of traversing a binary tree
Explain how to construct a BST with an example
Consider the following data : 25,7,43,12.
We start with the first data (25) and place it in the root node. The second data
(7) is compared with the root node data. It is less than the root node data and is
put in a new node on the left of the root node. The third data (43) is compared
with the root node data. Since it is greater than the root data, it is put in a new
node on the right side of the root node. Now we consider the data 12. When
compared with the root node, it goes to left subtree. Further moving on to the
left subtree, we reach the node having data (7). Since (12) is greater than (7) it
is placed on the right side of (7). Final BST is as follows
What is a graph? Explain different terms related to graphs.
Graph: A graph is a collection of points in the plane, called ‘vertices’ or ‘nodes’.
The vertices are connected by line segments called ‘edges’ or ‘arcs’. Formally ,
a graph G=( V,E) is defined by a pair of two sets : a finite non empty set V of
items called vertices and a set E of pairs of these items called edges.

1. A directed graph (or digraph) is a set of vertices and a collection of


directed edges that each connects an ordered pair of vertices. We say that
a directed edge points from the first vertex in the pair and points to the
second vertex in the pair.

2. If the edges in a graph are undirected, it is called undirected graph.

3. A graph with every pair of its vertices connected by an edge is called a


complete graph.

4. A graph with relatively few edges missing is called a dense graph.

5. A graph with few edges relative to the number of its vertices is called
sparse graph.

6. A weighted graph is a graph whose edges have weights or costs


assigned to them.

7. A graph is said to be connected if every pair of the graph is connected by


an edge.

8. A graph with no cycles is called an acyclic graph. A cycle is a path that


starts and ends at the same vertex and does not traverse the same edge
more than once.

9. Adjacent vertices: Two vertices are said to be adjacent if they have an


edge between them.

10.Adjacent edges: Two edges are said to be adjacent if they have a


common vertex.

11.Degree of a vertex: It is the number vertices adjacent to the vertex.

12.Pendant vertex: A vertex of degree one is called a pendant vertex.

13.Isolated vertex: A vertex of degree zero is said to be an isolated vertex.


14.
15.Loop: An edge is called a loop if both its end points are same.

16.Parallel Edges: Two edges are said to be parallel if their end points are
the same.
Explain the DFS (Depth First Search) method of graph traversal with
an example
The systematic approach used in DFS is to move forward using unvisited
adjacent vertices until no further forward movement is possible. At this stage,
backtrack to the previous vertex and the process is repeated once again.

Example:
Consider the following graph

Step 1: Suppose we start with vertex A. The adjacent vertices are B,D,F,G and
H. Then we mode on to B .
Step 2: Adjacent vertices of B are C,E and F(other than A which has already
been visited). So we move on to C
Step 3: Adjacent vertices of C that are unvisited are D and E. We move on to
D. D has no other adjacent vertex depth-wise.
Step 4: We backtrack to the vertex C and move on to E which is an unvisited
adjacent vertex of C.
Step 5: There are no more adjacent vertices depth-wise.
Step 6: We backtrack to C, since the all adjacent vertices of C has already
been visited, we again backtrack to B.
Step 7: From B, we move on to F which is an unvisited adjacent node.
Step 8: We backtrack to B and again to A. The remaining unvisited adjacent
vertices are G and H.
Step 9: We move on to G and thus to H and the traversal is complete.

Explain the BFS (Breadth First Search) method of graph traversal


with an example
The systematic approach of BFS is to start with a vertex and visit all the
adjacent vertices. Once all the adjacent vertices at level 1 are over, move on
to the next level. When all the vertices of level 2 have been visited, other
vertices from next level are considered. The process is repeated until all the
vertices are finished.

Example:

Consider the graph


Step 1: We start with A. We visit all the adjacent vertices of A in the first
level. ie, we visit A,B,D,F,H, and G .
Step 2: Since all the vertices at level 1 has been visited, the remaining
vertices at level 2 C and E are considered. This completes the BFS traversal of
the given graph.

You might also like