0% found this document useful (0 votes)
16 views16 pages

Ds 4 - Removed

The document provides a comprehensive overview of binary search trees (BST) and graph theory, including definitions, construction methods, and traversal algorithms. It covers various topics such as inserting elements, searching recursively and iteratively, and constructing trees from given traversals. Additionally, it discusses graph representations, traversal methods like BFS and DFS, and introduces concepts like selection trees and forests.

Uploaded by

gautampl444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views16 pages

Ds 4 - Removed

The document provides a comprehensive overview of binary search trees (BST) and graph theory, including definitions, construction methods, and traversal algorithms. It covers various topics such as inserting elements, searching recursively and iteratively, and constructing trees from given traversals. Additionally, it discusses graph representations, traversal methods like BFS and DFS, and introduces concepts like selection trees and forests.

Uploaded by

gautampl444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

MODULE IV

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

treepointer search(treepointer root, int key)


{
if (!root) return NULL;
if (key == root->data)
return root;
if (key < root->data)
return search(root->left_child, key);
return search(root->right_child,key);
}

2. Write function to insert an element in a Binary Search Tree(4)


In a binary search tree, the insertion operation is performed with O(log n) time complexity. In
binary search tree, new node is always inserted as a leaf node. The insertion operation is performed
as follows...
Step 1: Create a newNode with given value and set its left and right to NULL
Step 2: Check whether tree is Empty
Step 3: If the tree is Empty, then set set root to newNode
Step 4: If the tree is Not Empty, then check whether value of newNode is smaller or larger than the
node (here it is root node)
Step 5: If newNode is smaller than or equal to the node, then move to its left child. If newNode is
larger than the node, then move to its right child
Step 6: Repeat the above step until we reach a node (e.i., reach to NULL) where search terminates
Step 7: After reaching a last node, then insert the newNode as left child if newNode is smaller or
equal to that node else insert it as right child

C function to Insert an element in BST

Insert (TREE, VAL)


Step 1: IF TREE = NULL
Allocate memory for TREE
SET TREE DATA = VAL
SET TREE LEFT = TREE RIGHT = NULL
ELSE
IF VAL < TREE DATA
Insert(TREE LEFT, VAL)
ELSE
Insert(TREE RIGHT, VAL)
[END OF IF]
[END OF IF]
Step 2: END

3. Write the recursive search and Iterative search algorithm for a binary search tree(8)

• To search for an element with a key, begin at the root.


• If the root is NULL, the search tree contains no elements and the search is unsuccessful.
• Otherwise, compare key with the key value in root. If key equals root's key value, then the search
terminates successfully. If key is less than roof s key value, then no element in the right subtree can
have a key value equal to key. Therefore, we search the left subtree of root. If key is larger than roofs
key value, we search the right subtree of root. The function search (Program 5.15) recursively searches
the subtrees.
• Recursive search

treepointer search(treepointer root, int key)


{
if (!root) return NULL;
if (key == root->data)
return root;
if (key < root->data)
return search(root->left_child, key);
return search(root->right_child,key);
}

• Iterative Search

tree—pointer search2(tree—pointer tree, int key)


{
while(tree)
{
if (key == tree->data)
return tree;
if (key <tree->data)
tree->treeleftchild;
else
tree->right— child;
}
return NULL;
}

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

Linked List Representation


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
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)

8. Define the following terminologies with examples: (8)


a. Digraph
b. Weighted graph
c. Self-loop
d. Parallel edges

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)}.

Adjacency Matrix & Adjacency List Representation

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.

DEPTH FIRST SEARCH


• The depth first search (DFS), is to search deeper in the graph, when ever possible.
• Given an input graph G = (V, E) and a source vertex S, from where the searching starts.
• First visit the starting node.
• Then travel through each node along a path, which begins at S. i.e visit a neighbor vertex of S and
again a neighbor of a neighbor of S, and so on.
• The implementation of BFS is almost same except a stack is used instead of the queue.
• Algorithm
1. Input the vertices and edges of the graph G = (V, E).
2. Input the source vertex and assign it to the variable S.
3. Push the source vertex to the stack.
4. Repeat the steps 5 and 6 until the stack is empty.
5. Pop the top element of the stack and display it.
6. Push the vertices which is neighbor to just popped element, if it is not in the
7. queue and displayed (ie; not visited).
8. 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 1: Initially push A (the source vertex) to the queue.

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.

One of the neighboring elements E of C is present in the queue. So E is not added.


Step 5: Remove the front element D, and add the neighboring vertex 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

Applications of Breadth-First Search Algorithm


Breadth-first search can be used to solve many problems such as:
• Finding all connected components in a graph G.
• Finding all nodes within an individual connected component.
• Finding the shortest path between two nodes, u and v, of an unweighted graph.
• Finding the shortest path between two nodes, u and v, of a weighted graph.

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

Min Winner Tree


13. Define Forest. Transform the given forest into a Binary tree and traverse using inorder,
preorder and postorder traversal.

Forest

Definition: A forest is a set of n> 0 disjoint trees.


The concept of a forest is very close to that of a tree because if we remove the root of a tree we obtain
a forest. For example, removing the root of any binary tree produces a forest of two trees.

Transforming A Forest Into A Binary Tree


Suppose that we have a forest, To transform this forest into a single binary tree, we first obtain the
binary tree representation for each of the trees in the forest. We then link all the binary trees together
through the sibling field of the root node.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy