C++ Help

Castegna's Avatar, Join Date: Jun 2007
Newbie Member
Hi my name is Javier, I am Computer science major. I need help with Binary tree operations. I need some help writing a non-recursive c++ function that counts the number of leafs in a tree?? Thanks you
0
karri's Avatar, Join Date: Jun 2007
Light Poster
i think this code wil help

Code:
nodeptr stack[MAX_SIZE];

inorder(nodeptr node)
{
  int i = 0;
  while(1)
  {
    for(;node;node = node -> left)
      add(node);                           // adding node to stack
   node = pop();                         // pop from stack
   if(!node->rchild && !node->lchild)
    i++;
  node = node->rchild;
 }
 printf("no of leaf nodes = %d",i);
}
Basically it is the same as the iterative version of the inorder traversal

Last edited by shabbir; 6Jun2007 at 08:37.. Reason: Code block - http://www.go4expert.com/forums/misc.php?do=bbcode#code
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Karri, please read the "Before you post a query" thread; specifically, learn to use code tags.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by DaWei
Karri, please read the "Before you post a query" thread; specifically, learn to use code tags.
Code formatting