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

Spread the post

Binary search tree print level order data

Suppose Binary tree contain following nodes. Our goal is to print BST data in level order.


Try it Yourself

Example in Level Order Data of this BST.

Time complexity of this program O(n)

Accepted Output

Given BST tree node is:

140, 90, 220, 30, 104, 240, 40, 115, 104

Result :

Level order data is:- 140 90 220 30 104 240 40 115 104

C program to print data in level order. Iterative approach.

  BST Data is :  30  40  90  104  104  115  140  220  240
 Level order data is:-  140  90  220  30  104  240  40  115  104
 Free Memory of Queue

 Free BST node

Stack Areamainqueue (pointer) ? root(pointer)inorder_showtemp(pointer)inorder_showtemp(pointer)inorder_showtemp (pointer) ? Heap Areastruct Treedata (int)= 140left_child (pointer) right_child (pointer) struct Treedata (int)= 90left_child (pointer) right_child (pointer) struct Treedata (int)= 30left_child (pointer)= NULLright_child (pointer) struct Treedata (int)= 220left_child (pointer)= NULLright_child (pointer) struct Treedata (int)= 40left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 104left_child (pointer)= NULLright_child (pointer) struct Treedata (int)= 115left_child (pointer) right_child (pointer)= NULLstruct Treedata (int)= 104left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 240left_child (pointer)= NULLright_child (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: