Ds 4 - Removed
Ds 4 - Removed
1. Define Binary search tree. Draw the BST for the following input:
14 15 4 9 7 18 3 5 16 20 17 9
Give the recursive search function to search an element in that tree (6)
2. Write c function to insert an element in a Binary Search Tree (4)
3. Write the recursive search and Iterative search algorithm for a binary search tree (8)
4. For the given data, draw a binary search tree and show the array and linked representation of the
same(6) 100, 85, 45, 55, 110, 20, 70, 65
5. Construct binary search tree for the given set of values 14, 15, 4, 9, 7, 18, 3, 5, 16, 20. Also perform
inorder, preorder, and postorder traversal of the obtained tree (6)
6. Construct a binary search tree by using the following in-order and post-order traversals (6)
a. In-order: BCAEDGHFI
b. Preorder: ABCDEFGHI
7. A binary tree has 9 nodes. The inorder and preorder traversals yield the following sequences of
nodes:
Inorder : E A C K F H D B G
Preorder : F A E K C D H G B
Draw a binary tree. Also perform the post-order traversal of the obtained tree(6)
8. Define the following terminologies with examples: (8)
a. Digraph
b. Weighted graph
c. Self-loop
d. Parallel edges
e. Multigraph
f. Complete Graph
9. Define graph. For the given graph show the adjacency matrix and adjacency list representation of
the graph (8)
10. Write an algorithm for Breadth first search and Depth first search (8)
11. What are the methods used for traversing a graph. Explain any one with example and write the C
function for the same (8)
12. Define selection tree. Construct min winner tree for the runs of a game given below. Each run
consists of values of players. Find the first 5 winners.
10 9 20 6 8 9 90 17
15 20 20 15 15 11 95 18
16 38 30 25 50 16 99 20
28
13. Define Forest. Transform the given forest into a Binary tree and traverse using inorder, preorder
and postorder traversal.
1. Define Binary search tree. Draw the BST for the following input:
14 15 4 9 7 18 3 5 16 20 17 9
Give the recursive search function to search an element in that tree (6)
Definition: A binary search tree is a binary tree. It may be empty. If it is not empty, it satisfies the
following properties
• Every element has a key, and no two elements have the same key, that is, the keys are unique.
• The keys in a nonempty left subtree must be smaller than the key in the root of the subtree.
• The keys in a nonempty right subtree must be larger than the key in the root of the subtree.
• The left and right subtrees are also binary search trees
• Recursive search
3. Write the recursive search and Iterative search algorithm for a binary search tree(8)
• Iterative Search
4. For the given data, draw a binary search tree and show the array and linked representation
of the same(6) 100, 85, 45, 55, 110, 20, 70, 65
Construction of Binary Search Tree
Array Representation
6. Construct a binary search tree by using the following in-order and post-order traversals (6)
a. In-order: BCAEDGHFI
b. Preorder: ABCDEFGHI
Binary Search Tree
7. A binary tree has 9 nodes. The inorder and preorder traversals yield the following
sequences of nodes:
Inorder : E A C K F H D B G
Preorder : F A E K C D H G B
Draw a binary tree. Also perform the post-order traversal of the obtained tree(6)
e. Digraph
▪ A directed graph G is defined as an ordered pair (V, E) where, V is a set of vertices and
the ordered pairs in E are called edges on V.
▪ A directed graph can be represented geometrically as a set of marked points (called
vertices) V with a set of arrows (called edges) E between pairs of points (or vertex or
nodes) so that there is at most one arrow from one vertex to another vertex.
▪ Example
f. Weighted graph
• A graph G is said to be weighted graph if every edge and/or vertices in the graph is assigned
with some weight or value.
• A weighted graph can be defined as G = (V, E, We, Wv) where V is the set of vertices, E is
the set at edges and We is a weights of the edges whose domain is E and Wv is a weight to
the vertices whose domain is V.
• Example
g. Self-loop
• An edge is called a loop if it has identical end points i.e edge that is incident from and
into the same vertex
• Example
h. Parallel edges
If there are two undirected edges to have the same end vertices, and for two directed edges
to have the same origin and the same destination. Such edges are called parallel edges or
multiple edges.
Example
9. Define graph. For the given graph show the adjacency matrix and adjacency list
representation of the graph (8)
C E
D F
Graph
Definition
A graph G is defined as an ordered set (V, E), where V(G) represents the set of vertices and E(G)
represents the edges that connect these vertices.
Example:
• There are five vertices or nodes and six edges in the graph.
• Vertices V(G) = {A, B, C, D and E}
• Edges E(G) = {(A, B), (B, C),(A, D), (B, D), (D, E), (C, E)}.
10. Write an algorithm for Breadth first search and Depth first search (8)
Breadth First Search(BFS)
• Breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all
the neighbouring nodes.
• Then for each nearest nodes, the algorithm explores their unexplored neighbour nodes, and so on.
• For example, start examining the node A and then all the neighbours of A are examined. In the
next step, examine the neighbours of neighbours of A, so on and so forth i.e algorithm track the
neighbours of the node and guarantee that every node in the graph is processed and no node is
processed more than once.
• This is accomplished by using a queue that will hold the nodes that are waiting for further
processing and a variable STATUS to represent the current state of the node.
• ALGORITHM
1. Input the vertices of the graph and its edges G = (V, E)
2. Input the source vertex and assign it to the variable S.
3. Add or push the source vertex to the queue.
4. Repeat the steps 5 and 6 until the queue is empty (i.e., front > rear)
5. Pop the front element of the queue and display it as visited.
6. Push the vertices, which is neighbor to just, popped element, if it is not in the queue and
displayed (i.e., not visited).
7. Exit.
11. What are the methods used for traversing a graph. Explain any one with example and write
the C function for the same (8)
Graph Traversal
• Graph traversal, which means visiting all the nodes of the graph.
• There are two graph traversal methods.
(a) Breadth First Search (BFS)
(b) Depth First Search (DFS)
Breadth First Search(BFS)
• Breadth-first search (BFS) is a graph search algorithm that begins at the root node and
explores all the neighbouring nodes.
• Then for each nearest nodes, the algorithm explores their unexplored neighbour nodes, and
so on.
• For example, start examining the node A and then all the neighbours of A are examined. In
the next step, examine the neighbours of neighbours of A, so on and so forth i.e algorithm
track the neighbours of the node and guarantee that every node in the graph is processed
and no node is processed more than once.
• This is accomplished by using a queue that will hold the nodes that are waiting for further
processing and a variable STATUS to represent the current state of the node.
• ALGORITHM
1. Input the vertices of the graph and its edges G = (V, E)
2. Input the source vertex and assign it to the variable S.
3. Add or push the source vertex to the queue.
4. Repeat the steps 5 and 6 until the queue is empty (i.e., front > rear)
5. Pop the front element of the queue and display it as visited.
6. Push the vertices, which is neighbor to just, popped element, if it is not in the queue
and displayed (i.e., not visited).
7. Exit.
• Example
Considering the graph G
Step 2: Pop (or remove) the front element A from the queue (by incrementing front = front +1) and
display it. Then push (or add) the neighboring vertices of A to the queue, (by incrementing Rear =
Rear +1) if it is not in queue.
Step 3: Pop the front element B from the queue and display it. Then add the neighboring vertices
of B to the queue, if it is not in queue.
One of the neighboring element C of B is already preset in the queue, So C is not added to queue.
Step 4: Remove the front element C and display it. Add the neighboring vertices of C, if it is not
present in queue.
Step 6: Again the process is repeated until front>rear.i.i remove the front element E from the
queue and add the neighbouring vertex if it is not present in the queue
BFS traversal : A, B, C, D, E, F, G, H ,I
12. Define selection tree. Construct min winner tree for the runs of a game given below. Each
run consists of values of players. Find the first 5 winners.
10 9 20 6 8 9 90 17
15 20 20 15 15 11 95 18
16 38 30 25 50 16 99 20
28
Selection Tree
• Also called Tournament Tree
• Selection Tree is a data structure which is used to select a winner in a knockout tournament
• The leaves of the tree represent players entering the Tournament
• Each internal node represents winner or loser in the match
• If the internal nodes in a tree represent winners the tree is called winner tree
• If the internal nodes in a tree represent losers the tree is called loser tree
• Using selection tree we can sort the elements in ascending/descending order
• Two types of selection trees
o Winner Tree
o Loser Tree
10
10 16
10 15 20 16
Winner Tree
• A selection tree where each internal node represents the winner is called Winner tree.
• A Winner tree is a complete binary tree with n-leaf nodes and n-1 internal nodes where
o Each internal node records the winner of the match
o A winner tree can be either a min winner tree or max winner tree
o To determine the winner of the match, assume that each player is associated with a value
▪ Min winner tree- each internal node represents the smaller of its two children i.e
player with smaller value wins
▪ Max winner tree - each internal node represents the larger of its two children i.e
player with larger value wins
• Root node represents the smallest node in min winner tree
• Root node represents the largest node in max winner tree
• Steps
1. Input: A set of records arranged in ascending order
2. Consider first item from each run and treat them as leaves of the nodes
3. Between players select the smallest item and elevate to parent position
4. Each non-leaf node represents tournament winner at that level & Root node with smallest
item is the winner of the tournament
5. Remove the root node and output the winner
6. Next obtain the next node from corresponding run and reconstruct the winner tree by
playing the tournament along the path from root node.
7. Continue steps 3, 4, 5 and 6 until get 5 winners
Forest
Definition: If T1…..Tn is a forest of trees, then the binary tree corresponding to this forest, denoted
by B(T1….T)
Transform the given forest into a Binary tree
Forest Traversals
Preorder, inorder, and postorder traversals of the corresponding binary tree T of a forest F have a
natural correspondence with traversals of F.
Preorder Traversal
1. If F is empty, then return.
2. Visit the root of the first tree of F.
3. Traverse the subtrees of the first tree in tree preorder.
4. Traverse the remaining trees of F in preorder.
Postorder Traversal
1. If F is empty, then return
2. Traverse the subtrees of the first tree in tree inorder.
3. Visit the root of the first tree.
4. Traverse the remaining trees in tree inorder.
Inorder Traversal
1. If F is empty, then return.
2. Traverse the subtrees of the first tree of F in tree postorder.
3. Traverse the remaining trees of F in tree postorder.
4. Visit the root of the first tree of F.