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

Spread the post

Binary search tree print Specific level (Given Level) node data

In this post we are learning about how to print specific level (given level) BST nodes.Assume that following data are inserted on tree [476, 467, 584, 421, 475, 501, 900, 413, 432, 530, 533].

BST Tree BST to print given level data. 476584501467421475530432413533900

Try it Yourself

Print level 2 data of this tree.

BST Level 2 Data BST Print level 2 data 476467584421475501900413432530533

Level and data of given binary search tree is.

Another example: print level 3

BST Level 3 Data BST Print level 3 data 476467584421475501900413432530533

Algorithm:

Time complexity of this program O(n)

Execution Process:

Print level execution Print specific level of bst Stack Areamainlevel (int) =2result (int) =2root(pointer)print_levellevel (int) =2result(pointer)show (int) =0temp(pointer)print_levellevel (int) =2result(pointer)show (int) =1temp(pointer)print_levellevel (int) =2result(pointer)show (int) =2temp(pointer)print_levellevel (int) ? result (pointer) ? show (int) ? temp (pointer) ? Heap Areastruct Treedata (int)= 476left_child (pointer) right_child (pointer) struct Treedata (int)= 467left_child (pointer) right_child (pointer) struct Treedata (int)= 584left_child (pointer) right_child (pointer) struct Treedata (int)= 421left_child (pointer) right_child (pointer) struct Treedata (int)= 475left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 501left_child (pointer)= NULLright_child (pointer) struct Treedata (int)= 900left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 413left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 432left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 530left_child (pointer)= NULLright_child (pointer) struct Treedata (int)= 533left_child (pointer)= NULLright_child (pointer)= NULL

In this execution process not imagination all steps. below link click and view how to insert bst node, how to show all bst node and how to print given level BST node .

C program to print given level. Recursive approach.

Output
 Inorder Data :  413  421  432  467  475  476  501  530  533  584  900
 Level [3] data: 413 432 530
 Level [2] data: 421 475 501 900
 Level [5] data:Given level[5] not exist
 Free BST nodes

Spread the post

Recommended Posts: