Lab 11

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Data Structures and Algorithms Spring

2023

LAB – 11 Issue Date: November 30, 2023

ALERT!

1. You are encouraged to bring your laptops for this lab.


2. This is an individual lab, you are strictly NOT allowed to discuss your solution with fellow
colleagues, even not allowed to ask how is he/she is doing, it may result in negative
marking.
3. Your code must run without errors.
4. Anyone caught in an act of plagiarism would be awarded an “F” grade in this Lab.
5. Discussion is not allowed and don’t use google or Chatgpt.

Task 01: [5 Marks]


You have been given an array A of size N and an integer K. This array consists of N integers ranging from 1
to 10^7.
Each element in this array is said to have a Special Weight. The special weight of an element a[i] is a[i] % k.
You now need to sort this array in Non-Increasing order of the weight of each element, i.e the element with
the highest weight should appear first, then the element with the second highest weight and so on. In case
two elements have the same weight, the one with the lower value should appear in the output first.
Remember to justify how your solution is the most optimal one. Identify which sort should be used
(if any).
Input Format: The first line consists of two space separated integers N and K. The next line consists of N
space separated integers denoting the elements of array A.

Output Format: Print N space separated integers denoting the elements of the array in the order in which
they are required.

Sample Input:
52
12345
Sample Output:
13524

Task 02: [10 Marks]

Binary trees are a type of data structure used for searching, storing, and retrieving elements in an
efficient manner. It consists of nodes connected in a tree-like fashion where each node has a
maximum of two children nodes. The node at the top of the tree is called the root node, while the
nodes that have no children are called leaf nodes.

Explanation of Array Representation of Binary Tree

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

Dry Run of Array Representation of Binary Tree with Example

For the array representation of binary tree, we will form an array of size 2^h-1 size where h
represents the maximum possible height of tree. Now we will move step by step for the array
representation of binary tree.

1. First, suppose we have a binary tree with seven nodes and height 3.

2. Now, for the array representation of binary tree, we have to give numbering to the
corresponding nodes. The standard form of numbering the nodes is to start from the root
node and move from left to right at every level .After numbering the tree and nodes will look
like this:

3. Now as we have numbered from zero you can simply place the corresponding number in the
matching index of an array then the array will look like this:

4. That is the array representation of binary tree but this is the easy case as every node has
two child so what about other cases? We will discuss other cases below.

5. In these cases, we will discuss the scenarios for the array representation of binary tree
where there the lead node is not always on the last level.

6. So consider the binary tree given below:


7. While giving a number you are stuck with the cases where you encounter a leaf node so just
make the child node of the leaf nodes as NULL then the tree will look like this:

8. Now just number the nodes as done above for the array representation of binary tree after
that the tree will look like this:

9. Now we have the number on each node we can easily use the tree for array representation
of binary tree and the array will look like this:

The paranthesis and Explorer View of Binary Tree is as follows:

class ArrayBT
{
int maxHeight;
int arraySize;
T * data;
bool* nodeStatus;
// You can make a private helper function if you need, remember to justify it

public:
ArrayBT(int h); //Initializes the node status array to zero.
setRoot(T r); // stores r at data[0] as root of tree and also sets the
nodeStatus[0] = 1
T getRoot(); // return root of the tree if exists
setLeftChild(T parent, T child);
setRightChild(T parent, T child);
T getParent(T node);
remove(T node); // removes the given node and all its descendants from the tree
displayAncestors(T node); // display ancestors of the given node
displayDescendants(T node); // display descendants of the given node
int heightOfTree(); // returns height (actual height) of the tree
levelOrder(); // do the level order traversal of the tree
displayLevel(int levelNo); // display the nodes on a particular level number
int findLevelOfNode(T node); // returns the level/depth of the given node
//each index of a level starts from 2^levelNo-1 and end at 2^levelNo+2^(levelNo-1)
displayParenthesizedView();
displayExplorerForm();
};

Task 03: [ 5 Marks]


Pre-order traversal is a type of tree traversal algorithm where each node is processed before its
children. The order of processing is as follows:
• Visit the root node.
• Traverse the left subtree in pre-order.
• Traverse the right subtree in pre-order.

Consider the following binary tree:


1
/ \
2 3
/ \
4 5
In pre-order traversal, you would visit the nodes in the order 1, 2, 4, 5, 3.

Explanation of the steps:

Visit Root (1): Process the value of the root node, which is 1.

Traverse Left Subtree (2): Move to the left subtree and repeat the process.

Visit Root (2): Process the value of the node, which is 2.

Traverse Left Subtree (4): Move to the left subtree and repeat.

Visit Root (4): Process the value of the node, which is 4.

No left or right subtree for 4.

Traverse Right Subtree (5): Move to the right subtree and repeat.

Visit Root (5): Process the value of the node, which is 5.

No left or right subtree for 5.

Traverse Right Subtree (3): Move to the right subtree and repeat.

Visit Root (3): Process the value of the node, which is 3.

No left or right subtree for 3

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