Given a binary search tree (BST), efficiently convert it into a min-heap. In order words, convert a binary search tree into a complete binary tree where each node has a higher value than its parent’s value.

For example, the solution should convert the BST on the left into the binary tree on the right, or any other binary tree with the same set of keys that satisfies the structural and heap-ordering property of min-heap data structure.

BST to Min Heap

Practice this problem

CASE 1: BST is a Complete Binary Tree

If the given BST is already a complete binary tree, the min-heap’s structural property is already satisfied, and we need to take care of the only heap-ordering property of the min-heap. Basically, we need to ensure that each node’s value is greater than its parent’s value, with the minimum element present at the root.

The idea is to traverse the binary search tree in an inorder fashion and enqueue all encountered keys. Then traverse the tree in a preorder fashion and for each encountered node, dequeue a key and assign it to the node.

 
Following is the implementation of the above algorithm in C++, Java, and Python. The logic works since the nodes get visited in the increasing order of their keys inorder traversal. The preorder traversal ensures that each node in the binary tree has a value greater than its parent’s.

C++


Download  Run Code

Output:

2
3 6
4 5 8 10

Java


Download  Run Code

Output:

2
3 6
4 5 8 10

Python


Download  Run Code

Output:

2
3 6
4 5 8 10

The time complexity of the above solution is O(n), where n is the size of the BST. The program also requires O(n) extra space for the queue.

CASE 2: BST is not a Complete Binary Tree

For normal BST, we need to take care of both the structural and heap-ordering property of min-heap. We need to ensure that the final tree is a complete binary tree and the value of each node is greater than the value of its parent.

The idea is to traverse the BST in an inorder fashion and store all encountered keys in a queue. Then construct a complete binary tree from sorted keys in the queue. If the binary tree is built level-by-level with nodes having keys in increasing order, the resultant tree will be a min-heap.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

2
3 4
5 8 10

Java


Download  Run Code

Output:

2
3 4
5 8 10

Python


Download  Run Code

Output:

2
3 4
5 8 10

The time complexity of the above solution is O(n), where n is the size of the BST. However, the program requires O(n) extra space for the queue, which can be avoided by doing the conversion in-place. The idea is to convert the binary search tree into a sorted linked list and then transform it into a min-heap.

To convert a BST into a sorted linked list, perform reverse inorder traversal on the BST and push the encountered nodes at the front of the linked list. In the reverse inorder traversal, the right subtree is visited first; then the node is processed, followed by the left subtree.

To convert the sorted list into a min-heap, construct the complete binary tree level-wise from left-to-right. The idea is to traverse the linked list and consider two unprocessed nodes at a time from the front. Those two nodes form children of the last leaf node of the partially constructed complete binary tree. Since the linked list is sorted, the min-heap property is preserved by the algorithm. Note that the root node is separately handled as it is the same as the front node in the sorted list.

C++


Download  Run Code

Output:

2
3 4
5 8 10

Java


Download  Run Code

Output:

2
3 4
5 8 10

Python


Download  Run Code

Output:

2
3 4
5 8 10