Get last item in branch


In my programming, I have often had to derive the last item in a branch. In fact some of the other topics I cover, uses this function to get the last item in the branch.
 
// GetLastItem  - Gets last item in the branch
// Returns      - Last item
// hItem        - Node identifying the branch. NULL will 
//                return the last item in outine
HTREEITEM CTreeCtrlX::GetLastItem( HTREEITEM hItem )
{
        // Last child of the last child of the last child ...
        HTREEITEM htiNext;

        if( hItem == NULL ){
                // Get the last item at the top level
                htiNext = GetRootItem();
                while( htiNext ){
                        hItem = htiNext;
                        htiNext = GetNextSiblingItem( htiNext );
                }
        }

        while( ItemHasChildren( hItem ) ){
                htiNext = GetChildItem( hItem );
                while( htiNext ){
                        hItem = htiNext;
                        htiNext = GetNextSiblingItem( htiNext );
                }
        }

        return hItem;
}



Comments

  • Little problem ???

    Posted by Legacy on 11/05/1998 12:00am

    Originally posted by: Patrice Lacroix

    I believe I found a problem in this method.
    
    

    Please correct me if I'm wrong or if I am not using this method correctly.

    If I am searching for a string in the tree and this string is
    currently selected, the method will return NULL.

    Should I check before I call the method ?

    Here is what I did to correct it:

    // For the first pass, set the current item equal to the selection
    HTREEITEM htiSel = hItem ? hItem : GetSelectedItem();
    HTREEITEM htiCur = htiSel;

    ...
    ...
    // For the first pass only, we check to see if it
    // is the item we're looking for.
    BOOL bFirstPass = TRUE;

    while( htiCur && (htiCur != htiSel || bFirstPass) )
    {
    bFirstPass = FALSE;
    ...
    ...
    ...
    }

    ...
    ...
    ...

    Please, tell me if you have a better way to do it.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

  • The latest release of SugarCRM's flagship product gives users new tools to build extraordinary customer relationships. Read an in-depth analysis of SugarCRM's enhanced ability to help companies execute their customer-facing initiatives from Ovum, a leading technology research firm.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds