Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Print BST keys given Range

How to print binary search tree in given range? here include few example to clear. suppose following data are inserted on binary search tree [150, 70, 30, 90, 240, 110, 153, 650, 20, 10, 15, 660, 80].

15070240309015365066011080201015

Try it Yourself

Exmaple 1: BST Data Between [70 - 100].

15070240309015365066011080201015

BST Data Between [ 10 - 100 ].

15070240309015365066011080201015

Example 3: BST Data Between [ 105 - 300 ] is : [ 110 150 153 240 ].

15070240309015365066011080201015

Example 4:BST Data Between [ 310 - 700 ] is : 650 660

15070240309015365066011080201015

Accepted Output

Given BST tree node is:

50, 30, 70, 20, 35, 60, 90, 10, 85, 100, 15, 80

Result :

BST Data Between [70 - 100] is : 70 90 85 100 80

BST Data Between [0 - 70] is : 50 30 70 20 35 60 10 15

Given BST tree node is:

150, 70, 30, 90, 240, 110, 153, 650, 20, 10, 15, 660, 80

Result :

BST Data Between [ 10 - 100 ] is : 10 15 20 30 70 80 90

BST Data Between [ 105 - 300 ] is : 110 150 153 240

BST Data Between [ 310 - 700 ] is : 650 660

Algorithm and code ececution

Given algorithm and process to print all BST keys in given range.

Execution process

Execution process to print BST keys Help of this execution process to print all bst element in given range Stack Areamainroot(pointer)print_keysend (int) =300start (int) =105temp(pointer)print_keysend (int) =300start (int) =105temp(pointer)print_keysend (int) =300start (int) =105temp(pointer)print_keysend (int) =300start (int) =105temp(pointer) Heap Areastruct Treedata (int)= 150left_child (pointer) right_child (pointer) struct Treedata (int)= 70left_child (pointer) right_child (pointer) struct Treedata (int)= 30left_child (pointer) right_child (pointer)= NULLstruct Treedata (int)= 90left_child (pointer) right_child (pointer) struct Treedata (int)= 240left_child (pointer) right_child (pointer) struct Treedata (int)= 110left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 153left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 650left_child (pointer)= NULLright_child (pointer) struct Treedata (int)= 20left_child (pointer) right_child (pointer)= NULLstruct Treedata (int)= 10left_child (pointer)= NULLright_child (pointer) struct Treedata (int)= 15left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 660left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 80left_child (pointer)= NULLright_child (pointer)= NULL

Time complexity of this program O(n)

C program to print BST keys in given range.

Output
  BST Inorder Data Is :  10  15  20  30  70  80  90  110  150  153  240  650  660
 BST Data Between [ 10 - 100 ] is : 10 15 20 30 70 80 90
 BST Data Between [ 105 - 300 ] is : 110 150 153 240
 BST Data Between [ 310 - 700 ] is : 650 660 

Try it Yourself

Spread the post

Recommended Posts: