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


Python tree.Node类代码示例

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


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

示例1: main

def main():
    root = Node(0)
    root.set_right(10)
    root.right.set_right(20)
    root.right.set_left(5)
    root.set_left(-10)
    print get_common_ancestor(root.right.right, root.right.left)
开发者ID:SageBerg,项目名称:CrackingTheCodingInterviewProblems,代码行数:7,代码来源:4.7.py

示例2: make_tree

	def make_tree(self):
		"""
		Constructs the tree by iterating nodesList.
		"""
		while self.nodesList:
			row = self.nodesList.pop(0)
			nodeId = self.idFn(row)
			#print 'make_tree,row is %s, node id is %s'%(row,nodeId)
			
			try:
				node = self.rootNode.get_child_by_id(nodeId)
			except:
				node = None
			
			if not node:
				node = Node(nodeId)
				node.data = row
				#print 'make_tree,created a new node, id is ',node.id
				parentId = self.pidFn(row)	
					
				# creates it's parent node and recursivelly creates the ancestor nodes of this node.
				self.parent(parentId, node)
				
				#print '****make_tree is end, maked branch is ******\n',self.rootNode
				#print 70*'-'
		
		return
开发者ID:,项目名称:,代码行数:27,代码来源:

示例3: p_formalDecl

def p_formalDecl(p):
    '''formalDecl : typeSpecifier IDENTIFIER
                  | typeSpecifier IDENTIFIER LBRACKET RBRACKET'''
    varName = p[2]
    varType = p[1].children[0].type
    for item in global_scope:
        if item[1] == varName:
            if item[0] == varType:
                print "Warning : Shadowing global variable %s near line %s.\n"  % (varName, p.lexer.lineno)
                
    match = False
    for item in local_scope:
        if item[1] == varName: #same identifier
            if item[0] == varType: #same type
                print "Syntax Error: Redefinition of local variable %s with type %s near line %s.\n" % \
                      (varName, varType, p.lexer.lineno)
            else:
                print "Syntax Error: Redefinition of local variable %s with type %s (originally type %s) near line %s.\n" % \
                      (varName, varType, item[0], p.lexer.lineno)
            global syntaxerrs
            syntaxerrs += 1
            match = True
                
    if not match:
        if DEBUG:
            print "New local variable %s %s declared near line %s.\n" % (varType, varName, p.lexer.lineno)
            
        if len(p) > 3:
            local_scope.append((varType, varName, "array"))
        else:
            local_scope.append((varType, varName))
            
    p[0] = Node("formalDecl", p[1:])
    if len(p) > 3:
        p[0].subtype = 'array'
开发者ID:MounikaArkala,项目名称:txstateprojects,代码行数:35,代码来源:mcc.py

示例4: testAll

 def testAll(self):
   if IGNORE_TEST:
     return
   node = Node(NAME)
   self.assertEqual(node.getName(), NAME)
   node.setName(NAME2)
   self.assertEqual(node.getName(), NAME2)
开发者ID:ScienceStacks,项目名称:SciSheets,代码行数:7,代码来源:test_tree.py

示例5: find_solution_rec_DFS

def find_solution_rec_DFS(node, solution, visited, limit):
    if limit > 0:
        visited.append(node)
        if node.get_data() == solution:
            return node
        else:
            # expand children nodes (cities with connection)
            node_data = node.get_data();
            children_list = []
            for a_child in connections[node_data]:
                child = Node(a_child)
                if not child.on_list(visited):
                    children_list.append(child)

            node.set_children(children_list)

            for child_node in node.get_children():
                if not child_node.get_data() in visited:
                    # recursive call
                    sol = find_solution_rec_DFS(child_node, solution, \
                                                visited, limit-1)
                    if sol != None:
                        return sol

        return None
开发者ID:segabalanv,项目名称:intelligent_systems,代码行数:25,代码来源:flights_idfs.py

示例6: find_solution_BFS

def find_solution_BFS(connections, initial_state, solution):
    solved = False
    visited_nodes = []
    frontier_nodes = []
    startNode = Node(initial_state)
    frontier_nodes.append(startNode)
    while(not solved) and len(frontier_nodes) != 0:
        node = frontier_nodes[0]
        # extract node and add it to visited_nodes
        visited_nodes.append(frontier_nodes.pop(0));
        if node.get_data() == solution:
            # solution found
            solved = True
            return node
        else:
            # expand child nodes (cities with connection)
            node_data = node.get_data();
            children_list = []
            for one_child in connections[node_data]:
                child = Node(one_child)
                children_list.append(child)
                if not child.on_list(visited_nodes) \
                   and not child.on_list(frontier_nodes):
                    frontier_nodes.append(child)

            node.set_children(children_list)
开发者ID:segabalanv,项目名称:intelligent_systems,代码行数:26,代码来源:spaniard_flights.py

示例7: main

def main():
    root = Node(0)
    root.left = Node(-10)
    root.left.left = Node(-20)
    root.left.right = Node(-5)
    root.right = Node(10)
    root.right.right = Node(20)
    print get_tree_median(root)
开发者ID:SageBerg,项目名称:TechInterviewPractice,代码行数:8,代码来源:halfway_through_tree.py

示例8: main

def main():
    root = Node(0)
    root.left = Node(-10)
    root.left.left = Node(-20)
    root.left.right = Node(-5)
    root.left.left.right = Node(-15)
    root.right = Node(10)
    print lca(root, -15, -5)
开发者ID:SageBerg,项目名称:TechInterviewPractice,代码行数:8,代码来源:lowest_common_ancestor.py

示例9: main

def main():
    root = Node(0)
    root.left = Node(1)
    root.left.left = Node(2)
    root.right = Node(1)
    root.right.right = Node(2)
    root.right.right.right = Node(3)
    root.right.left = Node(2)
    print is_balanced(root)
开发者ID:SageBerg,项目名称:CrackingTheCodingInterviewProblems,代码行数:9,代码来源:4.1.py

示例10: main

def main():
    root = Node(0)
    root.right = Node(1)
    print is_binary_search_tree(root, -float("inf"), float("inf"))
    root = Node(0)
    root.right = Node(2)
    root.right.left = Node(1)
    root.right.right = Node(1)
    print is_binary_search_tree(root, -float("inf"), float("inf"))
开发者ID:SageBerg,项目名称:CrackingTheCodingInterviewProblems,代码行数:9,代码来源:4.5.py

示例11: setUp

 def setUp(self):
     # create test objects
     self.n = Node('node1')
     self.n2 = Node('node2', '2')
     self.n3 = Node('node3', '3')
     self.n4 = Node('node4', '4')
     self.n5 = Node('node5', '5')
     self.n6 = Node('node6', '6')
     self.n7 = Node('node7', '7')
开发者ID:mkgilbert,项目名称:GDriveMgr,代码行数:9,代码来源:test_node.py

示例12: createMinTree

def createMinTree(low,high):
	if(low>high):
		return
	mid = (low + high)/2
	data = treeElements[mid]
	node = Node(data)
	node.left = createMinTree(low,mid-1)
	node.right = createMinTree(mid+1,high)
	return node
开发者ID:nirmalvp,项目名称:algos,代码行数:9,代码来源:minheighttree.py

示例13: main

def main():
    root = Node(0)
    root.left = Node(1)
    root.right = Node(2)
    root.left.left = Node(3)
    root.left.right = Node(4)
    root.right.left = Node(5)
    root.right.right = Node(6)
    for i in range(7):
        print bfs(root, i), "\n"
开发者ID:SageBerg,项目名称:TechInterviewPractice,代码行数:10,代码来源:bfs.py

示例14: main

def main():
    root = Node(1)
    root.set_left(1)
    root.left.set_left(2)
    root.left.set_right(1)
    root.set_right(2)
    root.right.set_left(1)
    root.right.set_right(1)
    root.right.left.set_left(2)
    count = [0]
    count_all_sums(root, count, 2)
    print count
开发者ID:SageBerg,项目名称:CrackingTheCodingInterviewProblems,代码行数:12,代码来源:4.9.py

示例15: build_tree

def build_tree(lst):
    if lst == []:
        return
    middle = len(lst) / 2
    left_list = lst[:middle]
    right_list = []
    if len(lst) > 1:
        right_list = lst[middle + 1:]
    node = Node(lst[middle])
    node.left = build_tree(left_list)
    node.right = build_tree(right_list)
    return node
开发者ID:SageBerg,项目名称:CrackingTheCodingInterviewProblems,代码行数:12,代码来源:4.3.py


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