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

  • IT decision support impacts all aspects of technology management, from governance and strategy to budgets and resource planning. IT decision support effectiveness often falls prey to data-driven challenges that make it difficult to understand the data in context. These challenges: overwhelming data volumes, heterogeneous data types, and growing data complexity. This Forrester Consulting Paper reports the three key findings from their study conducted, on behalf of BDNA, to test the hypothesis that data …

  • Live Event Date: August 19, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT You deployed your app with the Bluemix PaaS and it's gaining some serious traction, so it's time to make some tweaks. Did you design your application in a way that it can scale in the cloud? Were you even thinking about the cloud when you built the app? If not, chances are your app is going to break. Check out this upcoming webcast to learn various techniques for designing applications that will scale successfully in Bluemix, for the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds