def createTreeNode(self, root, data, depth): """ Function: 用输入的data设置树结点,并记录当前结点的深度 Parameters: data: numpy array of shape [n_samples, n_features + 1] 数据的特征+数据的类别 depth: 当前树结点的深度 Returns: root: 返回树结点 """ # 当前层数等于maxDepth时,将此树结点视为叶节点,此结点将不再分裂,并且将majorClass设为value if self.maxDepth == depth: y = [example[-1] for example in data] root.val = majorityClass(y) root.targetValue = y root.isLeaf = True return # 若当前树结点不为叶节点,我们根据划分后的信息增益最大化,选择最佳的feature和value root.splitFeature, root.splitValue = chooseBestFeatureToSplit(data) leftData, rightData = splitDataSet(data, axis=root.splitFeature, value=root.splitValue) root.leftNode = TreeNode() root.rightNode = TreeNode() self.createTreeNode(root.leftNode, leftData, depth + 1) self.createTreeNode(root.rightNode, rightData, depth + 1)
def buildTree(self, preorder, inorder): """ :type preorder: List[int] :type inorder: List[int] :rtype: TreeNode """ if len(inorder) == 0 or len(preorder) != len(inorder): return None root = TreeNode(preorder[0]) root_index = inorder.index(preorder[0]) root.left = self.buildTree(preorder[1:root_index+1], inorder[0:root_index]) root.right = self.buildTree(preorder[root_index+1:], inorder[root_index+1:]) return root
def buildTree(self, inorder, postorder): """ :type inorder: List[int] :type postorder: List[int] :rtype: TreeNode """ if len(inorder) == 0 or len(postorder) != len(inorder): return None root = TreeNode(postorder[-1]) root_index = inorder.index(postorder[-1]) root.left = self.buildTree(inorder[0:root_index], postorder[0:root_index]) root.right = self.buildTree(inorder[root_index + 1:], postorder[root_index: len(postorder) - 1]) return root
def _generateTrees(l: int, r: int) -> List[TreeNode]: res = [] if l > r: res.append(None) elif l == r: res.append(TreeNode(l)) else: for i in range(l, r + 1, 1): left_nodes = _generateTrees(l, i - 1) right_nodes = _generateTrees(i + 1, r) for left_node in left_nodes: for right_node in right_nodes: root = TreeNode(i) root.left = left_node root.right = right_node res.append(root) return res
def __mkTreeByPreorder(cls, arr: List[int], idx_pointer: List[int]) -> TreeNode: value = arr[idx_pointer[0]] root = None if (value == -1) else TreeNode(value, None, None) idx_pointer[0] += 1 if root: root.left = TreeUtil.__mkTreeByPreorder(arr, idx_pointer) root.right = TreeUtil.__mkTreeByPreorder(arr, idx_pointer) return root
def _qs_add_node(self, entry_obj): logger.debug('Processing {} entry.'.format(entry_obj)) tmp_node = self.get_node_by_entry_key(entry_obj.id) if tmp_node is not None: return tmp_node else: qs_parent = entry_obj.parent_ref if qs_parent is None: # root has no parents root_node = TreeNode(entry_obj.id, entry_obj.value) self.root = root_node self.nodes.append(root_node) return root_node else: parent_node = self._qs_add_node(qs_parent) if parent_node is not None: new_node = TreeNode(entry_obj.id, entry_obj.value) # new_node.set_parent(parent_node) new_node.set_parent(parent_node.key) parent_node.add_child(new_node) self.nodes.append(new_node) return new_node
def __init__(self, root=None): TreeNode.__init__(self) self.nodes = [] self.root = root self.__dict = []
def __init__(self, maxDepth): self.maxDepth = maxDepth self.root = TreeNode()
/ \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 ''' from tree.TreeNode import TreeNode class Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if root: root.left, root.right = self.invertTree( root.right), self.invertTree(root.left) return root test_data = [4, 2, 7, 1, 3, 6, 9] print('Input data is ', test_data) tree = TreeNode.convert_list_to_tree(test_data) root = Solution().invertTree(tree) print('Output data is ', TreeNode.convert_tree_to_list(root))