# C Program To Calculate the Maximum depth of a Binary Search Tree(BST)

```#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node{
int data;
struct node *left;
struct node *right;
}*root=NULL,*temp;
void insert();
int find_height(struct node *t);
void create();
void search(struct node *t);
int n,array[1000],j=0,height;
void main()
{

int i,result;
printf("Enter the total number of elements: ");
scanf("%d",&n);
printf("\nEnter the elements of the array:\n ");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
insert();
printf("\n");
result=find_height(root);
printf("The maximum height of binary search tree is %d",result);
getch();
}
void insert()
{
int i;
for(i=0;i<n;i++)
{
create();
if(root==NULL)
{
root=temp;
}
else
{
search(root);
}
}
}
void create()
{
temp=(struct node*)malloc(sizeof(struct node));
temp->data=array[j];
temp->left=temp->right=NULL;
j++;
}
void search(struct node *t)
{
if((temp->data<t->data)&&(t->left!=NULL))
{
search(t->left);
}
else if((temp->data<t->data)&&(t->left==NULL))
{
t->left=temp;
}
else if((temp->data>t->data)&&(t->right!=NULL))
{
search(t->right);
}
else if((temp->data>t->data)&&(t->right==NULL))
{
t->right=temp;
}
}
int find_height(struct node* t)
{
int lefth=0,righth=0,height=0;
if(root==NULL)
{
return -1;
}
if(t->left!=NULL)
{
lefth=find_height(t->left);
}
if(t->right!=NULL)
{
righth=find_height(t->right);
}
if(lefth>righth)
{
height=lefth+1;
}
else
{
height=righth+1;
}
return height;
}```

#### Zaid Khan

This Is The Oldest Page