
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
Number of Full Binary Trees Where Each Node is Product of Its Children
A full binary tree is a special type of binary tree in which all the parent nodes either have two or no children. In data structures, these kinds of trees are considered as balanced and organized representation. Full binary trees may have a unique feature where each of the parent node is a product of its children.
In this article, we will discuss about different methods for counting the number of full binary trees such that each node is product of its children using C++.
Input Output Scenarios
For example, in the array {1, 5, 3, 4}, we have only four single nodes 1 (1 x 1), 5 (1 x 5), 3 (1 x 3) and 4 (1 x 4).
Input: arr = {1, 5, 3, 4} Output: 4
In a given array, using all the multiples of each element less than the maximum value, we can make a full binary tree if the multiple is present in the array. Hence, we find the maximum value in the array.
We start finding such binary trees by iterating from the minimum value to the maximum value since we know that the smaller values may be the multiple of the larger values in the array.
Using Dynamic Programming
Here, we use dynamic programming to find the number of full binary trees such that each node is a product of its children. We iterate over the array elements and find the possible trees which satisfy the above condition. We store these solutions in dp vector.
First, we find the maximum and minimum value in the array using the INT_MAX and INT_MIN constants which are defined in the <climits> header in C++. The maximum and minimum values are updated by iterating a for loop.
Next, we have a nested loop in which we iterate from the minimum value to the maximum value of the array. In this loop, we check if the dp vector is non-zero or not.
If the dp vector is non-zero then, we run another for loop over the multiples of j starting from (j + j) till the maximum value. For each multiple, we check if multiple is present or not.
If multiple is present then, the number of possible full binary trees is equal to the product of number of full binary trees of arr[j] and number of full possible binary trees of arr[j]/k.
We add current updated dp value modulo 1000000007 to the result to prevent overflow of data.
Example
#include <iostream> #include <algorithm> #include <vector> #include <climits> using namespace std; int numOfFullBinaryTrees(int arr[], int N) { int minValue = INT_MAX; int maxValue = INT_MIN; // Find the maximum and minimum value from the array for (int j = 0; j < N; j++) { minValue = min(minValue, arr[j]); maxValue = max(maxValue, arr[j]); } vector < int > dp(maxValue + 1, 0); // One possible full binary tree for each element // in case of single nodes for (int j = 0; j < N; j++) { dp[arr[j]] = 1; } int result = 0; for (int j = minValue; j <= maxValue; j++) { if (dp[j] != 0) { for (int k = j + j; k <= maxValue && (k / j) <= j; k += j) { if (dp[k] != 0) { dp[k] += (dp[j] * dp[k / j]); // Check if left child may become right child and vice versa if (j != (k / j)) { dp[k] += (dp[j] * dp[k / j]); } } } result = (result + dp[j]) % 1000000007; } } return result; } int main() { int array[] = {12, 3, 5, 6}; int N = sizeof(array) / sizeof(array[0]); cout << "Number of full binary trees satisfying the condition are: " << numOfFullBinaryTrees(array, N) << endl; return 0; }
Output
Number of full binary trees satisfying the condition are: 4
Note ? Here, the time complexity for this program is O(N^2).
Conclusion
We have discussed how to find the number of full binary trees such that each node is product of its own children. We have used the dynamic approach to solve this problem by storing the solutions of sub problems in dp vector. We may use simple nested for loops iterating from the minimum to the maximum value of the array and checking the possible number of full binary trees with the desired property.