Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C++ Help (http://www.go4expert.com/forums/cpp-help-t4549/)

Castegna 5Jun2007 04:52

C++ Help
 
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

karri 6Jun2007 01:15

Re: C++ Help
 
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

DaWei 6Jun2007 05:15

Re: C++ Help
 
Karri, please read the "Before you post a query" thread; specifically, learn to use code tags.

shabbir 6Jun2007 08:38

Re: C++ Help
 
Quote:

Originally Posted by DaWei
Karri, please read the "Before you post a query" thread; specifically, learn to use code tags.

Code formatting


All times are GMT +5.5. The time now is 11:19.