布尔递归

发布于 2024-10-18 02:57:20 字数 380 浏览 1 评论 0原文

试图编写一个布尔方法来判断某人是否是某人的后代......但似乎无法做到这一点。当然,如果该对象是孩子……或者孩子的后代,那么它就是后代。

public boolean isDescendant(member x){
    if (children.contains(x)){
        return true;
    }
    else{
        return false;
    }
}

但我在哪里或如何插入:

for (int i = 0; i < children.size(); i++){
    isDescendant(children.get(i));
}

谢谢!

trying to write a boolean method that tells if someone is a decendant of someone...but can't seem to do it. of course, the object is a descendant if it's a child...or the descendant of a child.

public boolean isDescendant(member x){
    if (children.contains(x)){
        return true;
    }
    else{
        return false;
    }
}

but where or how do i insert:

for (int i = 0; i < children.size(); i++){
    isDescendant(children.get(i));
}

thanks!

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

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

发布评论

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

评论(4

甜嗑 2024-10-25 02:57:20

我想你想要的如下:

// Cleaned up version
public boolean isDescendant(member x){
    // check for direct descendance 
    if (children.contains(x)){
        return true;
    }
    // check for being descendant of the children
    for (Child c: children){
        if (children.get(i).isDescendant(x)) {
            return true;
        }
    }
    return false;
}

I think what you want is below:

// Cleaned up version
public boolean isDescendant(member x){
    // check for direct descendance 
    if (children.contains(x)){
        return true;
    }
    // check for being descendant of the children
    for (Child c: children){
        if (children.get(i).isDescendant(x)) {
            return true;
        }
    }
    return false;
}
伤感在游骋 2024-10-25 02:57:20

树木向下行走的速度非常慢(从根部到叶子)。考虑 is-ancestor 检查的这种实现:

/**
 * Checks whether the given node is an ancestor of this node.
 */
public boolean isDescendantOf(Node ancestor) {
    Preconditions.checkNotNull(ancestor, "Ancestor");
    if (equals(ancestor)) {
        // every node is an ancestor to itself
        return true;
    } else if (parent == null) {
        // not related
        return false;
    } else {
        // recursive call
        return parent.isDescendantOf(ancestor);
    }
}

另一种方法现在是小菜一碟。

public boolean isDescendant(Node descendant) {
    return descendant.isDescendantOf(this);
}

没有循环,没有指数级的努力。

PS:
在我的示例中,我建议将 isDescendant 重命名为 isAncestorOf。

Walking trees is very slow downwards (from the root to the leaves). Consider this implementation for the is-ancestor check:

/**
 * Checks whether the given node is an ancestor of this node.
 */
public boolean isDescendantOf(Node ancestor) {
    Preconditions.checkNotNull(ancestor, "Ancestor");
    if (equals(ancestor)) {
        // every node is an ancestor to itself
        return true;
    } else if (parent == null) {
        // not related
        return false;
    } else {
        // recursive call
        return parent.isDescendantOf(ancestor);
    }
}

The other way is now a piece of cake.

public boolean isDescendant(Node descendant) {
    return descendant.isDescendantOf(this);
}

No loops, no exponentional effort.

PS:
In my example i would suggest renaming isDescendant to isAncestorOf.

等待圉鍢 2024-10-25 02:57:20
public boolean isDescendant(member currentRoot, member x){
    //check the current level
    if (currentRoot.children().contains(x)){
        return true;
    }

    //leaf
    if( currentRoot.children().isEmpty() ){ return false; }

    //try all my children
    boolean found = false;
    for( Member child : currentRoot.children() ){
       found = isDescendant( child, x );
       if( found ) break;
    }

    return found;
}

您很可能需要递归当前的根。

public boolean isDescendant(member currentRoot, member x){
    //check the current level
    if (currentRoot.children().contains(x)){
        return true;
    }

    //leaf
    if( currentRoot.children().isEmpty() ){ return false; }

    //try all my children
    boolean found = false;
    for( Member child : currentRoot.children() ){
       found = isDescendant( child, x );
       if( found ) break;
    }

    return found;
}

You need to recurse over the current root, most likely.

↙厌世 2024-10-25 02:57:20

编辑:如果您的数据结构有父指针,请使用这些指针而不是在树中搜索您的后代。如果没有,请考虑添加它们。请参阅威士忌塞拉 (whiskysierra) 的答案,了解带有父指针的解决方案。仅当无法添加它们时,才考虑这个答案。


当前的答案都有两个通过子项的循环(一个在 children.contains() 中,一个在后面)。

这个变体可能更高效一些(但它不会改变 O 级),并且更短一些。 (如果children是一个具有快速包含检查的集合(如HashSet),并且层次结构通常不是那么深(因此您根本不需要递归),那么其他答案更好。)

public boolean isDescendant(Member x) {
   for(Member child : children) {
      if(child.equals(x) || child.isDescendant(x))
        return true;
   }
   return false;
}

如果一个节点被认为是后代就其本身而言,你可以这样写:

public boolean isDescendant(Member x) {
   if(equals(x))
      return true;
   for(Member child : children) {
      if(child.isDescendant(x))
        return true;
   }
   return false;
}

Edit: If your data structure has parent pointers, use these instead of searching your descendants in the tree. If not, consider adding them. See the answer from whiskeysierra for a solution with parent pointers. Only if adding them is not possible, consider this answer.


The current answers all have two loops through children (one in children.contains(), one later).

This variant may be a bit more efficient (but it does not change the O-class), and is a bit shorter. (If children is a set with fast contains-check (like HashSet) and often the hierarchy is not so deep (so you don't need to recurse at all), the other answers are better.)

public boolean isDescendant(Member x) {
   for(Member child : children) {
      if(child.equals(x) || child.isDescendant(x))
        return true;
   }
   return false;
}

If a node is considered a descendant of itself, you can write it like this:

public boolean isDescendant(Member x) {
   if(equals(x))
      return true;
   for(Member child : children) {
      if(child.isDescendant(x))
        return true;
   }
   return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文