Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Q1.

a.
Post-Order and Level-Order ////// not possible
Reason: determining a tree due to the existence of multiple trees with the same set of nodes is
difficult.
b.
Inorder and Preorder ////// possible
Reason: constructing a tree using Inorder and Preorder traversals is better. In Inorder traversal,
nodes are visited in sorted order, while in Preorder traversal, the root is visited first, followed by
subtree traversals.

Pseudo-code
1. Create an empty tree node called root
2. Find root in preorder traversal
3. If root not found in preorder, return NULL
4. Find the root node in Inorder Traversal
5. If root node is not found in inorder traversal, return NULL.
6. Divide the inorder traversal into two parts: Left subtree and right subtree
7. Divide preorder also similarly only
8. Construct a left subtree by recursively using the left subtree function of inorder and preorder
traversals.
9. Similarly, use right subtree function recursively for the construction of the right subtree
10. Return the root node.
c.
Inorder and level order //// not possible
Reason: Determining a unique tree using Inorder and Level-Order traversals is difficult since
multiple trees share the same set of nodes.
d.
Inorder and Post-order ///// not possible
Reason: Determining a unique tree using Inorder and Level-Order traversals is difficult since
multiple trees share the same set of nodes.
e.
Post order and Preorder ///// possible
Reason: constructing a tree using Post-Order and Preorder traversals is feasible. In Preorder
traversal, the root is visited first, followed by subtree traversals, while in Post-Order traversal,
the subtree traversals precede the root node.
Pseudocode:
1. Create an empty tree node called a root
2. Find root in preorder traversal
3. If root not found in preorder, return NULL
4. Find root node in postorder Traversal
5. If root node is not found in postorder traversal, return NULL.
6. Divide the preorder traversal into two parts: Left subtree and right subtree
7. Similarly divide postorder
8. Recursively construct the right subtree by employing the corresponding function for right
subtree construction from both Postorder and Preorder traversals.
9. Likewise, recursively utilize the left subtree function to construct the left subtree.
10. return the root node

REPRort
4. We first make a data structure which
checks the type of query (1, 2, or 3) to determine which student is taking their turn.
If it's Yuji's turn (query 1), it randomly selects the task with the highest or lowest ID from the list
and prints the task ID.
If it's Megumi's turn (query 2), it randomly selects a number 'k' and prints the kth smallest task
ID from the list.
If it's Nobara's turn (query 3), it calculates the median task ID from the list and prints it.
After each turn, the selected task is removed from the list to prevent it from being chosen again,
and the list size is updated accordingly.
The code continues this process until each student has completed their turn. Finally, it exits the
program.

3. This code defines a structure called MyTree representing each node of the binary tree.
Then we define functions to create a new node (getNode) and free a node (freeNode).
Then initialize an array parent_nodes to hold pointers to all the nodes in the tree.
prompts the user to input two target node keys.
Then we initialize the parent_nodes array with nodes based on the provided tree node values.
It then links the nodes together to form the binary tree structure.
It searches for the nodes corresponding to the input target keys (t_node1 and t_node2) in the
tree and checks if the two target nodes have the same parent node.
It prints "Possible" if the two target nodes have different parent nodes, indicating that it's
possible to reach one from the other in the tree. Otherwise, it prints "Not Possible".

2. This code, It first defines a structure for a tree node (bTREE)


It then defines functions to create a new node (getNode) and free a node (freeNode).
Then initializes an array to hold pointers to all nodes in the tree (p_tree_nodes).
Afterwards, we create the binary tree based on the provided tree node values.
then searches for a specific target node in the tree. If the target node is found, it performs a
level-order traversal starting from that node: It uses a queue to store nodes at each level.
It iteratively dequeues nodes and prints their key values.
It continues this process until all nodes at every level are traversed.

You might also like