递归函数和返回值需要更多帮助

发布于 2024-10-01 00:59:11 字数 1899 浏览 1 评论 0原文

我认为我的递归工作正常,但我无法将最初请求的项目的属性设置为正确的值。

这里的目标是找到嵌套项的最顶层父级(或“祖先”,深度 = 0)(基于字典“父级”属性的分类法),并相应地分配最初请求的嵌套项的“祖先”属性。

例如,在

苹果中
- 红色
- - 帝国
- - - Fresh

“Fresh”的祖先应设置为“Apples”。

当我在“按需”和个人的基础上尝试这一点时,我愿意接受一种解决方案,该解决方案可以一次性或 for 语句标记与同一祖先相关的所有子项,因为这可能会更有效。

请求

for (var mc:Object in taxonomy) {
        var term = taxonomy[mc];
        term["ancestor"] = getAncestor(term);
        trace("Setting " + term.name + "'s ancestor as [" + term.ancestor + "]");
        ... }

功能

function getAncestor(term:Object):String {

    var ancestor = "default";

    for(var obj:Object in taxonomy) {
        if(term.parent == taxonomy[obj].tid) { // If next object is current object's parent
            if(taxonomy[obj].depth == 0) { // And if object's parent is a root object
                // Then object's parent is the ancestor
                trace(term.name + "'s parent IS the root (" + taxonomy[obj].name + "). DONE."); // "term" here is NOT originally requested term
                return(taxonomy[obj].name); // Return DIRECTLY to function call and assign originally requested term with this name.
                break; // Get the hell out of here
            }
            else { // If object's parent is not a root object
                trace(term.name + "'s parent (" + taxonomy[obj].name + ") is NOT a root. LOOPING.");
                getAncestor(taxonomy[obj]); // Step function again with current object's parent object as current object
            }
        }
    }
    return(ancestor);
}

最后,这里是基于我的许多调试语句的跟踪输出的片段:

治疗的父级(饮食失调)不是根。循环。
饮食失调的父母(心身)不是根。循环。
心身的父母(疾病)不是根源。循环。
疾病的父级(个人)不是根。循环。
Personal 的父级(Health)不是根。循环。
健康的父母是根(人)。完成。
将处理的祖先设置为[默认]

如您所见,虽然递归确实找到了根,但最初请求的项目仍然获得默认值。我缺少什么?

I think I have the recursion working properly, but I'm not able to get the originally requested item's property set to the right value.

The goal here is to find the topmost parent (or "ancestor," depth = 0) of the nested item (a taxonomy based on Dictionary "parent" attributes), and assign that originally requested nested item's "ancestor" property accordingly.

For instance, in

Apples
- Red
- - Empire
- - - Fresh

"Fresh"'s ancestor should be set to "Apples."

While I'm attempting this on an "on-demand" and individual basis, I'm open to a solution that tags all children that are related to the same ancestor in one fell swoop or for statement, since that would probably be more efficient.

REQUEST

for (var mc:Object in taxonomy) {
        var term = taxonomy[mc];
        term["ancestor"] = getAncestor(term);
        trace("Setting " + term.name + "'s ancestor as [" + term.ancestor + "]");
        ... }

FUNCTION

function getAncestor(term:Object):String {

    var ancestor = "default";

    for(var obj:Object in taxonomy) {
        if(term.parent == taxonomy[obj].tid) { // If next object is current object's parent
            if(taxonomy[obj].depth == 0) { // And if object's parent is a root object
                // Then object's parent is the ancestor
                trace(term.name + "'s parent IS the root (" + taxonomy[obj].name + "). DONE."); // "term" here is NOT originally requested term
                return(taxonomy[obj].name); // Return DIRECTLY to function call and assign originally requested term with this name.
                break; // Get the hell out of here
            }
            else { // If object's parent is not a root object
                trace(term.name + "'s parent (" + taxonomy[obj].name + ") is NOT a root. LOOPING.");
                getAncestor(taxonomy[obj]); // Step function again with current object's parent object as current object
            }
        }
    }
    return(ancestor);
}

Finally, here is a snippet of the traced output based on my many debug statements:

treatment's parent (Eating Disorders) is NOT a root. LOOPING.
Eating Disorders's parent (Psychosomatic) is NOT a root. LOOPING.
Psychosomatic's parent (Disease/Illness) is NOT a root. LOOPING.
Disease/Illness's parent (Personal) is NOT a root. LOOPING.
Personal's parent (Health) is NOT a root. LOOPING.
Health's parent IS the root (People). DONE.
Setting treatment's ancestor as [default]

As you can see, while the recursion does find the root, the originally requested item still gets the default value. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三五鸿雁 2024-10-08 00:59:11

我猜测您想要的 else 语句是:

ancestor = getAncestor(taxonomy[obj]);

现在您正在调用递归,但对返回值不执行任何操作,因此您永远不会更新祖先变量。

此外,return 语句之后的 break 是毫无意义的。 :)

如果我理解正确的话,你实际上可能可以这样做:

return getAncestor(taxonomy[obj]);

否则你的循环将继续运行。如果没有这个,你的循环将遍历分类中的所有内容,即使它看到的第一个是它递归的那个。

I'm guessing in that else statement you want:

ancestor = getAncestor(taxonomy[obj]);

Right now you're calling the recursion but doing nothing with the return value, so you're never updating the ancestor variable.

Also, that break after the return statement is rather pointless. :)

If I'm understanding things correctly, you might actually be able to do:

return getAncestor(taxonomy[obj]);

Otherwise your loop will keep running. Without that your loop will go over everything in the taxonomy, even if the first one it sees is the one it recurses on.

掌心的温暖 2024-10-08 00:59:11

也许我错过了一些东西,但我不明白为什么你需要使用递归来做到这一点。在普通函数内,只需循环遍历目标的父对象,直到找到根对象,然后将 Fresh 的祖先设置为该对象。

Maybe I'm missing sometehing, but I don't see why you need to use recursion to do this. Inside a normal function, just loop through parent objects of the target until you find one that is the root, then set Fresh's ancestor to that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文