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

Module 4 UQ

Uploaded by

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

Module 4 UQ

Uploaded by

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

MODULE - 4

1. Write the output of inorder, preorder & postorder traversals on the following tree.
2. Differentiate between complete binary tree and full binary tree. Give examples for each.

Fully Binary Tree

Complete Binary Tree


3. Show the structure of the binary search tree after adding each of the following values in that
order: 10, 25, 2, 4, 7, 13.
i) Illustrate how the element 2 can be deleted from the resultant tree
ii) What is the height of the final tree?
4. Explain Max Heap with an example.
A max-heap is a complete binary tree in which the value in each internal node is greater than or
equal to the values in the children of that node.

5. Explain any two graph representation methods with example for each.
Graph Representations
Graph data structure is represented using following representations
1. Adjacency Matrix
2. Adjacency List

1.Adjacency Matrix
In this representation, graph can be represented using a matrix of size total number of vertices
by total number of vertices; means if a graph with 4 vertices can be represented using a matrix
of 4X4 size. In this matrix, rows and columns both represent vertices.
This matrix is filled with either 1 or 0. Here, 1 represents there is an edge from row vertex to
column vertex and 0 represents there is no edge from row vertex to column vertex.
Adjacency Matrix : let G = (V, E) with n vertices, n >=1. The adjacency matrix of G is a 2-
dimensional n x n matrix, A, A(i, j) = 1 iff (vi, vj) ∈E(G) (<vi, vj> for a diagraph), A(i, j) = 0
otherwise.
example : for undirected graph

For a Directed graph

2. Adjacency List
In this representation, every vertex of graph contains list of its adjacent vertices. The n rows of
the adjacency matrix are represented as n chains. The nodes in chain I represent the vertices
that are adjacent to vertex i. It can be represented in two forms. In one form, array is used to
store n vertices and chain is used to store its adjacencies.
Example:
6. Show the structure of the binary search tree after adding each of the following values in
that order: 2, 5, 1, 7, 10, 9, 11, 6. What is the height of the created tree?
7. Create a Binary Search Tree for the following values 62, 14, 96, 12, 105, 3, 75, 22, 87, 32,
20, 13, 102, 68, 125.

8. Explain the term Complete Graph with an example


A complete graph is a graph in which each vertex is connected to every other vertex. That is,
a complete graph is an undirected graph where every pair of distinct vertices is connected by a
unique edge.
9. How can you represent a Binary Tree in memory using array?
10. Write down the inorder, preorder and postorder traversal of the following binary tree

11. What is a Binary Search Tree (BST)? Show the structure of the binary search tree after
adding each of the following values in that order: 10, 25, 2, 4, 7, 13, 11, 22. What is the
height of the created binary search tree?

A binary search tree follows some order to arrange the elements. In a Binary search
tree, the value of left node must be smaller than the parent node, and the value of right
node must be greater than the parent node. This rule is applied recursively to the left
and right subtrees of the root.

12. What do you mean by in-degree and out-degree of a graph?


In-degree and out-degree of a vertex in a directed graph are the number of edges that come
into and out of that vertex, respectively:
 In-degree: The number of edges that come into a vertex
 Out-degree: The number of edges that leave a vertex
The degree of a vertex is the total number of edges it connects to, both incoming and outgoing.
13. List any three applications of BFS algorithm.
 Shortest Path and Minimum Spanning Tree for unweighted graph: In an
unweighted graph, the shortest path is the path with the least number of edges. With
Breadth First, we always reach a vertex from a given source using the minimum
number of edges. Also, in the case of unweighted graphs, any spanning tree is
Minimum Spanning Tree and we can use either Depth or Breadth first traversal for
finding a spanning tree.
 Minimum Spanning Tree for weighted graphs: We can also find Minimum
Spanning Tree for weighted graphs using BFT, but the condition is that the weight
should be non-negative and the same for each pair of vertices.
 Peer-to-Peer Networks: In Peer-to-Peer Networks like BitTorrent, Breadth First
Search is used to find all neighbor nodes.
 Crawlers in Search Engines: Crawlers build an index using Breadth First. The idea
