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

Spread the post

Count height of binary search tree ( Recursive approach )

Time complexity of this program O(n)

Accepted Output

Given BST tree node is:

463, 422, 466, 425, 551, 483, 582, 526, 507, 522

View Tree Click Here

463422466551582483526507522425

Height of BST is [7] By node distance

Height of BST is [6] By node edge

Given BST tree node is:

528, 447, 538, 423, 519, 532, 538, 426, 503, 436

View Tree Click Here

528538447423538426532519503436

Height of BST is [5] By node distance.

Height of BST is [4] By node edge.

Code execution

See code execution process.

Code execution to count BST height Code execution process to count BST height Stack Areamainroot(pointer)count_heighta (int) =5b (int) ? temp(pointer)count_heighta (int) =1b (int) ? temp(pointer)count_heighta (int) ? b (int) ? temp(pointer)count_heighttemp (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) struct Treedata (int)= 940left_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) struct Treedata (int)= 60left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 80left_child (pointer)= NULLright_child (pointer) struct Treedata (int)= 15left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 84left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 47left_child (pointer)= NULLright_child (pointer)= NULL

In this execution process not imagination all steps. below link click and view how to insert binary search tree node,how to count binary search tree height, how to show all BST node and free all binary search tree element.

C program to count height of binary tree or binary search tree. Recursive approach.

Output
 BST Data is :  10  15  15  20  30  47  50  60  70  80  84  940 
 BST Heigh is : [6]
 

Recommended Posts:

Spread the post