Algorithm for convert binary search tree to max heap

Oct 2019
9
0
UAE
Hello,

I have BST in a custom size and I want to convert it to max-heap.
What algorithm do we have for this?


Thanks
 

Attachments

Oct 2019
9
0
UAE
I found a solution.
I can traversal inorder and postorder(for max-heap and preorder for min-heap) but I can't understand the red line.

Could you please give me an example of red line?
 

Attachments

May 2013
100
10
It's been a while since I've had to code a heap or BST, but here's my crack at it.
First let's take the structure, and put it into an array.
[27, 14, 35, 10, 19, 31, 42]
Now we want to make it a max heap.
dequeue 42, swap it for 27
[42,14, 35, 10, 19, 31, 27]
dequeue 31, examine it with 14 and 35, and swap with 14.
[42, 31, 35, 10, 19, 14 , 27]
and here we have a max heap.