当前位置: 首页>>代码示例>>Python>>正文


Python TreeNode.TreeNode类代码示例

本文整理汇总了Python中TreeNode.TreeNode的典型用法代码示例。如果您正苦于以下问题:Python TreeNode类的具体用法?Python TreeNode怎么用?Python TreeNode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了TreeNode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_rightSideView

 def test_rightSideView(self):
     s = LeetSolution()
     self.assertEqual([], s.rightSideView(None))
     root = TreeNode.makeTree([1, 2, 3, '#', 5, '#', 4])
     self.assertEqual([1, 3, 4], s.rightSideView(root))
     root = TreeNode.makeTree([1, 2, 3, '#', 5, '#', 4, 6])
     self.assertEqual([1, 3, 4, 6], s.rightSideView(root))
开发者ID:pengmeng,项目名称:LeetCode,代码行数:7,代码来源:test_leetSolution.py

示例2: buildTree

    def buildTree(self, inorder, postorder):
        """
        :type inorder: List[int]
        :type postorder: List[int]
        :rtype: TreeNode
        """

        if len(inorder) == 0 or len(inorder) != len(postorder):
            return None

        print 'inorder: ', inorder
        print 'postorder: ', postorder

        # key concept to understand is, when reaching root of inorder,
        # count of all left children are the same as postorder

        rootNum = postorder[-1]
        i = 0

        while i < len(inorder) and inorder[i] != rootNum:
            i += 1

        # i is at root number, start copying
        root = TreeNode(rootNum)

        root.left = self.buildTree(copy.deepcopy(inorder[0: i]), copy.deepcopy(postorder[0: i]))

        # @note:@memorize: here is the key, need to skip root node:
        #                   for inorder, it's list[i], so 2 halves: [0:i] and [i+1:]
        #                   for postorder, it's list[-1], so 2 halves: [0:i] and [i: -1]
        # root.right = self.buildTree(copy.deepcopy(inorder[i + 1: ]), copy.deepcopy(postorder[i + 1: ]))
        root.right = self.buildTree(copy.deepcopy(inorder[i + 1: ]), copy.deepcopy(postorder[i: -1]))

        return root
开发者ID:keypointt,项目名称:leetcode,代码行数:34,代码来源:Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal.py

示例3: construct

 def construct(l):
     if l is None or 0 == len(l):
         return None
     node = TreeNode(l[0])
     node.left = construct([i for i in l if i < node.val])
     node.right = construct([i for i in l if node.val < i])
     return node
开发者ID:hyunjun,项目名称:practice,代码行数:7,代码来源:construct_binary_search_tree_from_preorder_traversal.py

示例4: importJSONbusiness

def importJSONbusiness(dataFile):
    """
        parameters:
  dataFile - name of file containing business JSON objects

  returns:
  busData - list containing dictionaries representing Yelp businesses
  """

    busData = []
    rootNode = 0

    try:
        bus = open(dataFile)
    except IOError:
        print "Unable to open data file: ", dataFile
        return -1

    for line in bus:
        try:
            data = json.loads(line)
        except ValueError:
            print "Failed to convert JSON object to dictionary"
            return -1

        n = TreeNode()
        n.key = data["business_id"]
        n.value = data
        busData.append(n)
        if rootNode == 0:
            rootNode = n
        else:
            rootNode.insert(n)

    return (busData, rootNode)
开发者ID:jasenmh,项目名称:290D-Yelp,代码行数:35,代码来源:yelpdata.py

示例5: gen

