Figure 1:
GUI of the n-ary algorithm (its Pseudo code, see Figure 2).