is to start from the source page and follow all links from the source and keep doing
the same. Depth First Traversal can also be used for crawlers, but the advantage of
Breadth First Traversal is, the depth or levels of the built tree can be limited.
 Social Networking Websites: In social networks, we can find people within a given
distance ‘k’ from a person using Breadth First Search till ‘k’ levels.
 GPS Navigation systems: Breadth First Search is used to find all neighboring
locations.
 Broadcasting in Network: In networks, a broadcasted packet follows Breadth First
Search to reach all nodes.
 In Garbage Collection: Breadth First Search is used in copying garbage collection
using Cheney’s algorithm. Breadth First Search is preferred over Depth First Search
because of a better locality of reference.
 Cycle detection in undirected graph: In undirected graphs, either Breadth First
Search or Depth First Search can be used to detect a cycle. We can use BFS to detect
cycle in a directed graph also.
 Ford–Fulkerson algorithm In Ford – Fulkerson algorithm, we can either use
Breadth First or Depth First Traversal to find the maximum flow. Breadth First
Traversal is preferred as it reduces the worst-case time complexity to O(VE2).
 To test if a graph is Bipartite: We can either use Breadth First or Depth First
Traversal.
 Path Finding: We can either use Breadth First or Depth First Traversal to find if
there is a path between two vertices.
 Finding all nodes within one connected component: We can either use Breadth
First or Depth First Traversal to find all nodes reachable from a given node.
 AI: In AI, BFS is used in traversing a game tree to find the best move.
 Network Security: In the field of network security, BFS is used in traversing a
network to find all the devices connected to it.
 Connected Component: We can find all connected components in an undirected
graph.
 Topological sorting: BFS can be used to find a topological ordering of the nodes in
a directed acyclic graph (DAG).
14. Write a recursive algorithm to perform preorder traversal.

15. Develop an algorithm to add an element into a binary search tree.


16. Here is a small binary tree:
14
/ \
2 11
/ \ / \
1 3 10 30
/ /
7 40
What is the output obtained after preorder, inorder and postorder traversal of the above tree.
17. Draw the directed graph that corresponds to this adjacency matrix:
0 1 2 3
0 | true false true false |
1 | true false false false |
2 | false false false true |
3 | true false true false |

18. Explain the array implementation of a binary tree. Why is it not a good
representation for Binary Trees in general?
The array representation of binary tree can be done using a technique called level order
traversal. In level-order traversal, the elements of the binary tree are stored in the array
in the order in which they are visited in a breadth-first search.
The array representation of binary tree allows for efficient access to the elements of the
tree. For example, if a binary tree has n nodes, the array representation of the tree can
be stored in an array of size n, allowing for constant-time access to each node in the
tree.
In this representation, the root node of the binary tree is stored at the first position of
the array, and its left and right children are stored at the second and third positions,
respectively. The remaining nodes are stored in the same way, with the children of each
node stored in consecutive positions in the array.
For example, consider the following binary tree:

1
/\
2 3
/\
4 5
The level order representation of this tree in an array would be [1, 2, 3, 4, 5].
Using this representation, it is possible to access the children of a node at position i by
using the following formulas:
The left child of node i can be found at position 2i + 1
The right child of node i can be found at position 2i + 2
It is also possible to determine the parent of a node at position i using the formula:
The parent of node i can be found at position (i – 1) / 2
Using an array to represent a binary tree is not a good choice because it makes the
process of insertion, deletion, and traversal more costly.
Here are some other disadvantages of using an array to represent a binary tree:
 Wasting a lot of memory area
 Requiring a lot of data movement for insertion and deletion of nodes
 Having a high execution time
A better representation for a binary tree is the linked representation
19. Define (i) Tree (ii) Binary Tree
A tree is a data structure, and a binary tree is a type of tree data structure:

 Tree: A tree is a data structure that can be defined as a finite set of nodes.

 Binary tree: A binary tree is a rooted tree where each node has at most two children, called
the left and right child nodes. The children of a node are the nodes that are connected to it at
the level below.

20. Draw the binary tree whose sequential representation is given below.
21. Write the output of DFS and BFS traversals on the following graph considering
starting vertex as 1.

