C++ Help

Discussion in 'C++' started by Castegna, Jun 5, 2007.

  1. Castegna

    Castegna New Member

    Joined:
    Jun 5, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Seattle
    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
     
  2. karri

    karri New Member

    Joined:
    Jun 5, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Jun 6, 2007
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Karri, please read the "Before you post a query" thread; specifically, learn to use code tags.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Code formatting
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice