在javascript中实现一个方形树形图

use*_*310 8 javascript algorithm pseudocode treemap

我目前正在尝试在Javascript中实现树图算法.更具体地说,在Squarified Treemaps中描述的算法.给出的伪代码如下所示:

procedure squarify(list of real children, list of real row, real w)
begin
    real c = head(children);
    if worst(row, w) <= worst(row++[c], w) then
        squarify(tail(children),row++[c], w)
    else
        layoutrow(row);
        squarify(children,[], width());
    fi
end
Run Code Online (Sandbox Code Playgroud)

但我的JavaScript看起来像:

var c = children[0];
if (worst(row, w) >= worst(row.concat(c), w)) {
    this.squarify(children.splice(1), row.concat(c), w);
} else {
    layoutrow(row);
    this.squarify(children, [], width());
}
Run Code Online (Sandbox Code Playgroud)

据我所知,我的代码工作正常,但不平等是错误的方法.我假设我在实现中忽略了某些东西,或者伪代码中的不等式是不正确的?谢谢

nic*_*tic 4

当您想要添加c到当前row时,这样做会提高纵横比,即当

worst(row++[c], w) < worst(row, w)
Run Code Online (Sandbox Code Playgroud)

我最近在 github 上提交了一段代码,它在 TypeScript 中实现了该算法,并包含现成的 JavaScript:

https://github.com/nicnguyen/treemap