Given a binary min heap and a value x, print all the binary heap nodes having value less than the given value x.
Examples : Consider the below min heap as common input two both below examples. 2 / \ 3 15 / \ / \ 5 4 45 80 / \ / \ 6 150 77 120 Input : x = 15 Output : 2 3 5 6 4 Input : x = 80 Output : 2 3 5 6 4 77 15 45
The idea is to do a preorder traversal of the give Binary heap. While doing preorder traversal, if the value of a node is greater than the given value x, we return to the previous recursive call. Because all children nodes in a min heap are greater than the parent node. Otherwise we print current node and recur for its children.
2 3 5 6 4 77 15 45 80
This article is contributed by Raghav Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- k largest(or smallest) elements in an array | added Min Heap method
- Applications of Heap Data Structure
- Tournament Tree (Winner Tree) and Binary Heap
- Time Complexity of building a heap
- Print all elements in sorted order from row and column wise sorted matrix
- Binomial Heap
- Why is Binary Heap Preferred over BST for Priority Queue?
- Fibonacci Heap | Set 1 (Introduction)
- How to check if a given array represents a Binary Heap?
- Check if a given Binary Tree is Heap
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
- K-ary Heap
- Convert min Heap to max Heap
- Heap in C++ STL | make_heap(), push_heap(), pop_heap(), sort_heap(), is_heap, is_heap_until()
- Implementation of Binomial Heap
Improved By : shubham96301