def gen(l,r):
    if l> r:
        return [None]
    ans =[]
    for i in range(l,r+1):

        ltrees = gen(l,i-1)
        rtrees = gen(i+1,r)
        for j in range(len(ltrees)):
            for k in range(len(rtrees)):
                 root = TreeNode(i)
                 root.left = ltrees [j]
                 root.right = rtrees [k]
                 ans.append(root)

        # if ltrees != [None]:
        #     for lt in ltrees:
        #         root.left = lt
        #         if rtrees != [None]:
        #             for rt in rtrees:
        #                 root.right = rt
        #                 ans.append(root)
        #         else:
        #             root.right = None
        #             ans.append(root)
        # else:
        #     root.left = None
        #     if rtrees != [None]:
        #         for rt in rtrees:
        #             root.right = rt
        #             ans.append(root)
        #     else:
        #         root.right = None
        #         ans.append(root)
    return ans
开发者ID:buptconnor,项目名称:myleet,代码行数:35,代码来源:P95_generateTrees.py

示例6: buildTrees

    def buildTrees(self, start, end):

        if start > end:
            # @note:@memorize: should be something, [[]] or [None]
            # or the below for loop of left/right subtree will be skipped
            # return []
            return [None]

        if start == end: # reaching single node
            return [TreeNode(start)]

        result = []

        for i in range(start, end + 1): # end should be included
            # oneRoot = TreeNode(i) # @note: should be in inner loop
            leftSubtreeArray = self.buildTrees(start, i - 1)
            rightSubtreeArray = self.buildTrees(i + 1, end)

            for oneLeftSubtree in leftSubtreeArray:
                for oneRightSubtree in rightSubtreeArray:
                    # @note:@memorize: should be here, every append is a brand new tree
                    #                   or else, here is changing the reference for the same tree
                    oneRoot = TreeNode(i)
                    oneRoot.left = oneLeftSubtree
                    oneRoot.right = oneRightSubtree
                    result.append(oneRoot)

        return result
开发者ID:keypointt,项目名称:leetcode,代码行数:28,代码来源:Unique_Binary_Search_Trees_II.py

示例7: test_inorder

 def test_inorder(self):
     root = TreeNode.makeTree([1, '#', 2, 3])
     self.assertEqual([1, 3, 2], BTTraversal.inorder(root))
     root = TreeNode.makeTree([1, 2, 3, 4, 5, '#', 6])
     self.assertEqual([4, 2, 5, 1, 3, 6], BTTraversal.inorder(root))
     root = TreeNode.makeTree([3, 1, 4, '#', 2, '#', 5, '#', '#', '#', 6])
     self.assertEqual([1, 2, 3, 4, 5, 6], BTTraversal.inorder(root))
开发者ID:pengmeng,项目名称:LeetCode,代码行数:7,代码来源:test_bttraversal.py

示例8: _build_tree

    def _build_tree(self, X, y):
        '''
        INPUT:
            - X: 2d numpy array
            - y: 1d numpy array
        OUTPUT:
            - TreeNode

        Recursively build the decision tree. Return the root node.
        '''

        node = TreeNode()
        index, value, splits = self._choose_split_index(X, y)

        if index is None or len(np.unique(y)) == 1:
            node.leaf = True
            node.classes = Counter(y)
            node.name = node.classes.most_common(1)[0][0]
        else:
            X1, y1, X2, y2 = splits
            node.column = index
            node.name = self.feature_names[index]
            node.value = value
            node.categorical = self.categorical[index]
            node.left = self._build_tree(X1, y1)
            node.right = self._build_tree(X2, y2)
        return node
开发者ID:balajikvijayan,项目名称:MachineLearning,代码行数:27,代码来源:DecisionTree.py

