Removing duplicate subtrees from binary tree

前端 未结 3 652
清歌不尽
清歌不尽 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:22

    I would go with a hashing approach.

    A hash for a leaf is its value mod P_1. Hash for a node is (value+hash(left_son)*P_2+hash(right_son)*P_2^2) mod P_1, where P_1, P_2 are primes. If you count those hashes for at least 5 different big prime pairs(by big i mean something near 10^8-10^9, so you can do your math without overflowing), you can safely assume that nodes with same hashes are the same.

    Then you can walk the tree, checking sons, first and do your transform. This will work in O(n) time.

    NOTE that you can use other hash functions, like (value + hash(left_son)*P_2 + hash(right_son)*P_3) mod P_1, etc.

提交回复
热议问题