Module 4 UQ
Module 4 UQ
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.
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
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.
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.
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.
Algorithm
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
2. While root is not NULL, push root->right and then root to the stack.
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.