* find height of BINARY tree ITERATIVELY??*
//**************************************************************************************************************************
* Algorithm:-*
First create a STACK of Apropriate Size of Bnode pointer type
and perform a PUSH operation (Starting from Root of the Tree) ,and
then inceament one in the Level of the
Tree and as root=root->left until we get NULL.
if we get NULL ,check that TOP <0
if TOP< 0
then break ,Because Stack is Empty,Otherwise
if encreamented Level more than currently Height then
Height=Level.
perform POP Operation from the Stack and store in Root.
if Root->right does note exit then Decreament one from the level
and finaly Root=Root->right.
*Just a C -code *:-
int height(struct bnode* root)
{
int top=heigh=lev= -1
bnode *stack[MAX]
while(1)
{
while(root)
{
if(top>MAX-1)
{
print->"Stack is full"
break
}
stack[++top]=root // PUSH in Stack
lev ++
root=root->left
}
if(top<0)
break;
if(heigh>lev)
heigh=lev
root=stack[top--] //Stack is POP
if(root->right==NULL)
lev --
root=root->right
}
return height;
}
Umesh kumar
MCA from
University of Delhi.
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.