Count the nodes of a tree whose weighted string does not contain any duplicate characters


To identify the hubs of a tree in preparation for a depth-first look (DFS) traversal and whose weighted string does not include any copy characters. We travel through each hub, starting at the root hub, keeping track of the characters we've already encountered in the weighted string. We stop providing navigational support down that path if we run upon a character who is already present in the collection. For each hub we pass throughout the traverse, we raise a check variable. The count variable will indicate the number of hubs in the tree whose weighted string does not include any copy characters after the DFS traversal is complete.

Methods Used

  • DFS

  • Recursive approach

DFS

A algorithm called Depth-First Search (DFS) is used to move deeper through a tree. When examining hubs of a tree whose weighted string does not include copy characters, DFS starts at the root hub and probes each direction as much as is humanly possible, occasionally going backwards. A group of knowledgeable characters is maintained on guard during the traverse to look for duplicates. The traversal halts in that course if a copy character is discovered. DFS efficiently determines the number of necessary hubs in the tree by raising a number variable for each node that has since died.

Algorithm

  • Begin at the root hub of the tree.

  • Initialise a number variable to 0.

  • Initialise a purge set to keep track of experienced characters.

  • Perform a DFS traversal of the tree:

  • If the current hub is invalid, return.

  • Add the character of the current hub to the experienced character set.

  • If the included character is, as of now, within the set, halt navigating assistance down that path.

  • Otherwise, increase the number of variables by 1.

  • Recursively navigate the cleared-out child of the current node.

  • Recursively go to the current node's correct child.

  • The count variable will contain the number of hubs in the tree whose weighted string does not include copy characters once the DFS traversal is complete.

  • Give the cheque back.

Example

#include <iostream>
#include <fstream>
#include <set>
using namespace std;

// Global variables
int count = 0;
set<char> purgeSet;

// Structure representing a hub node
struct Hub {
   char character;
   Hub* child1;
   Hub* child2;
};

// Recursive DFS traversal function
void traverseHub(Hub* hub) {
   if (hub == nullptr)
      return;
    
   purgeSet.insert(hub->character);
    
   if (purgeSet.count(hub->character) > 1)
      return;
    
   count++;
    
   traverseHub(hub->child1);
   traverseHub(hub->child2);
}

int main() {
   // Create a sample tree
   Hub* root = new Hub{'A', nullptr, nullptr};
   root->child1 = new Hub{'B', nullptr, nullptr};
   root->child2 = new Hub{'C', nullptr, nullptr};
   root->child1->child1 = new Hub{'D', nullptr, nullptr};
   root->child1->child2 = new Hub{'E', nullptr, nullptr};
   root->child2->child1 = new Hub{'F', nullptr, nullptr};
   root->child2->child2 = new Hub{'G', nullptr, nullptr};

   // Traverse the tree
   traverseHub(root);

   // Output the count
   cout << "Number of hubs with unique characters: " << count << endl;

   // Clean up the allocated memory
   // ...

   return 0;
}

Output

Number of hubs with unique characters: 7

Recursive Approach

The recursive approach includes executing recursive work. Begin with a hub and a set of experienced characters. If the hub is invalid, return 0. Check in case the character of the current hub is as of now experienced. In the event that this is the case, return 0. Something else: include the character in the set. Recursively number the hubs within the cleared-out and right subtrees, passing the upgraded set of experienced characters. Increase the number by 1 and return the entirety of the tallies from both subtrees. This approach recursively investigates each hub, checking for copies and increasing the tally as it were in case no copies are found, guaranteeing the weighted string does not contain any copy characters.

Algorithm

  • Begin with a hub and a set of experienced characters.

  • If the hub is invalid, return 0.

  • Check on the off chance that the character of the current hub is now displayed within the set of experienced characters.

  • If there's a copy character, return 0.

  • Otherwise, add the character to the set of experienced characters.

  • Recursively call the calculation for the cleared-out subtree, passing the upgraded set of experienced characters, and store the result.

  • Recursively call the calculation for the correct subtree, passing the upgraded set of experienced characters, and store the result.

  • Increment the check by 1.

  • Return the entirety of the checks gotten from both subtrees, plus the increased check.

Example

#include <iostream>
#include <unordered_set>

struct Node {
   char data;
   Node* left;
   Node* right;
};

Node* createNode(char data) {
   Node* newNode = new Node();
   newNode->data = data;
   newNode->left = nullptr;
   newNode->right = nullptr;
   return newNode;
}

int countNodes(Node* node, std::unordered_set<char>& encounteredChars) {
   if (node == nullptr)
      return 0;
    
   if (encounteredChars.find(node->data) != encounteredChars.end())
      return 0;
    
   encounteredChars.insert(node->data);
    
   int leftCount = countNodes(node->left, encounteredChars);
   int rightCount = countNodes(node->right, encounteredChars);
    
   return leftCount + rightCount + 1;
}

int main() {
   Node* root = createNode('A');
   root->left = createNode('B');
   root->right = createNode('C');
   root->left->left = createNode('D');
   root->left->right = createNode('E');
   root->right->right = createNode('F');
    
   std::unordered_set<char> encounteredChars;
   int totalCount = countNodes(root, encounteredChars);
    
   std::cout << "Total number of nodes: " << totalCount << std::endl;
    
   return 0;
}

Output

Total number of nodes: 6

Conclusion

This article gives an algorithmic approach to checking the number of hubs in a tree whose weighted string does not contain any copy characters. It presents a step-by-step handle employing a depth-first look (DFS) traversal to productively navigate the tree and keep up a set of experienced characters. By checking for copy characters and augmenting a number variable for each special hub, the algorithm precisely counts the specified hubs within the tree. The code cases in C illustrate diverse executions of the calculation for errands such as tallying hubs, checking for copy characters, and finding the height of a twofold tree.

Updated on: 19-Jul-2023

32 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements