Tree traversal algorithm

Source: Internet
Author: User
Public Function upload tree (tree: Tree, item: object, startatparent: Boolean = false): void
{
// Get the tree's data Descriptor
VaR descriptor: itreedatadescriptor = tree. datadescriptor;
VaR cursor: iviewcursor;

VaR parentitem: object;
VaR childitem: object;
VaR childitems: object;

// If the item is null, stop
If (item = NULL)
Return;

// Do we back up one level to the item's parent
If (startatparent)
{
// Get the parent
Parentitem = tree. getparentitem (item );
// Is the parent real
If (parentitem)
{
Trace ("| -- parent node", parentitem [tree. labelfield]);
// If the parent is a branch
If (descriptor. isbranch (parentitem ))
{
// If the branch has children to run through
If (descriptor. haschildren (parentitem ))
{
// Get the children of the Branch
// This Part of the algorithm contains the item
// Passed
Childitems = descriptor. getchildren (parentitem );
}
}
// If the branch has valid child items
If (childitems)
{
// Create our back step cursor
Cursor = childitems. createcursor ();
// Loop through the items parent's children (item)
While (! Cursor. afterlast)
{
// Get the current Child item
Childitem = cursor. Current;
VaR label: String = childitem [tree. labelfield];
VaR branch: Boolean = descriptor. isbranch (childitem );

// Good place for a custom method ()
Trace ("sibling nodes:", label, "Is Branch:", Branch );

// If the child item is a branch
If (descriptor. isbranch (childitem ))
// Traverse the childs branch all the way down
// Before returning
Tree (tree, childitem );
// Do it again!
Cursor. movenext ();
}
}
}
}
Else // we don't want the parent or this is the second iteration
{
// If we are a branch
If (descriptor. isbranch (item ))
{
// If the branch has children to run through
If (descriptor. haschildren (item ))
{
// Get the children of the Branch
Childitems = descriptor. getchildren (item );
}

// If the child items exist
If (childitems)
{
// Create our cursor pointer
Cursor = childitems. createcursor ();
// Loop through all of the children
// If one of these children are a branch we will recurse
While (! Cursor. afterlast)
{
// Get the current Child item
Childitem = cursor. Current;

VaR label: String = childitem [tree. labelfield];
VaR branch: Boolean = descriptor. isbranch (childitem );

// Good place for a custom method ()
Trace ("-- sub node:", label, "Is Branch:", Branch );

// If the child item is a branch
If (descriptor. isbranch (childitem ))
// Traverse the childs branch all the way down
// Before returning
Tree (tree, childitem );
// Check the next child
Cursor. movenext ();
}
}
}
}
}

////////////////
DFS. You can use it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.