instructions
In a Number Tree puzzle, you must enter the numbers from 1 to N once each (for a puzzle with N circles), such that each non-leaf of the tree contains the sum of the numbers that it is connected to from above. The root and usually at least one other circle have numbers given to you, as shown in the example above.
For clarification:
Complete the NumberTree class described below:
Sample Input:
A B C D 4 A B E D 4 F 4 B G B C 19 E F G
The input str will consist of a sequence of node names and values separated by semi-colons. For example a 1; b 2; c 3;
Raise an invalid_argument exception if the input is not valid (doesn't parse, unknown node referenced, node is missing a value, value is out of range, etc.)
Important: Notice that this method is const. That means this method can't modify the NumberTree, only query it. Specifically, you need to store the solution values in a data structure that's local to the method. The benefit of this method being const is that you can now verify multiple solutions without having to "reset" the object.
Find a solution to the puzzle and return a map of node names to values. Return an empty map if no solution exists. You may find this function helpful:www.cplusplus.com/reference/algorithm/next_permutation/. Notice that this is a const function: It may not modify the NumberTree object.
Extra credit: Find a non-brute-force solution. In other words, find a solution that is more clever than just trying assignments of values to nodes until it find one that works.
Get Free Quote!
433 Experts Online