Removing duplicate subtrees from binary tree

前端 未结 3 653
清歌不尽
清歌不尽 2021-02-15 07:55

I have to design an algorithm under the additional homework. This algorithm have to compress binary tree by transforming it into DAG by removing repetitive subtrees and redirect

3条回答
  •  终归单人心
    2021-02-15 08:35

    This is a problem commonly solved to do common sub-expression elimination in programming languages.

    The approach is as follows (and is easily generalized to more than 2 children in a node):

    Algorithm (Assumes mutable tree structure; You can easily build a new tree along the way):

    MakeDAG(tree):
    
        HASH = a new hash-table-based dictionary
    
        foreach subtree NODE in the tree // traverse this however you like
    
            if NODE is in HASH
                replace NODE with HASH[NODE]
            else
                HASH[NODE] = N // insert the current node, N, in the dictionary
    

    To compute the hash code for a node, you need to recursively compute the hash nodes until you reach the leaves of the tree.

    Simply calculating these hash codes naively will bump up your runtime to O(n^2).

    It is crucial that you store the results on your way down the tree to avoid repeated recursive calls and to improve the runtime to O(n).

提交回复
热议问题