andreamad8 / Algo2Problems

2016/1027

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EX 14

t-costa opened this issue · comments

The solution seems correct to me, the only thing I think is to fix is the position of the pad to infinite. Probably, it's better to insert as a padding when needed the next element in the sorted array, and only when there is no such element the infinite (or the sequence of infinite). In this way, in the example, the last leaf becomes (22,23), the father (21, 24) and all the other nodes on the right have their values increased by 1. In this way, the infinite should always be at the rightmost position in the tree.

EDIT: I wrote that the leaf becomes (21,23), my fault, it's (22,23).

Could you please explain a bit better your idea, I don't get it.
I have tried some instance, and it seams work.

Citing the solution, "For the last level we need exactly L elements, we traverse the sorted array A from left to right selecting B elements and then skipping the next, then taking the B following, then skipping the next and so on, until we select L elements. This way we build the L/B blocks (i.e. nodes) for the last level. It is possible that the last block selected this way remain half empty, we pad with ∞ in that case."
I suggest to pad not with infinite, but with the next elements in the starting array, if such elements exist. If there are no more elements to add, it means that the last element we put in the leaf is the biggest, so in this case it's correct to pad with infinite. Otherwise, we build a tree with an infinite as an element that is less than another (in the example in the solution, we have that the infinite is in a position where should be elements between 21 and 23).
In practice, the proposed solution works, it also as the benefit that even with a large B, we can always save a single infinite, because it is at the and of the array, but I don't know if it is logically correct. Instead, using as padding the other elements of the array we build a tree where all the elements respect the given structure (the possibly many infinite are always greater than any other element), but the pad could be in the rightmost node at any level of the tree, it depends on the number of elements, so if we have to add B-1 infinite, we have to save all of them or use some other method (I don't know which one, maybe a counter of the number of infinite) to use O(1) additional memory.
I hope it's clearer, the rest of the solution doesn't change.

yep, now more or less I got. If you have time, could you please add a note, at the end or in a place in which is understandable, in the latex file.
Where you simply explain your idea, and the best could be if you provide an example, even a picture of your draft book (quaderno), where you show how does it work
For could be useful for everyone, obviously just if u have time. 👍
ps: you can also work on your file in case I will merge for you

@delleciliege what do you think?

Yes, I don't like the position of ∞ too, but the procedure is as simple as possible and in my opinion this is the most important thing.
Your alternative is absolutely welcome if you have time to write it, but what you think about simply substitute ∞ with "nan" or some other special symbol?
Even because in the actual form, when you store the tree in memory in linear form, the ∞ are all at the end of the array and can be simply removed.
By the way thank you a lot for the feedback!

I tried to formalize my alternative solution, and it's uselessly complicated. @delleciliege you are right, probably the best thing to do is to replace the infinite with a NaN or another special symbol, just for "autistic ordering"! :P