本文整理汇总了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)
示例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
示例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'
示例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)
示例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
示例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)
示例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)
示例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)
示例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)
示例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"))
示例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')
示例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
示例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"
示例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
示例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