BFS:- 1 7 5 2 6 4 3
DFS:-1 7 5 6 4 2 3
22. Write algorithm to perform DFS in a graph. Explain with an example.

Depth-First Search (DFS)


Begin the search by visiting the start vertex v
 If v has an unvisited neighbor, traverse it recursively
 Otherwise, backtrack
We begin by visiting the start vertex v. Next an unvisited vertex w adjacent to v is selected,
and a depth-first search from w is initiated. When a vertex u is reached such that all its
adjacent vertices have been visited, we back up to the last vertex visited that has an unvisited
vertex w adjacent to it and initiate a depth-first search from w. The search terminates when no
unvisited vertex can be reached from any of the visited vertices.
DFS traversal of a graph, produces a spanning tree as final result. Spanning Tree is a graph
without any loops. We use Stack data structure with maximum size of total number of vertices
in the graph to implement DFS traversal of a graph.
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 adjacent vertex of the verex which is at top of the stack which is
not visited and push it on to the stack.
Step 4: Repeat step 3 until there are no new vertex to be visit from the vertex on top of the
stack.
Step 5: When there is no new vertex to be 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
23. Explain an application of a graph with an example
Ans :- Explain algorithm of BFS or DFS with example
24. How can we find the depth of a tree. Write an algorithm to find depth of a tree.
The height or depthof the tree is the number of edges in the tree from the root to the
deepest node.

Algorithm

 If the tree is empty, return -1.


 Otherwise, do the following:
o Get the height of the left subtree recursively, i.e., call height(node->left).
o Get the height of the right subtree recursively, i.e., call height(node->right).
o Compute the maximum of the heights of the left and right subtrees and add 1
to it for the current node.
o height = max(height of left subtree, height of right subtree) + 1.

25. Write an algorithm to delete a node from a Binary Search Tree

1. Start at the root of the binary search tree.


2. If the root is NULL, return NULL.
3. If the value to be deleted is less than the root's data, recursively call the delete function
on the left subtree.
4. If the value to be deleted is greater than the root's data, recursively call the delete
function on the right subtree.
5. If the value to be deleted is equal to the root's data, check if the node to be deleted has
one child or no child.
6. If the node has only one child or no child, free the memory allocated to the node and
return the appropriate child (if it exists).
7. If the node to be deleted has two children, find the minimum value in the right subtree
(i.e., the leftmost node in the right subtree). Copy the data in the minimum node to the
node to be deleted, and recursively call the delete function on the right subtree to delete
the minimum node.
8. Return the root of the updated binary search tree.

1) The node to be deleted is a leaf node:


2) The node to be deleted has only one child:
3) The node to be deleted has two children:

26. Write algorithms for Depth First Search and Breadth First Search of a Graph
Breadth-First Search (BFS)
In a breadth-first search, we begin by visiting the start vertex v. Next all unvisited vertices
adjacent to v are visited. Unvisited vertices adjacent to these newly visited vertices are then
visited and so on.
BFS traversal of a graph, produces a spanning tree as final result. Spanning Tree is a graph
without any loops. We use Queue data structure with maximum size of total number of
vertices in the graph to implement BFS traversal of a graph.
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 adjacent vertices of the vertex 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 produce final spanning tree by removing
unused edges from the graph
27. Write a non-recursive algorithm/pseudocode for pre-order traversal of a Binary
Tree.
1. Create an empty stack to store nodes.
2. Push the root node to the stack.
3. Run a loop while the stack is not empty
1. Pop the top node from stack.
2. Print the popped node.
3. Store all the children of popped node onto the stack. We must store children from
right to left so that leftmost node is popped first.
4. If stack is empty then we are done.

28. Write the non-recursive algorithm for post order traversal of tree

1. Create an empty stack.

2. While root is not NULL, push root->right and then root to the stack.

3. And Set root as root->left.

4. If root becomes NULL, pop item from the stack and set it as root.

5. If the popped item has a right child and the right child is at top of stack, then remove the right child

from stack, push the root back and set root as root’s right child.

6. Else print root’s data and set root as NULL.

7. Repeat the above two steps until stack is not empty.

You might also like