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

Spread the post

Count BST node using recursion

Count node of given binary search tree. suppose BST contain following nodes [ 542, 469, 577, 461, 528, 572, 584, 454, 461, 597].

542469577461528572584454461597

Number of BST is [10].

Try it Yourself

Function to Count BST nodes.

Example :

507030902010156080

Number of BST node is [9].

Try it Yourself

C program to count BST nodes. Recursive approach.

Output
 BST Data is :  10  15  20  30  50  60  70  80  90
 Number of node is [9] 

Visualize BST

Stack Areamainresult (int) =0root(pointer) Heap Areastruct Treedata (int)= 50left_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)= NULLstruct 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)= 60left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 80left_child (pointer)= NULLright_child (pointer)= NULL

Process to count BST nodes.

Stack Areamainresult (int) =2root(pointer)count_nodetemp(pointer)value(pointer)count_nodetemp(pointer)value(pointer)count_nodetemp(pointer)value(pointer)count_nodetemp(pointer)value(pointer)count_nodetemp(pointer)value(pointer)count_nodetemp(pointer) NULLvalue(pointer) Heap Areastruct Treedata (int)= 50left_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)= NULLstruct 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)= 60left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 80left_child (pointer)= NULLright_child (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: