Count the nodes of the given tree whose weight string is a palindrome


We had to explore the tree and assess the weight of each hub in order to identify the nodes in a particular tree whose weighted string may be a palindrome. In this scenario, a hub's weight is seen as a string. The weight string is checked to see if it is a palindrome using the palindrome checking theory. We traverse the tree recursively, starting at the root, and evaluate the weight of each node. We raise the counter if the weight string is a palindrome. We can accurately examine the hubs that satisfy the requirement of having a weighted string that is a palindrome by systematically travelling the tree and using the palindrome check.

Methods Used

  • Breadth-First Search (BFS) with Queue

  • In order Traversal

Breadth- First Search (BSF) with Queue

In the situation of counting the hubs in a given tree, whose weight string may be a palindrome, breadth-first search (BFS) with a line might be used as a traversal approach. We enqueue every hub into a queue starting at the root. Even if the line cannot be purge, we dequeue a hub and check to see whether its weight string contains a palindrome. We raise the counter if there is a probability that it is. The cleared-out and right-child hubs of the dequeued hub are then enqueued into the line, if applicable. BFS ensures that we visit all hubs and accurately number the ones satisfying the palindrome criteria by preparing hubs level by level.

Algorithm

  • Initialise a counter variable to 0.

  • Create a purge line and enqueue the root node.

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

  • a. Dequeue a hub from the front of the queue.

  • b. Check if the weight string of the dequeued node could be a palindrome.

  • If it is, increase the counter.

  • c. Enqueue the cleared-out child and the proper child of the dequeued hub (in case they exist) into the queue.

  • Return the ultimate value of the counter.

Example

#include <iostream>
#include <queue>
#include <string>

// Structure for a tree node
struct Node {
   std::string weight;
   Node* left;
   Node* right;
};

// Function to check if a string is a palindrome
bool isPalindrome(const std::string& str) {
   int i = 0;
   int j = str.length() - 1;

   while (i < j) {
      if (str[i] != str[j]) {
         return false;
      }
      i++;
      j--;
   }

   return true;
}

// Function to perform BFS and count nodes with palindrome weight strings
int countPalindromeNodes(Node* root) {
   if (root == nullptr) {
      return 0;
   }

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

   while (!nodeQueue.empty()) {
      Node* currentNode = nodeQueue.front();
      nodeQueue.pop();

      if (isPalindrome(currentNode->weight)) {
         count++;
      }

      if (currentNode->left) {
         nodeQueue.push(currentNode->left);
      }
      if (currentNode->right) {
         nodeQueue.push(currentNode->right);
      }
   }

   return count;
}

int main() {
   // Create a sample tree
   Node* root = new Node{"level"};
   root->left = new Node{"radar"};
   root->right = new Node{"tree"};
   root->left->left = new Node{"deed"};
   root->left->right = new Node{"car"};
   root->right->left = nullptr;
   root->right->right = nullptr;

   // Count nodes with palindrome weight strings
   int count = countPalindromeNodes(root);

   // Output the result
   std::cout << "Number of nodes with palindrome weight strings: " << count << std::endl;

   // Clean up memory
   delete root->left->left;
   delete root->left->right;
   delete root->right;
   delete root->left;
   delete root;

   return 0;
}

Output

Number of nodes with palindrome weight strings: 3

In-order Traversal

In-order traversal, within the setting of tallying hubs in a given tree whose weight string could be a palindrome, alludes to a deliberate way of navigating the tree hubs. Beginning from the root, we visit the cleared-out subtree, at that point the current hub, and at long last the proper subtree. During this traversal, we check to see if the weight string of each hub could be a palindrome. In case it is, we increase the counter. By taking this approach, we methodically look at each hub within the tree and number the hubs that fulfil the condition of having a weight string that's a palindrome. This guarantees a precise number of such hubs within the given tree.

Algorithm

  • Characterise a work countPalindromeNodes (node) that takes a hub as input.

  • Initialise a counter variable to 0.

  • If the hub is invalid, return 0.

  • Recursively call countPalindromeNodes on the cleared-out child of the node.

  • Check if the weight string of the current hub may be a palindrome.

  • If it is, increment the counter.

  • Recursively call countPalindromeNodes on the proper child of the node.

  • Return the ultimate value of the counter.

Example

#include <iostream>
#include <string>

struct Node {
   std::string weight;
   Node* left;
   Node* right;

   Node(const std::string& val) : weight(val), left(nullptr), right(nullptr) {}
};

int countPalindromeNodes(Node* node) {
   if (node == nullptr) {
      return 0;
   }

   int counter = 0;
   counter += countPalindromeNodes(node->left);

   // Check if weight string is a palindrome
   std::string weight = node->weight;
   bool isPalindrome = true;
   int i = 0, j = weight.length() - 1;
   while (i < j) {
      if (weight[i] != weight[j]) {
         isPalindrome = false;
         break;
      }
      i++;
      j--;
   }

   if (isPalindrome) {
      counter++;
   }

   counter += countPalindromeNodes(node->right);

   return counter;
}

int main() {
   // Constructing the tree
   Node* root = new Node("level");
   root->left = new Node("radar");
   root->right = new Node("tree");
   root->left->left = new Node("deed");
   root->left->right = new Node("moon");
   root->right->left = new Node("river");
   root->right->right = new Node("boat");

   // Counting nodes with weight string as palindrome
   int result = countPalindromeNodes(root);

   std::cout << "Number of nodes with weight string as palindrome: " << 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 string as palindrome: 3

Conclusion

This article gives a clarification and execution of checking the hubs in a given tree whose weight string could be a palindrome. It presents two approaches: Breadth-First Look (BFS) with a line and In-order traversal. The BFS approach involves traversing the tree level by level, employing a queue, and checking in the event that the weight string of each hub may be a palindrome. The In-order traversal approach recursively visits the hubs within the cleared-out subtree, checks the current hub, and then visits the hubs within the right subtree, whereas tallying the hubs with palindrome weight strings Both approaches guarantee an exact tally of hubs that meet the palindrome condition.

Updated on: 19-Jul-2023

58 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements