JSFiddle - React, Tailwind, and code Playground

by evgkch

JavaScript

// Get power of two that is an left neighborhood of a number
function get2PowerLeftNeighborhood(n){
	return Math.floor(Math.log2(n));
};

// @NOTE only for string that is length is power of two
function reverseStrRelativeMiddle(str){
	const strLength = str.length;
  return str.substr(strLength / 2) + str.substr(0, strLength / 2);
}

// Print tree to testing
function printRadjechTreeCol(tree, col){
  if (col == 0)
  	return tree;
  else
  	return printRadjechTreeCol(tree + reverseStrRelativeMiddle(tree), col - 1);
};

function getRadjechKinsman(generation, n){
  const m = get2PowerLeftNeighborhood(n);
  if (n > 1)
    return getRadjechKinsman(generation, n - Math.pow(2, m) + (n % 2 == 0 ? 1 : -1));
  else
    return n;
}

const dict = ['M', 'F'];

const tree20 = printRadjechTreeCol('MF', 20);

let ok = true;
for (let i = 0, length = tree20.length; i < length; i++)
{
	console.log(tree20[i],dict[getRadjechKinsman(20, i)])
  ok = ok && (tree20[i] == dict[getRadjechKinsman(20, i)]);
}

console.log(ok)