Tree遍历的算法 [转]

None.gif public  function walkTree(tree:Tree, item:Object, startAtParent:Boolean  =   false ): void
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// get the Tree's data descriptor
InBlock.gif
    var descriptor:ITreeDataDescriptor = tree.dataDescriptor;
InBlock.gif    var cursor:IViewCursor;
InBlock.gif    
InBlock.gif    var parentItem:Object;
InBlock.gif    var childItem:Object;
InBlock.gif    var childItems:Object;
InBlock.gif    
InBlock.gif    
// if the item is null, stop
InBlock.gif
    if(item == null)
InBlock.gif        
return;
InBlock.gif        
InBlock.gif    
// do we back up one level to the item's parent
InBlock.gif
    if(startAtParent)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// get the parent
InBlock.gif
        parentItem = tree.getParentItem(item);
InBlock.gif        
// is the parent real
InBlock.gif
        if(parentItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            trace(
"|-- Parent Node ", parentItem[tree.labelField]);
InBlock.gif            
// if the parent is a branch
InBlock.gif
            if(descriptor.isBranch(parentItem))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// if the branch has children to run through
InBlock.gif
                if(descriptor.hasChildren(parentItem))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// get the children of the branch
InBlock.gif                    
// this part of the algorithm contains the item
InBlock.gif                    
// passed
InBlock.gif
                    childItems = descriptor.getChildren(parentItem);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
// if the branch has valid child items
InBlock.gif
            if(childItems)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// create our back step cursor
InBlock.gif
                cursor = childItems.createCursor();
InBlock.gif                
// loop through the items parent's children (item)
InBlock.gif
                while(!cursor.afterLast)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// get the current child item
InBlock.gif
                    childItem = cursor.current; 
InBlock.gif                    var label:String 
= childItem[tree.labelField];
InBlock.gif                    var branch:Boolean 
= descriptor.isBranch(childItem);
InBlock.gif                    
InBlock.gif                    
// good place for a custom method()
InBlock.gif
                    trace("Sibling Nodes :: ", label, "Is Branch :: ", branch); 
InBlock.gif                    
InBlock.gif                    
// if the child item is a branch
InBlock.gif
                    if(descriptor.isBranch(childItem))
InBlock.gif                        
// traverse the childs branch all the way down 
InBlock.gif                        
// before returning
InBlock.gif
                        walkTree(tree, childItem);
InBlock.gif                    
// do it again!
InBlock.gif
                    cursor.moveNext();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
else// we don't want the parent OR this is the second iteration
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
// if we are a branch
InBlock.gif
        if(descriptor.isBranch(item))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// if the branch has children to run through
InBlock.gif
            if(descriptor.hasChildren(item))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// get the children of the branch
InBlock.gif
                childItems = descriptor.getChildren(item);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
// if the child items exist
InBlock.gif
            if(childItems)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// create our cursor pointer
InBlock.gif
                cursor = childItems.createCursor();
InBlock.gif                
// loop through all of the children
InBlock.gif                
// if one of these children are a branch we will recurse
InBlock.gif
                while(!cursor.afterLast)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// get the current child item
InBlock.gif
                    childItem = cursor.current;
InBlock.gif
InBlock.gif                    var label:String 
=  childItem[tree.labelField];
InBlock.gif                    var branch:Boolean 
= descriptor.isBranch(childItem);
InBlock.gif                    
InBlock.gif                    
// good place for a custom method()
InBlock.gif
                    trace("-- Sub Node :: ", label, "Is Branch :: ", branch); 
InBlock.gif
InBlock.gif                    
// if the child item is a branch
InBlock.gif
                    if(descriptor.isBranch(childItem))
InBlock.gif                        
// traverse the childs branch all the way down 
InBlock.gif                        
// before returning
InBlock.gif
                        walkTree(tree, childItem);
InBlock.gif                    
// check the next child
InBlock.gif
                    cursor.moveNext();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


DFS看看懂,拿来用就行了

转载于:https://www.cnblogs.com/fishert/archive/2007/03/02/661182.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值