簡體   English   中英

從樹中正確刪除節點

[英]Removing nodes from a tree properly

我有以下修剪樹數據結構的功能:

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,並且我讀到您可以復制集合並刪除原始集合,也可以從迭代器中刪除。 有人可以幫助我了解我需要做些什么才能使這種方法起作用嗎?

問題是,您要遍歷節點的集合,並且在某些情況下,需要在遞歸調用中從集合中刪除實際項。 您可以從遞歸調用中返回一個布爾值標志,以指示要刪除實際項,然后通過Iterator.remove()將其刪除(您需要將foreach循環更改為迭代器循環,以實現此目的)。

用唯一的子節點替換實際項比較棘手-您可以定義一個自定義類以從遞歸方法調用中返回更多信息,但開始變得笨拙。 或者您可以考慮使用例如堆棧的循環替換遞歸調用。

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM