def DecisionTree(examples = [], attributes = [], standard = ''): if not examples: return standard elif SameClassification(examples): return examples[0]['Accident Level'] elif not attributes: return MajorityValue(examples) else: best = ChooseAttribute(attributes, examples) #print(best) #print(attributes) tree = Node(best) m = MajorityValue(examples) different_values = DifferentValues(best, examples) #print(different_values) attributes.pop(attributes.index(best)) #print(attributes) for v_i in different_values: examples_i = BestEqualsVi(examples, best, v_i) #print(examples_i) #print(attributes_minus_best) #print(m) sub_tree = DecisionTree(examples_i, attributes, m) tree.add(Edge(tree, sub_tree, v_i)) return tree
def test_tree_traverseBF_1child(): n = Node(0) n.add(20) t = Tree() t.root = n f = lambda n : print("data={0}".format(n.data)) t.traverseBF(f)
def test_tree_node_remove_data_multi_diff_match(): n = Node(1) n.add(2) n.add(3) n.remove(2) n.remove(3) assert n.children[0] == None assert n.children[1] == None
def test_tree_traverseDF_1sibling(): n = Node(0) n.add(20) n.add(21) t = Tree() t.root = n f = lambda n : print("data={0}".format(n.data)) t.traverseDF(f)
def test_tree_traverseBF_1direct_grandchild(): n = Node(0) n.add(20) n.add(21) n.children[0].add(30) t = Tree() t.root = n f = lambda n : print("data={0}".format(n.data)) t.traverseBF(f)
def test_tree_traverseDF_1both_grandchild1sibling(): n = Node(0) n.add(20) n.add(21) n.children[0].add(30) n.children[1].add(31) t = Tree() t.root = n f = lambda n : print("data={0}".format(n.data)) t.traverseDF(f)
def play_node(me: int, board: board.Board, move: int, player: int, node: tree.Node) -> tree.Node: """ Play a move in the current board and for the parent node. :param me: player we're building the tree for :param board: current board :param move: move to make :param player: current player :param node: parent node :return: new node for the current move """ status = board.play(move, player) total_valid_moves_after_play = len(board.valid_moves) new_node = tree.Node(0, 1, move, board.state, player, node) # create a new node if node: node.add(new_node ) # if parent node is supplied -> add new node as a child new_node.status = status if status == board.WIN: if player == me: if node: node.winner = True # mark parent as a winner new_node.winner = True # score is equal to the number of valid moves that would be possible if a win didn't occur new_node.score = board.WIN * total_valid_moves_after_play new_node.total = total_valid_moves_after_play # print(move, 'wins the game!') else: if node: node.loser = True # mark parent as a loser new_node.score = board.LOSS * total_valid_moves_after_play new_node.total = total_valid_moves_after_play new_node.loser = True return new_node
def test_tree_node_add_child(): n = Node(1) n.add(2) assert n.children[0].data == 2
def test_tree_node_remove_data_mismatch(): n = Node(1) n.add(2) n.remove(3) assert n.children[0] != None
def test_tree_node_add_grandchild(): n = Node(1) n.add(2) n.children[0].add(3) assert n.children[0].children[0].data == 3
def test_tree_node_add_childAndSibling(): n = Node(1) n.add(2) n.add(3) assert n.children[0].data == 2 assert n.children[1].data == 3
from tree import Node if __name__ == "__main__": root = Node(10) root.add(7) root.add(11) print root.exists(int(input()))