Issue Statement
In this scenario, you have been presented with a tree consisting of N nodes that are rooted at 1. Each node in the tree is associated with a special number, Se. Moreover, each node possesses a certain Power, which is determined by the count of heavy nodes in the subtree of the respective node (inclusive of the node itself). A heavy node is defined as a node whose total sum of divisors of its special number is a multiple of 3. There are Q queries provided.
The two types of queries available are:
Type 1: Update the special number of a specific node.
Type 2: Determine the power of a given node.
Input Structure
Initial Line: Two integers N and Q separated by space indicating the total number of nodes in the tree and the number of queries respectively. Subsequently, each of the following N-1 lines includes two space-separated integers U and V signifying an edge between them. Next line: N space-separated integers where i denotes the special number connected to node i. The first integer in the next Q lines stands for T (the query type). If T is 1, it is succeeded by two integers X and Y specifying that the special number S of node X needs to be updated to Y. In case T is 2, it will be followed by a single integer X.
Outcome Scheme
For every Type 2 query, disclose the power of the specified node X. Each query's response should be displayed on a new line.
Explanation of the Problem Statement Essentially, we are confronted with a tree comprising numbered nodes (Si), some of which are likely to have child nodes (as per the tree structure).
The issue seeks to ascertain the "Power" of a node, wherein power is interpreted as the count of "heavy nodes" among its children.
A "heavy node" is identified as a node whose sum of divisors for Si is a multiple of 3.
Several assessments need to be made here:
* For a given node, identification of all its child nodes is imperative
* Determination of the divisors for each child node's number is essential
* Aggregation of the divisors and validation of divisibility by 3
Provided Sample Input and Outputs Like
Sample Input
5 5
1 2
1 3
3 4
3 5
16 8 17 3 18
2 1
2 3
1 3 7
2 1
2 3
Sample Output
3
2
2
1
Code That I Attempted
function BinarySearchTree() {
this.root = null;
}
BinarySearchTree.prototype.insertNode = function (val) {
var node = {
data : val,
left : null,
right : null
};
var currentNode;
if (!this.root) {
this.root = node;
} else {
currentNode = this.root;
while (currentNode) {
if (val < currentNode.data) {
if (!currentNode.left) {
currentNode.left = node;
break;
} else {
currentNode = currentNode.left;
}
} else if (val > currentNode.data) {
if (!currentNode.right) {
currentNode.right = node;
break;
} else {
currentNode = currentNode.right;
}
} else {
break;
}
}
}
};
var BST = new BinarySearchTree();
BST.insertNode(16);
BST.insertNode(8);
BST.insertNode(17);
BST.insertNode(3);
BST.insertNode(18);
console.log(BST);
My Output
BinarySearchTree {
root:
{ data: 16,
left: { data: 8, left: [Object], right: null },
right: { data: 17, left: null, right: [Object] } } }
Now, my objective is to convert this data into an array like [16, 8, 17, 3, 18]
and then proceed to calculate the divisors of each node and determine whether these divisors are divisible by 3 or not. What would be the appropriate approach for accomplishing this?
Is the method employed correct?