Question

I am learning about heaping and I am having trouble understand how you are supposed to move each node.I will give you an example tree below:

          1
      /      \
     2        3
   /  \      /  \
  4    5    6    7
 / \   /
8   9 10  

So that is my tree. I am trying to get 10 to the node but do not understand the steps that I take. Would I first look at the bottom of the tree? Heres my attempts:

          1
      /      \
     2        3
   /  \      /  \
  4    5    6    7
 / \   /
8   9 10 


 -> Move ten up and the two down. 

          1      
      /      \
    10        3
   /  \      /  \
  4    5    6    7
 / \   /
8   9 2 

-> Move the 9 up 

         1      
      /      \
    10        3
   /  \      /  \
  9    5    6    7
 / \   /
8   4 2 

-> move the 7 up

          1      
      /      \
    10        7
   /  \      /  \
  9    5    6    3
 / \   /
8   4 2

-> Move the whole left side up and bring the 1 down.

         10      
      /      \
    9         7
   /  \      /  \
  8    5    6    3
 / \   /
1   4 2

This is what I end up with but I have a feeling this is not right because it is not an ordered tree. Can someone help me understand where I went wrong?

Was it helpful?

Solution

Heap is not an ordered binary tree. The only ordering that heap preserves is that any child node is less (or equal) than it's parent node. Child nodes at the same level of the tree can be in any order relatively to each other.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top