
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Nodes of Tree with Anagram Weighted String
To check the nodes of a tree whose weighted string is a rearranged word of the given string, perform a depth-first search (DFS) on the tree. Beginning from the root, navigate each hub and calculate the weighted string by relegating a weight to each character within the node's esteem. Compare this weighted string with the given string to check for a rearranged word coordinate. In the event that they are rearranged words, increase the check. Recursively investigate the children of each node. At last, return the overall count of hubs fulfilling the condition. This approach guarantees each hub within the tree is considered, checking, as it were, those whose weighted string is a rearranged word of the given string.
Methods Used
DFS
Pre order traversal
DFS
DFS (Depth-First Search) is an algorithmic approach to numbering the hubs of a tree whose weighted string is a rearranged word of the given string. Beginning from the root hub, we visit each hub in a depth-first way. At each hub, we calculate the weighted string by doling out a weight to each character within the node's esteem. At that point, we compare this weighted string with the given string. On the off chance that they are rearranged words, we increase the check. We recursively investigate the children of each hub, rehashing the method until all hubs are navigated. At long last, we return the whole check of hubs fulfilling the rearranged word condition.
Algorithm
Initialise a variable "count" to 0.
Define a recursive work "DFS(node, targetString, characterCount)" to perform the DFS traversal:
Calculate the weighted string of "hub" by relegating weights to each character based on "characterCount".
eck in the event that the weighted string is a rearranged word of "targetString". On the off chance that yes, increase "count".
date the "characterCount" by adding the characters of "hub" to it.
cursively call "DFS" for each child of "node".
ll the "DFS" work with the root of the tree, the given string as "targetString", and a purge "characterCount" dictionary.
Return the esteem of "tally", which speaks to the overall number of hubs whose weighted string is a rearranged word of the given string.
Example
#include <iostream> #include <unordered_map> #include <algorithm> using namespace std; int totalCount = 0; string calculateWeightedString(const string& word, unordered_map<char, int>& characterCount) { string weightedString = ""; for (char c : word) { weightedString += to_string(characterCount[c]); } return weightedString; } struct Node { string data; Node* next; Node(const string& value) : data(value), next(nullptr) {} }; void DFS(Node* node, const string& targetString, unordered_map<char, int> characterCount) { string hub = calculateWeightedString(node->data, characterCount); sort(hub.begin(), hub.end()); // Sort the weighted string for comparison if (hub == targetString) { totalCount++; } for (char c : node->data) { characterCount[c]++; } if (node->next) { DFS(node->next, targetString, characterCount); } } int main() { Node* root = new Node("root"); // Sample code - Initialize the root node root->next = new Node("child1"); root->next->next = new Node("child2"); root->next->next->next = new Node("grandchild1"); root->next->next->next->next = new Node("grandchild2"); string targetString = "toor"; // Sample code - Set the target string unordered_map<char, int> characterCount; // Store character counts for each node for (char c : root->data) { characterCount[c] = 0; } DFS(root, targetString, characterCount); cout << "Total Count: " << totalCount << endl; // Clean up memory Node* current = root; while (current) { Node* nextNode = current->next; delete current; current = nextNode; } return 0; }
Output
Total Count: 0
Pre-order Traversal
To check the nodes of a tree whose weighted string is a rearranged word of the given string utilising the preorder traversal approach, begin from the root hub. Calculate the weighted string for the current hub and compare it with the given string. On the off chance that they are rearranged words, increase the check. At that point, recursively navigate the cleared-out subtree and rehash the method. A short time later, navigate the correct subtree recursively and apply the same steps. Accumulate the checks from both subtrees. Finally, return the overall check, considering all the hubs within the tree whose weighted string is a rearranged word of the given string.
Algorithm
Initialise a count variable to 0.
Define a work preorder count (node, given). String, count):
If the current hub is invalid, return.
Calculate the weighted string for the current node.
If the weighted string is a rearranged word of the given string, increase the count.
Recursively call preorderCount on the cleared-out subtree.
Recursively call preorderCount on the correct subtree.
Call preorderCount(root, givenString, tally), where root is the root of the tree.
Return the value of the tally variable.
of the checks gotten from both subtrees, plus the increased check.
Example
#include <iostream> #include <algorithm> #include <string> struct Node { std::string data; Node* left; Node* right; Node(const std::string& value) : data(value), left(nullptr), right(nullptr) {} }; // Function to calculate the weighted string std::string calculateWeightedString(const std::string& input) { std::string weightedString = input; std::sort(weightedString.begin(), weightedString.end()); return weightedString; } // Function to perform preorder count int preorderCount(Node* node, const std::string& given, int count) { if (node == nullptr) { return count; } std::string weightedString = calculateWeightedString(node->data); if (weightedString == given) { count++; } count = preorderCount(node->left, given, count); count = preorderCount(node->right, given, count); return count; } int main() { // Binary Search Tree creation Node* root = new Node("abc"); root->left = new Node("cba"); root->right = new Node("bac"); std::string givenString = "abc"; int tally = 0; int result = preorderCount(root, givenString, tally); std::cout << "Preorder count: " << result << std::endl; // TODO: Free memory by deleting the nodes return 0; }
Output
Preorder count: 3
Conclusion
This article gives an algorithmic approach to checking the hubs of a tree whose weighted string is a rearranged word of a given string. It clarifies two strategies: Depth-First Look (DFS) and Pre-order traversal. The DFS strategy includes recursively navigating the tree, calculating the weighted string at each hub, and comparing it with the given string. The Pre-order traversal strategy visits the hubs in a particular arrangement, calculates the weighted string, and gathers the check of hubs fulfilling the rearranged word condition. The given code illustrations illustrate the use of both strategies.