1

Given a k-ary tree, i want to convert it into a min-heap with minimum number of changes. Change is defined as relabelling a node.

one solution i have found is that, i can try a dp solution of changing a nodes value or not changing. But its going to be exponential in time complexity ? Any ideas, (preferable with optimality proofs).

Example : Say the tree is, 1-3, 3-2, 1-4, 4-5. where 1 is root. Then i can relabel node 3 to 1 or 2, that is in 1 change it becomes a min-heap.

  • You want to change it into a min d-ary heap? Or a min *binary* heap? And is the k-ary tree ordered in any way? In your example, are you saying that 3 and 4 are children of 1, that 2 is a child of 3, and 5 is a child of 4? A picture would be more clear. – Jim Mischel Dec 16 '17 at 23:18
  • I want to maintain the structure of tree, but the value of node can be changed to whatever. By heap i just meant that, If i take a node u, and v belongs to its subtree, then u >= v. – Ensei Tankado Dec 17 '17 at 11:40
  • So you want the resulting tree to satisfy the *heap* property, but not necessarily the *shape* property (see https://en.wikipedia.org/wiki/Binary_heap and https://en.wikipedia.org/wiki/D-ary_heap)? Interesting. – Jim Mischel Dec 17 '17 at 14:45

1 Answers1

0

If all you want to do is make sure that the tree satisfies the heap property (the key stored in each node is less than or equal to the keys stored in the node's children), then you should be able to use something like the build-heap algorithm, which operates in O(n).

Consider this tree:

                    8
              -------------
             |      |      |
            15      6      19
           /  \     |    / |  \
          7    3    5   12 9  22

Now, working from the bottom up, you push each node down the tree as far as it can go. That is, if the node is larger than any of its children, you replace it with the smallest of its children, and you do so until you reach the leaf level, if necessary.

For example, you look at the node valued 15. It's larger than its smallest child, so you swap it, making the subtree:

           3
         /   \
        7     15

Also, 6 swaps places with 5, and 19 swaps places with 9, giving you this tree:

                    8
              -------------
             |      |      |
             3      5      9 
            / \     |    / |  \
           7  15    6   12 19  22

Note that at the next to leaf level, each node is smaller than its smallest child.

Now, the root. Since the rule is to swap the node with its smallest child, you swap 8 with 3, giving:

                    3
              -------------
             |      |      |
             8      5      9 
            / \     |    / |  \
           7  15    6   12 19  22

But you're not done because 8 is greater than 7. You swap 8 with 7, and you get this tree, which meets your conditions:

                    3
              -------------
             |      |      |
             7      5      9 
            / \     |    / |  \
           8  15    6   12 19  22

If the tree is balanced, the entire procedure has complexity O(n). If the tree is severely unbalanced, the complexity is O(n^2). There is a way to guarantee O(n), regardless of the tree's initial order, but it requires changing the shape of the tree.

I won't claim that the algorithm guarantees the "minimal number of changes" for any given tree. I can prove, however, that with a balanced tree the algorithm is O(n). See https://stackoverflow.com/a/9755805/56778, which explains it for binary heap. The explanation also applies to d-ary heap.

Jim Mischel
  • 122,159
  • 16
  • 161
  • 305
  • The algorithm described is simply a greedy approach, of finding one possible solution. What I wanted was that the number of changes be minimal. Minimality is an important criteria. Thanks. – Ensei Tankado Dec 19 '17 at 16:04
  • @EnseiTankado: Minimizing the number of changes will require a lot more processing time to figure out what changes need to be made. I think there is a guaranteed O(n^2) solution, but will have to play with it a bit. – Jim Mischel Dec 19 '17 at 17:55