Count the nodes of the given tree whose weight has X as a factor


The assignment is to check the number of hubs in a given tree where the weight of each hub is detachable by a given number, X. To achieve this, we navigate the tree in a precise way, analysing each hub and its weight. In case the weight of a hub is distinct by X, we increase a counter. We proceed with this process for all hubs within the tree. Finally, the value of the counter speaks to the overall number of hubs within the tree, whose weight may be a figure of X. This approach guarantees that we recognise and tally, as it were, those hub assemblies in the desired condition, giving a precise number.

Methods Used

  • Recursive Depth-first search (DFS)

  • In order Traversal

Recursive Depth-first Search

Recursive Depth-First Search (DFS) may be a strategy to navigate a tree in an orderly way, whereas checking hubs whose weight is separable by X Beginning from the root, we check on the off chance that the weight of the current hub is distinct by X. In the event that it is, we increase the counter. At that point, we recursively apply the same handle to the cleared-out and right subtrees. This approach guarantees that each hub is gone, and the counter is augmented fittingly when a hub meets the given condition. By navigating the tree recursively in a depth-first way, we will viably tally the hubs, fulfilling the weight calculation basis.

Algorithm

  • Begin with a work countNodes(root, X) that takes the root hub of the tree and the divisor X as parameters.

  • Initialise a counter variable to 0.

  • Check in case the weight of the current hub (root) is distinct by X. On the off chance that it is, increase the counter.

  • Recursively call countNodes on the cleared-out child of the current hub, passing the cleared-out child and X as parameters.

  • Recursively call countNodes on the proper child of the current hub, passing the proper child and X as parameters.

  • Return the ultimate value of the counter.

  • Invoke the count Nodes work with the root of the tree and the specified divisor X to get the tally of hubs whose weight is separable by X.

Example

#include <iostream>

struct Node {
   int weight;
   Node* left;
   Node* right;

   Node(int val) : weight(val), left(nullptr), right(nullptr) {}
};

int countNodes(Node* root, int X) {
   if (root == nullptr) {
      return 0;
   }

   int count = 0;

   if (root->weight % X == 0) {
      count++;
   }

   count += countNodes(root->left, X);
   count += countNodes(root->right, X);

   return count;
}

int main() {
   // Example usage

   // Constructing the tree
   Node* root = new Node(10);
   root->left = new Node(15);
   root->right = new Node(20);
   root->left->left = new Node(30);
   root->left->right = new Node(35);
   root->right->left = new Node(40);
   root->right->right = new Node(45);

   // Counting nodes with weight divisible by 5
   int divisor = 5;
   int result = countNodes(root, divisor);

   std::cout << "Number of nodes with weight divisible by " << divisor << ": " << result << std::endl;

   // Clean up the dynamically allocated memory (deallocate the tree)
   delete root->left->left;
   delete root->left->right;
   delete root->right->left;
   delete root->right->right;
   delete root->left;
   delete root->right;
   delete root;

   return 0;
}

Output

Number of nodes with weight divisible by 5: 7

In-order Traversal

Within the setting of tallying the hubs in a tree whose weight is separable by X, the in-order traversal could be a strategy of going by the tree hubs in a particular arrangement. Beginning from the root, we recursively navigate the left subtree; at that point, we visit the current hub, and at long last, we navigate the correct subtree. During this traversal, at each hub, we check to see if the weight is distinct by X. If it is, we increase the counter. By taking this approach, we methodically look at each hub within the tree and tally the hubs that fulfil the condition of having a weight detachable by X.

Algorithm:

  • Initialise a counter variable to 0.

  • Start at the root of the tree.

  • If the current hub is invalid, return.

  • Recursively navigate the cleared-out subtree.

  • At the current hub, check to see if the weight is detachable by X.

  • If it is, increase the counter.

  • Recursively navigate the proper subtree.

  • Repeat steps 2–6 for each hub within the tree.

  • Once all hubs have been gone by, the value of the counter speaks to the full number of hubs within the tree whose weight is detachable by X.

  • Return the value of the counter.

Example

#include <iostream>

// Structure for a tree node
struct Node {
   int weight;
   Node* left;
   Node* right;
};

// Function to perform in-order traversal and count nodes divisible by X
int countNodesDivisibleByX(Node* root, int X) {
   if (root == nullptr) {
      return 0;
   }

   int counter = 0;

   // In-order traversal
   counter += countNodesDivisibleByX(root->left, X);

   // Check if current node's weight is divisible by X
   if (root->weight % X == 0) {
      counter++;
   }

   counter += countNodesDivisibleByX(root->right, X);

   return counter;
}

int main() {
   // Create a sample tree
   Node* root = new Node();
   root->weight = 10;

   Node* node1 = new Node();
   node1->weight = 20;

   Node* node2 = new Node();
   node2->weight = 15;

   Node* node3 = new Node();
   node3->weight = 30;

   root->left = node1;
   root->right = node2;
   node1->left = node3;
   node1->right = nullptr;
   node2->left = nullptr;
   node2->right = nullptr;
   node3->left = nullptr;
   node3->right = nullptr;

   // Specify the value of X
   int X = 5;

   // Count nodes divisible by X
   int count = countNodesDivisibleByX(root, X);

   // Output the result
   std::cout << "Number of nodes divisible by " << X << ": " << count << std::endl;

   // Clean up memory
   delete node3;
   delete node2;
   delete node1;
   delete root;

   return 0;
}

Output

Number of nodes divisible by 5: 4

Conclusion

This article talks about the issue of checking the hubs in a given tree whose weight is separable by a given number, X. It presents diverse approaches, including recursive depth-first look (DFS) and in-order traversal, to illuminate the issue. The article gives algorithmic steps for each approach and outlines their execution using C code cases. It emphasises the significance of orderly tree traversal and condition checking to precisely tally the hubs that meet the desired weight divisibility criterion. The objective is to provide a comprehensive understanding of the issue and offer different methodologies for its viable arrangement.

Updated on: 19-Jul-2023

44 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements