当前位置: 首页>>代码示例>>Python>>正文


Python Tree.traverse方法代码示例

本文整理汇总了Python中tree.Tree.traverse方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.traverse方法的具体用法?Python Tree.traverse怎么用?Python Tree.traverse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tree.Tree的用法示例。


在下文中一共展示了Tree.traverse方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_traverse_returns_none_when_operation_always_returns_false

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import traverse [as 别名]
def test_traverse_returns_none_when_operation_always_returns_false():
    t = Tree("a")
    t.add_child("b")

    node = t.traverse(lambda n: False)

    assert_that(node, is_(None))
开发者ID:povilasb,项目名称:pytree,代码行数:9,代码来源:test_tree.py

示例2: test_traverse_uses_depth_first_strategy

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import traverse [as 别名]
def test_traverse_uses_depth_first_strategy():
    t = Tree("a")
    child = t.add_child("b")
    child.add_child("d")
    child.add_child("e")
    t.add_child("c")

    visited = []

    def visit_all(node):
        visited.append(node.value)
        return False

    t.traverse(visit_all)

    assert_that(visited, is_(["a", "b", "d", "e", "c"]))
开发者ID:povilasb,项目名称:pytree,代码行数:18,代码来源:test_tree.py

示例3: test_traverse_returns_self_when_operation_returns_true_on_root_node

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import traverse [as 别名]
def test_traverse_returns_self_when_operation_returns_true_on_root_node():
    t = Tree("a")

    node = t.traverse(lambda n: True)

    assert_that(node, is_(t))
开发者ID:povilasb,项目名称:pytree,代码行数:8,代码来源:test_tree.py

示例4: dict

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import traverse [as 别名]
        tree[parent]
    except KeyError:
        tree.add_node(parent)
        
    try:
        tree[child]
    except KeyError:
        tree.add_node(child)
        
    tree[parent].add_child(child)
    tree[child].add_parent(parent)


query = ("SELECT id, name FROM imm_web")
cursor.execute(query)
id_name_dict = {}
for (id, name) in cursor:
    id_name_dict[id] = name

tree.display(1)
return_info = dict()
print("***** DEPTH-FIRST ITERATION *****")
for node in tree.traverse(1):
    print(str(node) + ", " + id_name_dict[node] + ", " + str(tree[node].parent))
    return_info[node] = [id_name_dict[node], tree[node].parent]
print("***** BREADTH-FIRST ITERATION *****")
for node in tree.traverse(1, mode=_BREADTH):
    print(str(node) + ", " + id_name_dict[node])

cursor.close()
cnx.close()
开发者ID:earage,项目名称:nodePresent,代码行数:33,代码来源:testItem.py

示例5: Copyright

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import traverse [as 别名]
# Copyright (C) by Brett Kromkamp 2011-2014 ([email protected])
# You Programming (http://www.youprogramming.com)
# May 03, 2014

from tree import Tree

(_ROOT, _DEPTH, _BREADTH) = range(3)

tree = Tree()

tree.add_node("Harry")  # root node
tree.add_node("Jane", "Harry")
tree.add_node("Bill", "Harry")
tree.add_node("Joe", "Jane")
tree.add_node("Diane", "Jane")
tree.add_node("George", "Diane")
tree.add_node("Mary", "Diane")
tree.add_node("Jill", "George")
tree.add_node("Carol", "Jill")
tree.add_node("Grace", "Bill")
tree.add_node("Mark", "Jane")

tree.display("Harry")
print("***** DEPTH-FIRST ITERATION *****")
for node in tree.traverse("Harry"):
    print(node)
print("***** BREADTH-FIRST ITERATION *****")
for node in tree.traverse("Harry", mode=_BREADTH):
    print(node)
开发者ID:robertowtr,项目名称:SegurancaAuditoria,代码行数:31,代码来源:app.py

示例6: importData

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import traverse [as 别名]

#.........这里部分代码省略.........
    node_ID_number,node_parent_ID_number,data_item1,data_item2,...,data_itemN\n

    The separator can, optionally, be a character other than ","

    The root node must have a parent id of 0 and normally should also have an index of 1

    From MATLAB one can produce tree structures and dump data in the correct format
    using https://github.com/raacampbell13/matlab-tree and the tree.dumptree method

    Inputs:
    fname - if a string, importData assumes it is a file name and tries to load the tree from file.
            if it is a list, importData assumes that each line is a CSV data line and tries to 
            convert to a tree.        
    displayTree - if True the tree is printed to standard output after creation
    colSep - the data separator, a comma by default.
    headerLine - if True, the first line is stripped off and considered to be the column headings.
                headerLine can also be a CSV string or a list that defines the column headings. Must have the
                same number of columns as the rest of the file.
    verbose - prints diagnositic info to screen if true
    """



    if verbose:
        print "tree.importData importing file %s" % fname

    #Error check
    if isinstance(fname,str):
        if os.path.exists(fname)==False:
            print "Can not find file " + fname
            return

        #Read in data
        fid = open(fname,'r')
        contents = fid.read().split('\n')
        fid.close()

    elif isinstance(fname,list):
        contents=fname #assume that fname is data rather than a file name


    #Get header data if present
    if headerLine==True:
        header = contents.pop(0)
        header = header.rstrip('\n').split(colSep)
    elif isinstance(headerLine,str):
        header = headerLine.rstrip('\n').split(colSep)
    elif isinstance(headerLine,list):
        header = headerLine
    else:
        header = False


    data = []
    for line in contents:
        if len(line)==0:
            continue

        dataLine = line.split(colSep)
        if len(header) !=len(dataLine):
            print "\nTree file appears corrupt! header length is %d but data line length is %d.\ntree.importData is aborting.\n" % (len(header),len(dataLine))
            return False

        theseData = map(int,dataLine[0:2]) #add index and parent to the first two columns

        #Add data to the third column. Either as a list or as a dictionary (if header names were provided)
        if header != False: #add as dictionary
            dataCol = dict()

            for ii in range(len(header)-2):
                ii+=2
                dataCol[header[ii]]=dataTypeFromString.convertString(dataLine[ii])

        else:
            dataCol = dataLine[2:] #add as list of strings


        theseData.append(dataCol) 
        data.append(theseData)

    if verbose:
        print "tree.importData read %d rows of data from %s" % (len(data),fname)


    #Build tree
    tree = Tree()
    tree.add_node(0)
    for thisNode in data:
        tree.add_node(thisNode[0],thisNode[1])
        tree[thisNode[0]].data = thisNode[2]


    #Optionally dump the tree to screen (unlikely to be useful for large trees)
    if displayTree:
        tree.display(0)

        for nodeID in tree.traverse(0):
            print "%s - %s" % (nodeID, tree[nodeID].data)

    return tree
开发者ID:raacampbell,项目名称:lasagna,代码行数:104,代码来源:tree.py

示例7: print

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import traverse [as 别名]

    print "List of nodes:"
    print treeOfLife.nodes.keys()
    print ""

    print "Children of node 'Vertebrates'"
    print treeOfLife.nodes['Vertebrates'].children
    print ""


    print treeOfLife.display('Life')


    print("\n***** Depth-first *****")
    for nodeID in treeOfLife.traverse("Life"):
        print(nodeID)

    print("\n***** Width-first *****")
    for nodeID in treeOfLife.traverse("Life", mode=_WIDTH):
        print(nodeID)
    
    print("\n***** Width-first of all data in vertebrates *****")
    for nodeID in treeOfLife.traverse("Vertebrates", mode=_WIDTH):
        print "%s - %s" % (nodeID, treeOfLife[nodeID].data)

    print "\nLeaves:"
    print treeOfLife.findLeaves('Life')

    print "\nBranches:"
    print treeOfLife.findBranches('Life')
开发者ID:raacampbell,项目名称:lasagna,代码行数:32,代码来源:tree.py


注:本文中的tree.Tree.traverse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。