当前位置: 首页 > 知识库问答 >
问题:

从树上移除子树

宰父焕
2023-03-14
 public void removeChild(TreeNode childToRemove)
  {
           if(this.children.isEmpty())
           {
             return ;
           }
           else if(this.children.contains(childToRemove))
           {
               this.children.remove(childToRemove);
               return;
           }
           else
           {
             for(TreeNode child : this.children)
             {
                 child.removeChild(childToRemove);
             }
           }
  }

共有1个答案

燕经国
2023-03-14

正如我在评论中所写的,我假设:

  • 此方法位于类TreeNode中。
  • this.children具有类型list .

即。这是一个基本的树状数据结构。

/**
 * Remove the given node from this TreeNode.
 *
 * @param childToRemove the child to be removed
 * @return true if the child was removed, false otherwise.
 */
public boolean removeChild(TreeNode childToRemove)
{
           // if this tree is empty, there's nothing to do
           if(this.children.isEmpty())
           {
             return false;
           }

           // there's no point checking with "contains", just try
           // to remove the child, and if that succeeds, we're done.
           if (this.children.remove(childToRemove)) 
           {
             return true;
           }

           // the child is not a direct child of this TreeNode, but it
           // may be an indirect child, i.e. a descendant of this node,
           // so we need to try every single descendant in this tree,
           // stopping as soon as we find it to avoid wasting time.
           for(TreeNode child : this.children)
           {
               if (child.removeChild(childToRemove))
               {
                   return true;
               }
           }

           // we couldn't find the node anywhere, so it couldn't be removed.
           return false;
}
 类似资料:
  • 我正在制作一个按字符串键排序的二叉搜索树。每个节点由一个与一个键相关联的无序信息链表组成。这棵树是按字母顺序排列的。 我已经完成了大部分的程序,但有麻烦的删除方法。 谢谢你。

  • 大家早上好。我有以下代码: 问题是,uri变量被分配给RestTemplate构造函数为,也就是说,它从代码变量中删除初始的。

  • 问题内容: 如何从网络应用程序中嵌入的iframe中删除边框?iframe的一个示例是: 假设背景颜色一致,我希望从页面上的内容到iframe的内容的过渡是无缝的。目标浏览器仅是IE6,遗憾的是,其他浏览器的解决方案将无济于事。 问题答案: 添加属性(注意 大写字母’B’ )。 因此,它看起来像:

  • 问题内容: 可能是一个很糟糕的问题,但是我使用的是Django的UserCreationForm(经过稍微修改以包括电子邮件),并且我想删除Django自动显示在HTML页面上的help_text。 在我的HTML页面的“注册”部分,它具有“用户名”,“电子邮件”,“密码1”和“密码2”字段。但是用户名下面是“必填。必须少于30个字符。仅字母,数字和@ …”。在“密码确认”(密码2)下,显示“输入

  • 问题内容: 我试图了解如何从Firebase中删除项目。我已经建立了一个函数()创建一个项目,但无法弄清楚如何删除一个项目。 HTML JS 我已经尽力创建了一个名为的函数,但无法计算出要放入其中的内容。我意识到我必须以某种方式定位特定项目,然后将其从列表中删除。我只是不确定如何。 任何帮助,将不胜感激。提前致谢! 问题答案: 要从Firebase中删除项目,您需要知道它,它是在您调用将新项目添加

  • 我有一个带有子pom B、C和D的父pom A,在B、C和D中,我有相同类型的依赖项。 ie父Pom 儿童Pom 到目前为止,我已经尝试过编辑插件surefire,当试图排除子pom的测试范围时,它不起作用。 我的问题是,是否有一种方法可以将概要文件添加到父pom中,以排除所有子pom的类型测试jar的所有依赖项。谢谢