Home » Other » General » algorithm to find out height and no of nodes in a binary tree
algorithm to find out height and no of nodes in a binary tree [message #103915] Mon, 17 May 2004 02:56 Go to next message
dipankar saha
Messages: 36
Registered: March 2004
Member
hi to all.

can anyone plz me help me out and provide me the algorithm to find out height and no of nodes in a binary tree.

thanks in advance.

dipankar saha
Re: algorithm to find out height and no of nodes in a binary tree [message #104023 is a reply to message #103915] Wed, 30 June 2004 18:05 Go to previous messageGo to next message
Jai Vrat Singh
Messages: 205
Registered: September 2002
Location: Singapore
Senior Member
Hi -- it is somwwhat like this ( I HAVE NOT TESTED THE CODE):

int getNodes( struct node* n){

if(n->left == NULL && n->right == NULL)
return(0);
else
return(1+ getNodes(n->left) + getNodes(n->right));
}

int getHeight( struct node* n){
int l_height=0;
int r_height=0;

if(n->left == NULL && n->right == NULL)
return(0);
else{
l_height = getHeight( n->left);
r_heigth = getHeight( n->right);
return(1+ (l_height> r_height)?l_height:r_height);
}
}
Re: algorithm to find out height and no of nodes in a binary tree [message #104024 is a reply to message #103915] Wed, 30 June 2004 22:26 Go to previous message
Ritesh
Messages: 38
Registered: November 2001
Member
You can use the DEPTH FIRST SEARCH algorithm.
Explanations & Implementations in C can be found in many books(Sartaj Sahni, Data Structures & Algorithms)
Previous Topic: Oracle Job Networking
Next Topic: toad
Goto Forum:
  


Current Time: Fri Apr 19 19:39:50 CDT 2024