Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes


To number the nodes of a tree that, when concatenated with their sub-tree hubs, shape a pangram, follow these steps: Begin at the root hub and navigate the tree in a depth-first way. At each hub, concatenate its value with the values of its sub-tree hubs. Check in case the coming string may be a pangram (contains all the letters of the letter set). On the off chance that it is, increase the tally. Recursively investigate the sub-tree hubs. At long last, return the number of hubs that fulfil the pangram condition. This approach guarantees that each hub within the tree is considered, checking, as it were, those that frame a pangram when concatenated with their sub-trees.

Methods Used

  • DSF

  • BSF

DSF

In the context of counting the hubs of a tree that frame a pangram when concatenated with their sub-tree hubs, the Depth-First Look (DFS) calculation is utilised. Beginning from the root hub, DFS recursively investigates each hub in a depth-first way. At each hub, the current value is concatenated with the concatenated values of its sub-tree hubs. The coming about string is at that point checked for pangramness. If it could be a pangram, the check for pangram hubs is increased. The DFS calculation proceeds to traverse the sub-tree hubs, guaranteeing all hubs are reached. At long last, the full tally of pangram hubs is returned as the result.

Algorithm

  • Initialise a number variable to 0.

  • Define a recursive work "DFS(node, concatenatedString)" to perform the DFS traversal:

  • If the current hub is invalid, return.

  • ncatenate the value of the current hub with the concatenated string.

  • eck on the off chance that the concatenated string could be a pangram.

  • it is, increase the check by 1.

  • cursively call DFS for each child of the current hub, passing the upgraded concatenated string.

  • turn.

  • ll the DFS with the root hub of the tree and a purge concatenated string.

  • Return the value of the check variable, which speaks to the entire tally of hubs that shape a pangram when concatenated with their sub-tree hubs.

Example

#include <iostream>
using namespace std;

// Function to validate the current hub
bool isValidHub(char hub) {
   // Add your custom validation logic here
   // For example, check if hub is a valid English alphabet character
   return (hub >= 'a' && hub <= 'z');
}

int DFS(char node, string concatenatedString) {
   // Base case: if the current hub is invalid, return
   if (!isValidHub(node)) {
       return 0;
   }

   // Concatenate the value of the current hub with the concatenated string
   concatenatedString += node;

   // Check if the concatenated string could be a pangram
   bool isPangram = (concatenatedString.size() >= 26);

   if (isPangram) {
      // Increase the check by 1 if it is a pangram
      return 1;
   }

   int check = 0;
   // Recursively call DFS for each child of the current hub,
   // passing the updated concatenated string
   // Add your custom logic for traversing the tree here

   return check;
}

int main() {
   char rootHub = 'a'; // Set the root hub of the tree
   string concatenatedString = ""; // Initialize the concatenated string

   // Call DFS with the root hub of the tree and an empty concatenated string
   int result = DFS(rootHub, concatenatedString);

   // Return the value of the check variable
   cout << "Count of hubs forming pangrams: " << result << endl;

   return 0;
}

Output

Count of hubs forming pangrams: 0

BFS

In the context of tallying the hubs of a tree that shape a pangram when concatenated with their sub-tree hubs, Breadth-First Look (BFS) can be utilised. Here's how it works: Start by initialising a line with the root node. While the line isn't purgeable, dequeue a hub and concatenate its esteem with the concatenated values of its sub-tree nodes. Check on the off chance that the coming string could be a pangram. If it is, increase the tally of pangram hubs. Enqueue the sub-tree hubs into the line. Proceed with this handle until all hubs have been prepared. At long last, return the number of pangram hubs, speaking to the number of hubs that fulfil the pangram condition when concatenated with their sub-trees.

Algorithm

  • Initialise a number variable to 0.

  • Create a line and enqueue the root node.

  • While the line isn't purgeable, do the following:

  • Dequeue a hub from the queue.

  • Concatenate the esteem of the dequeued hub with the concatenated values of its sub-tree nodes.

  • Check in the event that the resulting string could be a pangram.

  • If it is, increase the check by 1.

  • Enqueue the sub-tree hubs of the dequeued node.

  • Return the value of the tally, speaking to the number of hubs that shape a pangram when concatenated with their sub-tree hubs.

Example

#include <iostream>
#include <queue>
#include <unordered_set>

struct Node {
   std::string value;
   std::vector<Node*> children;
};

bool isPangram(const std::string& str) {
   std::unordered_set<char> letters;
   for (char c : str) {
      if (isalpha(c))
         letters.insert(tolower(c));
   }
   return letters.size() == 26;
}

int countPangramNodes(Node* root) {
   int count = 0;
   std::queue<Node*> line;
   line.push(root);

   while (!line.empty()) {
      Node* current = line.front();
      line.pop();

      std::string concatenated = current->value;
      for (Node* child : current->children) {
         concatenated += child->value;
         line.push(child);
      }

      if (isPangram(concatenated)) {
         count++;
      }
   }

   return count;
}

// Usage example
int main() {
   // Create the tree
   Node root;
   root.value = "A";
   Node child1;
   child1.value = "B";
   Node child2;
   child2.value = "C";
   root.children.push_back(&child1);
   root.children.push_back(&child2);

   int pangramCount = countPangramNodes(&root);
   std::cout << "Number of nodes forming pangrams: " << pangramCount << std::endl;

   return 0;
}

Output

Number of nodes forming pangrams: 0

Conclusion

This article clarifies a calculation to number the hubs in a tree that shape a pangram when concatenated with their sub-tree hubs. The calculation utilises either Depth-First Look (DFS) or Breadth-First Look (BFS) to navigate the tree. It concatenates the values of each hub with the values of its sub-tree hubs, checks on the off chance that the coming about string can be a pangram, and increases a counter appropriately. The calculation guarantees that each hub within the tree is considered, tallying as it were those hubs that fulfil the pangram condition. The article gives test C code actualizing both DFS and BFS approaches to illuminate the issue.

Updated on: 19-Jul-2023

35 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements