本文整理汇总了Python中tree.Node.add_child方法的典型用法代码示例。如果您正苦于以下问题:Python Node.add_child方法的具体用法?Python Node.add_child怎么用?Python Node.add_child使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Node
的用法示例。
在下文中一共展示了Node.add_child方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: grow_tree
# 需要导入模块: from tree import Node [as 别名]
# 或者: from tree.Node import add_child [as 别名]
def grow_tree(records, attributes, default = ''):
if stopping_condition(records, attributes):
label = classify(records)
if label is None:
label = default
return Node(label = label)
else:
i, condition = find_best_split(records, attributes)
default = classify(records)
attributes[-1], attributes[i] = attributes[i], attributes[-1]
name, v, values = attributes.pop()
if v != False:
root = Node(label = name + '<=' + str(v), test = condition)
values = [True, False]
else:
root = Node(label = name, test = condition)
for value in values:
new_records = filter(lambda r: condition(r) == value, records)
child = grow_tree(new_records, attributes, default)
root.add_branch(value)
root.add_child(child)
attributes.append( (name, v, values) )
attributes[-1], attributes[i] = attributes[i], attributes[-1]
return root
示例2: build_game_tree
# 需要导入模块: from tree import Node [as 别名]
# 或者: from tree.Node import add_child [as 别名]
def build_game_tree(self, depth, board, is_max, number):
cpy = copy.deepcopy(board)
r = Node(cpy, is_max, None)
if depth == 0 :
return r
el_moves = self.get_eligible_for_board(cpy, number)
for move in el_moves:
new_board, free_move = self.board._dummy_move_stones(number, move, cpy)
r_num = number if free_move else self.flip_number(number)
ci = self.build_game_tree(depth-1, new_board, (is_max if free_move else not is_max), r_num)
ci.set_move(move)
r.add_child(ci)
return r
示例3: TestNodeMethods
# 需要导入模块: from tree import Node [as 别名]
# 或者: from tree.Node import add_child [as 别名]
class TestNodeMethods(unittest.TestCase):
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')
def tearDown(self):
# set objects to none or delete stuff
self.n = None
self.n2 = None
def test_get_id_where_no_id(self):
self.assertEqual(self.n.get_id(), None)
def test_get_id_and_title_n_n2(self):
self.assertEqual(self.n.get_title(), 'node1')
self.assertEqual(self.n2.get_title(), 'node2')
self.assertEqual(self.n2.get_id(), '2')
def test_string_repr(self):
self.assertEqual(self.n.__str__(), 'node1')
self.assertEqual(self.n2.__str__(), 'node2')
def test_adding_2_children(self):
self.n.add_child(self.n2)
self.n.add_child(self.n3)
children = Set()
children.add(self.n2)
children.add(self.n3)
self.assertEqual(self.n.get_children(), children)
def test_parent_id(self):
n = Node(id='6', parent_id='1')
self.assertEqual(n.get_parent_id(), '1')
def test_set_and_get_parent_id(self):
n = Node(id='1')
n.set_parent_id(0)
id = n.get_parent_id()
self.assertEqual(0, id)