正确地从树中删除节点

发布于 2024-11-08 07:48:59 字数 800 浏览 6 评论 0原文

我有以下函数来修剪树数据结构:

public static void pruneTree(final ConditionTreeNode treeNode) {

    final List<ConditionTreeNode> subTrees = treeNode.getSubTrees();

    for (ConditionTreeNode current : subTrees) {
        pruneTree(current);
    }

    if(subTrees.isEmpty()) {
        final ConditionTreeNode parent = treeNode.getParent();
        parent.removeConditionTreeNode(treeNode);
    }

    if (treeNode.isLeaf()) {
        //this is the base case
        if (treeNode.isPrunable()) {
            final ConditionTreeNode parent = treeNode.getParent();
            parent.removeConditionTreeNode(treeNode);
        }
        return;
    }

}

我想知道修剪它的最佳方法是什么。我当前收到 ConcurrentModificationExceptions,并且我读到您可以复制集合,然后删除原始集合,或者从迭代器中删除。有人可以帮助我了解我需要做什么才能使此方法发挥作用吗?

I have the following function that prunes a tree data structure :

public static void pruneTree(final ConditionTreeNode treeNode) {

    final List<ConditionTreeNode> subTrees = treeNode.getSubTrees();

    for (ConditionTreeNode current : subTrees) {
        pruneTree(current);
    }

    if(subTrees.isEmpty()) {
        final ConditionTreeNode parent = treeNode.getParent();
        parent.removeConditionTreeNode(treeNode);
    }

    if (treeNode.isLeaf()) {
        //this is the base case
        if (treeNode.isPrunable()) {
            final ConditionTreeNode parent = treeNode.getParent();
            parent.removeConditionTreeNode(treeNode);
        }
        return;
    }

}

and I want to know what the best way to prune this is. I'm getting ConcurrentModificationExceptions currently, and I've read that you can copy the collection, and remove the original -- or remove from an iterator. Can someone help me understand what I need to do inorder for this method to work?

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

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

发布评论

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

评论(2

凌乱心跳 2024-11-15 07:48:59

问题是,您正在迭代节点集合,并且在某些情况下从递归调用内的集合中删除实际项目。您可以从递归调用中返回一个布尔标志来指示要删除的实际项目,然后通过 Iterator.remove() 删除它(您需要将 foreach 循环更改为迭代器循环使这成为可能)。

用其唯一的子节点替换实际项目比较棘手 - 您可以定义一个自定义类以从递归方法调用中返回更多信息,但这开始变得尴尬。或者您可以考虑使用堆栈等循环来替换递归调用。

The problem is, you are iterating through the collection of nodes and in some cases removing the actual item from the collection inside the recursive call. You could instead return a boolean flag from the recursive call to sign that the actual item is to be removed, then remove it via Iterator.remove() (you need to change the foreach loop to an iterator loop to make this possible).

Replacing the actual item with its only subnode is trickier - you could define a custom class to return more info from the recursive method call, but it starts to become awkward. Or you may consider replacing the recursive call with a loop using e.g. a stack.

窝囊感情。 2024-11-15 07:48:59
public boolean remove( int target )
{
    if( find( target ) )    //TreeNode containing target found in Tree
    {
        if( target == root.getInfo( ) ) //target is in root TreeNode
        {
            if( root.isLeaf( ) )
          root = null;
      else if( root.getRight( ) == null )
          root = root.getLeft( );
      else if( root.getLeft( ) == null )
          root = root.getRight( );
      else
      {
          root.getRight( ).getLeftMostNode( ).setLeft( root.getLeft( ) );
          root = root.getRight( );
      }
        }
        else    //TreeNode containing target is further down in Tree
        {
            if( cursor.isLeaf( ) )  //TreeNode containing target has no children
      {
          if( target <= precursor.getInfo( ) )  
              precursor.removeLeftMost( );
          else
        precursor.removeRightMost( );
            }
      else if( cursor.getRight( ) == null ) //TreeNode containing target        
      {                 //has no right subtree
          if( target <= precursor.getInfo( ) )
              precursor.setLeft( cursor.getLeft( ) );
          else
        precursor.setRight( cursor.getLeft( ) );
      }
      else if( cursor.getLeft( ) == null )  //TreeNode containing target
      {                 //has no left subtree
          if( target <= precursor.getInfo( ) )
                    precursor.setLeft( cursor.getRight( ) );
          else
        precursor.setRight( cursor.getRight( ) );
      }
      else  //TreeNode containing target has two subtrees
      {
          cursor.getRight( ).getLeftMostNode( ).setLeft( cursor.getLeft( ) );
          if( target <= precursor.getInfo( ) )
                    precursor.setLeft( cursor.getRight( ) );
          else
        precursor.setRight( cursor.getRight( ) );
            }
        }

        cursor = root;
        return true;
    }
    else    //TreeNode containing target not found in Tree          
        return false;
}
public boolean remove( int target )
{
    if( find( target ) )    //TreeNode containing target found in Tree
    {
        if( target == root.getInfo( ) ) //target is in root TreeNode
        {
            if( root.isLeaf( ) )
          root = null;
      else if( root.getRight( ) == null )
          root = root.getLeft( );
      else if( root.getLeft( ) == null )
          root = root.getRight( );
      else
      {
          root.getRight( ).getLeftMostNode( ).setLeft( root.getLeft( ) );
          root = root.getRight( );
      }
        }
        else    //TreeNode containing target is further down in Tree
        {
            if( cursor.isLeaf( ) )  //TreeNode containing target has no children
      {
          if( target <= precursor.getInfo( ) )  
              precursor.removeLeftMost( );
          else
        precursor.removeRightMost( );
            }
      else if( cursor.getRight( ) == null ) //TreeNode containing target        
      {                 //has no right subtree
          if( target <= precursor.getInfo( ) )
              precursor.setLeft( cursor.getLeft( ) );
          else
        precursor.setRight( cursor.getLeft( ) );
      }
      else if( cursor.getLeft( ) == null )  //TreeNode containing target
      {                 //has no left subtree
          if( target <= precursor.getInfo( ) )
                    precursor.setLeft( cursor.getRight( ) );
          else
        precursor.setRight( cursor.getRight( ) );
      }
      else  //TreeNode containing target has two subtrees
      {
          cursor.getRight( ).getLeftMostNode( ).setLeft( cursor.getLeft( ) );
          if( target <= precursor.getInfo( ) )
                    precursor.setLeft( cursor.getRight( ) );
          else
        precursor.setRight( cursor.getRight( ) );
            }
        }

        cursor = root;
        return true;
    }
    else    //TreeNode containing target not found in Tree          
        return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文