Correct Implementation of Min

Correct implementation of Min-Max in a tree

I find it a little unnatural that the evaluation_function, which receives a Node as input, actually updates the value for the parent node.

I feel that a better approach would be for that function to update the current node instead.

It should do nothing if the current node is a leaf.

Otherwise, it should iterate through all the children (as it currently does in the first loop), and after each recursive call for a child, it should also check whether the current node value should be updated (i.e. a better value was just encountered, for the current child).

In addition, it is possible to add an additional field to each node, e.g. bestChild, or bestMove, to avoid re-iterating through children when calling print-optimal.

What is the best implementation of min?

No, there isn't. The two implementations are equivalent under all defined circumstances. (An individual compiler might exhibit performance differences, but not functional differences.)

It would be different if the function weren't concerned solely with ints. Floating point numbers, as chux mentioned in a comment, can have both a<b and b<a false even when their bit patterns differ, as with negative/positive zero, or when at least one is a NaN. Technically this could also occur with an exotic (but standards-compliant) integer representation (through -0 or padding bits), but AFAIK no otherwise standards-compliant compiler does that.

Binary Tree: summation of all nodes between a min and max value

Think about it this way: the moment you reach a node which isn't in the desired range, your algorithm stops. What happens if the root itself is out of range? You'll just return 0 right off the bat.
You need to fix your exit condition so that your recursion stops only when you've reached a null, since you can't know how many nodes that are within range of [min, max] are present in the sub-tree of the node you're currently in. Then, in the recursion itself, you should make the decision whether to add the current node to the overall sum or not.

Possible implementation - spoilers ahead, I suggest you only look after you've solved it yourself:

private static int problem2(Node root, int min, int max){

if(root == null){
return 0;
}

int sum = 0;
// No point in keeping the recursion right/left if the current key is
// larger/smaller than the desired range - usage of BST property:
if (root.key <= max) {
sum += problem2(root.right, min, max);
}
if (root.key >= min) {
sum += problem2(root.left, min, max);
}
// If root is within range add it to sum:
if (root.key <= max && root.key >= min){
return root.key + sum;
} else {
return sum;
}

}

Explanation: each recursive call sums up the results of the left and right subtrees. The key of the node you're currently in is added to the calculation iff it is within the desired range of [min, max].

Is a sorted array a min-heap? What's the minimum value of a max-heap?

An array sorted from lowest to highest is a min-heap when using the array-based heap implementation. The min-heap property that the parent node value is less than or equal to it's child nodes (2i + 1 and 2i + 2, using zero-based arrays) holds for all nodes that have children.

The minimum value of a max heap is in one of the leaf nodes, but you don't know which. Since the minimum node cannot, by definition, have any child nodes, it must be a leaf. The heap property, however, does not specify how leaf nodes compare with each other, only with their parent.

Want to change a value continuously from min to max to min in a loop as a Sine curve

If the pseudocode you posted accurately mirrors the expressions you use in real code, you probably want to change the argument of sin to this:

2 * PI * (counter * SCHEDULE_INTERVAL) / timeDuration

counter is the number of executions, while timeDuration is (I presume) the desired length in seconds.

In other words, your units don't match - it's always worthwhile to perform a dimensional analysis when formulae don't work.

Time complexity to get min elements from max-heap

My question is that if this answer is correct.

No, that's not correct. The only guarantee you have, is that each node contains the maximum element of the subtree below it. In other words, the minimum element can be any leaf in the tree.

If not what is the correct answer?

The correct answer is O(n). In each step you need traverse both left and right sub-trees in order to search for the minimum element. In effect, this means that you need to traverse all elements to find the minimum.



Related Topics



Leave a reply



Submit