Pergunta

I came across this picture, and someone had commented that there's a problem with the diagram, but I am not sure what it is.

Here's the picture: (original link)

Now the tree looks alright to me but the heap creates some doubt.

I know in binary heap, if the root has two children, then the left child must have it's two children before we can proceed on to the right child. Is it the case with n-ary heap also. That is, since the root has four children, then the first child should have had it's four children, before we move on to the next child.

Foi útil?

Solução

In general, a structure is a heap if it satisfies heap condition - therefore this heap is ok, because it does satisfy it.

If we're looking for some concrete heap, I guess that pairing heap would be ok.

Outras dicas

The problem is that there is a second condition that is generally required. That condition is that every row of the tree must be full except possibly the last, but the last row must be left-filled. In other words, if there are any nodes missing on the last row, they must be all towards the right. In the diagram the second node in the fourth row has no children, and the forth and fifth each have just a right child. Even worse, the first node in the second row doesn't have a right child. There is one more problem, but I'll leave it to you to find it.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top