示例9: build

    def build(self, inorder, postorder, startIndexInorder, endIndexInorder, startIndexPostorder, endIndexPostorder):

        if len(inorder) == 0 or len(inorder) != len(postorder) or startIndexInorder > endIndexInorder or startIndexPostorder > endIndexPostorder:
            return None

        # key concept to understand is, when reaching root of inorder,
        # count of all left children are the same as postorder

        rootNum = postorder[endIndexPostorder]
        count = 0

        while startIndexInorder + count <= endIndexInorder and inorder[startIndexInorder + count] != rootNum:
            count += 1

        # i is at root number, start copying
        root = TreeNode(rootNum)

        # @note:@memorize: here cannot use 'i - 1' as end of posterorder
        #                   eg. 2nd recursion, 'i - 1' = 1, that's for inorder
        #                   so it should be the ***count*** of how many nodes
        # root.left = self.build(inorder, postorder, startIndexInorder, i - 1, startIndexPostorder, i - 1)

        root.left = self.build(inorder, postorder, startIndexInorder, startIndexInorder + count - 1, startIndexPostorder, startIndexPostorder + count - 1)
        root.right = self.build(inorder, postorder, startIndexInorder + count + 1, endIndexInorder, startIndexPostorder + count, endIndexPostorder - 1)

        return root
开发者ID:keypointt,项目名称:leetcode,代码行数:26,代码来源:Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal.py

示例10: linkNode

 def linkNode(self, nodes, idx):
     if 0 == len(nodes):
         return None
     nodes[idx] = TreeNode(nodes[idx])
     nodes[idx].left = self.linkNode(nodes[:idx], idx // 2)
     nodes[idx].right = self.linkNode(nodes[idx + 1:], (len(nodes) - idx - 1 - 1) // 2)
     return nodes[idx]
开发者ID:hyunjun,项目名称:practice,代码行数:7,代码来源:delete_node_in_a_bst.py

示例11: sortedArrayToBST

 def sortedArrayToBST(self, nums):
     if not nums:
         return None
     mid = len(nums) / 2
     root = TreeNode(nums[mid])
     root.left = self.sortedArrayToBST(nums[:mid])
     root.right = self.sortedArrayToBST(nums[mid+1:])
     return root
开发者ID:YOUDAN,项目名称:Leetcode,代码行数:8,代码来源:ConvertSortedArrayToBinarySearchTree.py

示例12: doit

 def doit():
     val = next(vals)
     if val == "#":
         return None
     node = TreeNode(int(val))
     node.left = doit()
     node.right = doit()
     return node
开发者ID:YusiZhang,项目名称:leetcode-python,代码行数:8,代码来源:Codec.py

示例13: sortedArrayToBST

 def sortedArrayToBST(self, array):
     length = len(array)
     if length == 0: return None
     if length == 1: return TreeNode(array[0])
     root = TreeNode(array[length / 2])
     root.left = self.sortedArrayToBST(array[:length / 2])
     root.right = self.sortedArrayToBST(array[length / 2 + 1:])
     return root
开发者ID:keypointt,项目名称:leetcode,代码行数:8,代码来源:Convert_Sorted_List_to_Binary_Search_Tree.py

示例14: sortedArrayToBSTRecur

 def sortedArrayToBSTRecur(self, nums):
     if nums is None or 0 == len(nums):
         return None
     mid = len(nums) // 2
     node = TreeNode(nums[mid])
     node.left = self.sortedArrayToBSTRecur(nums[:mid])
     node.right = self.sortedArrayToBSTRecur(nums[mid + 1:])
     return node
开发者ID:hyunjun,项目名称:practice,代码行数:8,代码来源:convert_sorted_array_to_binary_search_tree.py

示例15: test_preorder

 def test_preorder(self):
     self.assertEqual([], BTTraversal.preorder([]))
     root = TreeNode.makeTree([1, '#', 2, 3])
     self.assertEqual([1, 2, 3], BTTraversal.preorder(root))
     root = TreeNode.makeTree([1, 2, 2, '#', 3, '#', 3])
     self.assertEqual([1, 2, 3, 2, 3], BTTraversal.preorder(root))
     root = TreeNode.makeTree([3, 1, 2])
     self.assertEqual([3, 1, 2], BTTraversal.preorder(root))
开发者ID:pengmeng,项目名称:LeetCode,代码行数:8,代码来源:test_bttraversal.py


注:本文中的TreeNode.TreeNode类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。