def decision_tree_learning(examples, attributes, binary_targets): if same_binary_targets(binary_targets): return TreeNode.create_leaf(binary_targets[0], 0) elif len(attributes) == 0: counter = collections.Counter(binary_targets) return TreeNode.create_leaf(majority_value(binary_targets), get_entropy(counter[1], counter[0])) else: best_attribute = choose_best_decision_attribute(examples, attributes, binary_targets) tree = TreeNode.create_internal(best_attribute) for v in [0, 1]: v_examples = [] v_binary_targets = [] for i in range(0, len(examples)): example = examples[i] if example[best_attribute - 1] == v: v_examples.append(example) v_binary_targets.append(binary_targets[i]) if len(v_examples) == 0: counter = collections.Counter(binary_targets) return TreeNode.create_leaf(majority_value(binary_targets), get_entropy(counter[1], counter[0])) else: attributes.remove(best_attribute) subtree = decision_tree_learning(v_examples, attributes, v_binary_targets) tree.add_kid(subtree) attributes.append(best_attribute) return tree
def postorderTraversal(self, root: TreeNode): cur = TreeNode() ans = [] cur.left = root while cur is not None: left = cur.left if left is None: cur = cur.right else: while left.right is not None: left = left.right pre = left if pre.right is None: pre.right = cur cur = cur.left else: tmp = [] tmp_p = cur.left while tmp_p is not pre: tmp.append(tmp_p.val) tmp_p = tmp_p.right tmp.append(pre.val) pre.right = None cur = cur.right ans += tmp.reverse() return ans
def deserialize(self, data): """Decodes your encoded data to tree. :type data: str :rtype: TreeNode """ values = [ int(value) if value != ' ' else None for value in data.split('*') ] if not (values and values[0] is not None): return None root = TreeNode(values[0]) node_queue = collections.deque([root]) value_queue = collections.deque(values[1:]) while node_queue: node = node_queue.popleft() if node is None: continue left_check, right_check = False, False while value_queue and not (left_check and right_check): value = value_queue.popleft() if not left_check: node.left = TreeNode(value) if value is not None else None node_queue.append(node.left) left_check = True elif not right_check: node.right = TreeNode(value) if value is not None else None node_queue.append(node.right) right_check = True return root
def __init__(self): TreeNode.__init__(self) self.parent = None self.leftChild = None self.rightChild = None self.value = None
def test_set_left_twice(self): key1 = 'a' root = TreeNode(key1) key2 = 'b' node = root.set_right(key2) self.assertEqual(None, root.parent) self.assertEqual(key1, root.key) self.assertEqual(None, root.left) self.assertEqual(node, root.right) self.assertEqual(root, node.parent) self.assertEqual(key2, node.key) self.assertEqual(None, node.left) self.assertEqual(None, node.right) key3 = 'c' node = root.set_right(key3) self.assertEqual(None, root.parent) self.assertEqual(key1, root.key) self.assertEqual(None, root.left) self.assertEqual(node, root.right) self.assertEqual(root, node.parent) self.assertEqual(key3, node.key) self.assertEqual(None, node.left) self.assertEqual(None, node.right)
def print_supported_formats(): from tree import TreeNode t = TreeNode() t.populate(4, "ABCDEFGHI") print t for f in NW_FORMAT: print "Format", f, "=", write_newick(t, features=None, format=f)
def bstFromPreorder(self, preorder: List[int]) -> TreeNode: """ :type preorder: List[int] :rtype: TreeNode """ if not preorder: return None root = TreeNode(preorder[0]) stack = [root] for i in range(1, len(preorder)): val = preorder[i] node = TreeNode(val) if val < stack[-1].val: stack[-1].left = node stack.append(node) else: parent = stack.pop() while stack and stack[-1].val < val: parent = stack.pop() parent.right = node stack.append(node) return root
def __init__(self, tree_string, tree_index, D=None, ID=None, IR=None, target_dl=None): # type: (TreeWithDataDependencies, str, int, list, list, float) -> None self.t = TreeNode(tree_string, format=1) self.precedence_dict = dict() if target_dl is None: self.target_determinism_level = 0.5 else: self.target_determinism_level = target_dl if D is None: self.D = [] else: self.D = D if ID is None: self.ID = [] else: self.ID = ID if IR is None: self.IR = [] else: self.IR = IR self.choice_labels = dict() self.Delta = dict() # test if there are choice nodes in tree to determine if data dependencies are possible self.data_dependencies_possible = (len( self.t.search_nodes(name='choice')) > 0) self.tree_index = tree_index
def deserialize(self, data): """Decodes your encoded data to tree. :type data: str :rtype: TreeNode """ dummy = TreeNode(0) q = collections.deque() q.append((dummy, True)) for val in data.split(' '): parent, is_left = q.popleft() if val == '#': continue node = TreeNode(int(val)) if is_left: parent.left = node else: parent.right = node q.append((node, True)) q.append((node, False)) return dummy.left
def deserialize(self, data): """Decodes your encoded data to tree. :type data: str :rtype: TreeNode """ if data == '# #': return None nodes = data.split() root = TreeNode(int(nodes[1])) queue = collections.deque([root]) index = 2 while queue: node = queue.popleft() if nodes[index] is not '#': node.left = TreeNode(int(nodes[index])) queue.append(node.left) index += 1 if nodes[index] is not '#': node.right = TreeNode(int(nodes[index])) queue.append(node.right) index += 1 return root
def buildTree(self, preorder, inorder): """ :type preorder: List[int] :type inorder: List[int] :rtype: TreeNode """ # preorder = [3, 9, 20, 15, 7] # inorder = [9, 3, 15, 20, 7] if len(preorder) == 0: return None if len(preorder) == 1: return TreeNode(preorder[0]) root = TreeNode(preorder[0]) root_val = preorder[0] i = 0 while i < len(inorder) and inorder[i] != root_val: i += 1 left_inorder = inorder[:i] right_inorder = inorder[i + 1:] left_preorder = preorder[1:len(left_inorder) + 1] right_preorder = preorder[len(left_inorder) + 1:] root.right = self.buildTree(right_preorder, right_inorder) root.left = self.buildTree(left_preorder, left_inorder) return root
def get_tree_ast(str): """Возвращает AST """ lst = get_ast(str) tree_ast = SimpleTree(TreeNode(None, None)) new_lst = lst.copy() while len(new_lst) != 0: token = new_lst.pop(0) #print(token) if token == '(': tree_ast.add_child(TreeNode(tree_ast.current, None)) tree_ast.current = tree_ast.current.child[0] elif token == ')': tree_ast.current = tree_ast.current.parent elif token != '/' and token != '*' and token != '-' and token != '+': tree_ast.current.value = token tree_ast.current = tree_ast.current.parent else: tree_ast.current.value = token #print('value =', tree_ast.current.value) #print('level =', tree_ast.current.level) tree_ast.add_child(TreeNode(tree_ast.current, None)) tree_ast.current = tree_ast.current.child[1] return tree_ast
def createSampleTree(): node = TreeNode(5) leftNode = TreeNode(3) node.setLeftChild(leftNode) rightNode = TreeNode(7) node.setRightChild(rightNode) return node
def build_tree(preorder, inorder): print(preorder) print(inorder) if len(preorder) != len(inorder): print("Invalid") sys.exit(0) if len(preorder) == 1 and len(inorder) == 1: return TreeNode(preorder[0]) #preorder's 1st item is the root root = TreeNode(preorder[0]) # get location of root in Inorder root_ix = inorder.index(root.data) # left of root's location is leftsubtree left_in = inorder[:root_ix] #right of root's location is right subtree right_in = inorder[root_ix + 1:] left_pre = preorder[1:len(left_in) + 1] right_pre = preorder[1 + len(left_in):] if len(left_pre) > 0: root.left = build_tree(left_pre, left_in) if len(right_pre) > 0: root.right = build_tree(right_pre, right_in) return root
def debug_print(input_f: str, limit: int = 50) -> None: with open(input_f, "r") as f: for lid, line in enumerate(f.readlines()): if lid >= limit: break data = json.loads(line) print(f"********Conversation {lid+1}********") for tid, turn in enumerate(data["turns"]): print(f"***Turn {tid}***") utterance = turn["utterance"] print(f"Utterance: {utterance}") if tid > 0: input_ds = pretty_print_tree( TreeNode.from_dict(turn["input_dialog_state"])) print(f"Last dialog state:\n{input_ds}\n") input_das = turn["input_system_acts"] das = [] for da in input_das: das.append( pretty_print_tree(TreeNode.from_dict(da["paths"]))) das = "\n".join(das) print(f"Last system acts:\n{das}\n") target_ds = pretty_print_tree( TreeNode.from_dict(turn["target_dialog_state"])) print(f"Target dialog state:\n{target_ds}\n") print("********End of Conversation********")
def deserialize(self, data): """Decodes your encoded data to tree. :type data: str :rtype: TreeNode """ rootParent = TreeNode(sys.maxsize) parent = rootParent stack = [] for val in data.split(): val = int(val) node = TreeNode(val) if val < parent.val: parent.left = node else: while parent.val <= val: parent = stack.pop() stack.append(parent) parent = parent.left while parent.right: stack.append(parent) parent = parent.right parent.right = node stack.append(parent) parent = node return rootParent.left
def second_solution(preorder: List[int], inorder: List[int]) -> TreeNode: if inorder: index = inorder.index(preorder.pop(0)) node = TreeNode(inorder[index]) node.left = second_solution(preorder, inorder[:index]) node.right = second_solution(preorder, inorder[index + 1:]) return node
def print_supported_formats(): from tree import TreeNode t = TreeNode() t.populate(4, "ABCDEFGHI") print t for f in NW_FORMAT: print "Format", f,"=", write_newick(t, features=None, format=f)
def test_height_of_one_level_tree(self): node1 = TreeNode('1') node11 = node1.set_left('11') node12 = node1.set_right('12') self.assertEqual(1, node1.height) self.assertEqual(0, node11.height) self.assertEqual(0, node12.height)
def create_minimal_BST(arr: list, start: int, end: int) -> Optional[TreeNode]: if end < start: return None mid = math.ceil((start + end) / 2) mid_node = TreeNode(arr[mid]) mid_node.left = create_minimal_BST(arr, start, mid - 1) mid_node.right = create_minimal_BST(arr, mid + 1, end) return mid_node
def reconstruct(start, end): if start >= end: return None mid = start + ((end - start) >> 1) curr = TreeNode(values[mid]) curr.left = reconstruct(start, mid) curr.right = reconstruct(mid + 1, end) return curr
def test_size_of_one_level_tree(self): node1 = TreeNode('1') node11 = node1.set_left('11') node12 = node1.set_right('12') self.assertEqual(3, node1.size) self.assertEqual(1, node11.size) self.assertEqual(1, node12.size)
def third_solution(t1: TreeNode, t2: TreeNode) -> TreeNode: if t1 and t2: node = TreeNode(t1.val + t2.val) node.left = third_solution(t1.left, t2.left) node.right = third_solution(t1.right, t2.right) return node else: return t1 or t2
def preorder(q): cur = q.pop(0) if cur == '#': return None root = TreeNode(int(cur)) root.left = preorder(q) root.right = preorder(q) return root
def _to_bst(nums, start_idx, end_idx): if start_idx > end_idx: return None mid_idx = int(start_idx + (end_idx - start_idx) // 2) root = TreeNode(nums[mid_idx]) root.left = _to_bst(nums, start_idx, mid_idx - 1) root.right = _to_bst(nums, mid_idx + 1, end_idx) return root
def parse(self): tree = TreeNode() while True: try: tree.insert(input()) except EOFError: break return tree
def sorted_list_to_bst(l): if len(l) == 0: return None mid = len(l) / 2 root = TreeNode(l[mid]) root.left = sorted_list_to_bst(l[:mid]) root.right = sorted_list_to_bst(l[mid + 1:]) return root
def test_repr_node_with_parent(self): root = TreeNode('a') node = root.add_child('b') self.assertEqual( '[key=a, parent=None, children=[[key=b, parent=a, children=[]]]]', repr(root)) self.assertEqual('[key=b, parent=a, children=[]]', repr(node))
def bt_from_preorder_and_inorder(preorder, inorder): if len(preorder) <= 0: return None root = TreeNode(preorder[0]) index = inorder.index(preorder[0]) root.left = bt_from_preorder_and_inorder(preorder[1: 1 + index], inorder[0: index]) root.right = bt_from_preorder_and_inorder(preorder[1 + index:], inorder[index + 1:]) return root
def inorder(tree: TreeNode) -> int: string: str = "" if tree.getLeftChild() is not None: string += "(" + inorder(tree.getLeftChild()) string += str(tree.getRootValue()) if tree.getRightChild() is not None: string += inorder(tree.getRightChild()) + ")" return string
def qlearn3(resume=True): root_state = State(ACTORS, PLACES, ITEMS) root_node = TreeNode(state=root_state, parent_edge=None, possible_methods=True) from tree import POSSIBLE_METHODS num_methods = len(POSSIBLE_METHODS) table2 = {} eps = 0.2 if resume: with open("table2.pickle", "rb") as table2file: table2 = pickle.load(table2file) current_node = root_node edge = None depth = 0 counter = 0 prob_dist = initialize_prob_dist() while True: if depth >= 20: depth = 0 prob_dist = initialize_prob_dist() counter += 1 edge = None #print() if counter % 100 == 0: print("Counter - " + str(counter) + " - Dumping To File") with open("table2.pickle", "wb") as table2file: pickle.dump(table2, table2file, protocol=pickle.HIGHEST_PROTOCOL) root_state = State(ACTORS, PLACES, ITEMS) root_node = TreeNode(state=root_state, parent_edge=None, possible_methods=True) current_node = root_node continue next_edge = expand_heuristic_edge(current_node, prob_dist) expand_all_believable_edges(node=current_node, debug=True) best_edge = choose_max_q_edge(node=current_node) if edge != None: reward = percent_goals_satisfied(current_node, GOALS) idx = state_index_number_2(edge.prev_node.state) if idx not in table2: table2[idx] = [0.1] * num_methods idxc = state_index_number_2(current_node.state) if idxc not in table2: table2[idxc] = [0.1] * num_methods #print(idxc) #print(idx) #print(len(POSSIBLE_METHODS)) bestqval = table2[idxc][find_edge_index(best_edge)] qval = table2[idx][find_edge_index(edge)] table2[idx][find_edge_index( edge)] = qval + 0.1 * (reward + 0.9 * (bestqval) - qval) #print("{} {} {}".format(edge.method.sentence, reward, edge.qval)) edge = next_edge depth += 1 current_node = edge.next_node
def generate_optimal(self, i, j): ind_k = self.k[i][j] if(ind_k is not None): root = TreeNode(self.values[self.k[i][j]]) root.left = self.generate_optimal(i, ind_k - 1) root.right = self.generate_optimal(ind_k, j) return root else: return None
def test_breadth_first_traverse_gen_on_one_level_tree(self): node1 = TreeNode('1') node11 = node1.add_child('11') node12 = node1.add_child('12') node13 = node1.add_child('13') generated_nodes = [n for n in node1.breadth_first_traverse_gen()] self.assertEqual([node1, node11, node12, node13], generated_nodes)
def build(ns): ln = len(ns) if not ln: return m = (ln-1)/2 node = TreeNode(ns[m]) node.left = build(ns[0:m]) node.right = build(ns[m+1:]) return node
def create_min_bst_aux(a, l, r): if not a: return None if l > r: return None mid = l + (r - l) // 2 root = TreeNode(a[mid]) root.left = create_min_bst_aux(a, l, mid - 1) root.right = create_min_bst_aux(a, mid + 1, r) return root
def __init__(self, definition): TreeNode.__init__(self) self.definition = definition self.solution = definition.solution self.files = [] self.configurations = [] #print "Loading " + definition.type + " project at: " + definition.absolutePath # Is there an actual file related to this project? if (definition.type == "general"): pass # TODO: Add support for other kinds of projects. Do I need to make a distinction?? if (definition.type == "cpp"): try: # First try to open the filter file which is the tree we prefer to show tree = ET.parse(definition.absolutePath + ".filters") except Exception as e: try: # Apparently there's no filter file, just open the regular vcxproj tree = ET.parse(definition.absolutePath) except Exception as e: print "Project " + definition.name + " could not be opened at \"" + definition.absolutePath + "\". Skipping." print e self.loaded = False return root = tree.getroot() # Get the namespace since every tag returned by etree is prefixed by it # so we're gonna need it to find element using xPath self.xmlns = re.match("{(.*?)}", root.tag).group(1) itemGroups = root.findall(".//{%s}ItemGroup" % (self.xmlns)); for g in itemGroups: # TODO: Apparently if it doesn't have a label then it's a group of # files we want to show? There's probably a better way to determine that if g.get("Label") != None: continue for i in g: # Ignore filters since each file's filter specify the same information if i.tag == ("{%s}Filter" % (self.xmlns)): continue # Ignore project references (this are project dependencies, maybe we want those?) if i.tag == ("{%s}ProjectReference" % (self.xmlns)): continue # All items I've seen have the Include property but just to make sure if i.get("Include") != None: self.__ReadFile(i) self.loaded = True
def sortedArrayToBST(self, nums): """ :type nums: List[int] :rtype: TreeNode """ root, size = None, len( nums ) if size > 0: i = size / 2 root = TreeNode( nums[ i ] ) root.left = self.sortedArrayToBST( nums[ : i ] ) root.right = self.sortedArrayToBST( nums[ i + 1 : ] ) return root
def __getcharlist(message): cdict = {} for c in message: if not cdict.__contains__(c): node = TreeNode() node.data = c node.count = 1 cdict[c] = node else: cdict[c].count += 1 res = list(cdict.copy().values()) if DEBUG: __printList(res) return res
def hypergraph_from_line(line): line = line.strip() # Berkeley parser will output lines like # "Don't have a 7-best tree" when you ask it # for 10 best, but it only has 6. if line.startswith('Don\'t have a'): return None is_one_of_kbest = True parts = line.split('\t') if len(parts) == 1: score = 0.0 line, = parts is_one_of_kbest = False elif len(parts) == 2: score, line = parts score = float(score) else: sent_prob, joint_prob, line = parts score = float(joint_prob) - float(sent_prob) if score == '-Infinity' or line == '(())': return None score = math.exp(score) # Strip the extra parens # TODO: What if the trees have different root nodes? if line.startswith('( (') and line.endswith(') )'): line = line[2:-2] tree = TreeNode.from_string(line) return tree, score, is_one_of_kbest
def generate_tree(self, da, gen_doc=None): root = TreeNode(TreeData()) self.candgen.init_run(da) nodes = deque([self.generate_child(root)]) treesize = 1 while nodes and treesize < self.MAX_TREE_SIZE: node = nodes.popleft() for _ in xrange(self.candgen.get_number_of_children(node.formeme)): child = self.generate_child(node) if child: nodes.append(child) treesize += 1 if gen_doc: zone = self.get_target_zone(gen_doc) zone.ttree = root.create_ttree() return return root.tree
def generate_tree(self, da, gen_doc=None): root = TreeNode(TreeData()) cdfs = self.candgen.get_merged_child_type_cdfs(da) nodes = deque([self.generate_child(root, da, cdfs[root.formeme])]) treesize = 1 while nodes and treesize < self.MAX_TREE_SIZE: node = nodes.popleft() if node.formeme not in cdfs: # skip weirdness continue for _ in xrange(self.candgen.get_number_of_children(node.formeme)): child = self.generate_child(node, da, cdfs[node.formeme]) nodes.append(child) treesize += 1 if gen_doc: zone = self.get_target_zone(gen_doc) zone.ttree = root.create_ttree() return return root.tree
def run(self): try: # Create a state of the initial game self.state = State(self.game.grid) free, highest = self.state.free_tiles() # Run the algorithm one step if free <= 1: root = TreeNode(self.state, None, 7) elif free <= 2: root = TreeNode(self.state, None, 6) elif free <= 3: root = TreeNode(self.state, None, 4) elif free <= 6: root = TreeNode(self.state, None, 3) else: root = TreeNode(self.state, None, 3) # root = TreeNode(self.state, None, 4) # Chose dept depentdent on free tiles direc, way = root.get_move() # Log the move if way is None: # self.log.write_log() pass else: self.log.add_log(copy.deepcopy(self.game.grid), way) self.log.write_log() return direc, highest except Exception, err: eprint(err)