예제 #1
0
def left_rotate(node: BinaryTreeNode):
    right = node.right
    if right:
        node.right = right.left
        right.left = node
        return right
    return node
예제 #2
0
 def _build_tree(arr):
     if not arr:
         return None
     mid = (0 + len(arr)) // 2
     root = BinaryTreeNode(arr[mid])
     root.left = _build_tree(arr[0:mid - 1])
     root.right = _build_tree(arr[mid + 1:len(arr)])
     return root
예제 #3
0
 def _build_tree(s, e):
     if e < s:
         return None
     nonlocal l_node
     mid = s + (e - s) // 2
     left = _build_tree(s, mid - 1)
     root = BinaryTreeNode(l_node.val)
     root.left = left
     l_node = l_node.next
     root.right = _build_tree(mid + 1, e)
     return root
예제 #4
0
    def _gen_trees(s, e):
        ret = []
        if s > e:
            ret.append(None)
        for idx in range(s, e + 1):
            left_trees = _gen_trees(s, idx - 1)
            right_trees = _gen_trees(idx + 1, e)

            for l, r in product(left_trees, right_trees):
                root = BinaryTreeNode(idx)
                root.left = l
                root.right = r
                ret.append(root)
        return ret
                    i = 2 * i + 1
                else:
                    not_heap = False
        else:
            not_heap = False

    print(heap)        
    return root

if __name__ == '__main__':
    a = BinaryTreeNode(1)
    b = BinaryTreeNode(2)
    c = BinaryTreeNode(3)
    d = BinaryTreeNode(4)
    e = BinaryTreeNode(5)
    f = BinaryTreeNode(6)
    g = BinaryTreeNode(7)
    h = BinaryTreeNode(8)
    i = BinaryTreeNode(9)
    d.left = b
    d.right = f
    d.left.left = a
    d.left.right = c
    d.right.left = e
    d.right.right = g
    d.right.right.right = h
    d.right.right.right.right = i
    print(depth(d, d))
    a = [5, 9, 6, 12, 10, 13, 7, 18, 14]
    print(heap_remove(a))