# Uncommenting all 3 lines gives following Balanced Tree
    # 5 C
    #  2 L
    #   1 L
    #   3 R
    #    4 R
    #  8 R
    #   6 L
    #    7 R
    #   9 R
    #
    # =============================================================================

    # Starting from tn5 (which is root), print the Tree
    print('Printing Binary Tree...')
    priint_tree(tn5, 'C')
    print()

# Algorithm to check if Tree is Balanced


############
# APPROACH#1
############
def getHeight(tn):
    if tn == None:
        return 0

    # The maximum height from left branch and right branch plus one (for its own level)
    # is considered a TreeNodes height
    return max(getHeight(tn.left), getHeight(tn.right)) + 1
    tn30 = TreeNode(30)
    tn17 = TreeNode(17)

    tn20.left = tn10
    tn20.right = tn30

    tn10.parent = tn20
    tn10.left = tn05
    tn10.right = tn15

    tn30.parent = tn20

    tn05.parent = tn10
    tn05.left = tn03
    tn05.right = tn07

    tn15.parent = tn10
    tn15.right = tn17

    tn03.parent = tn05
    tn07.parent = tn05
    tn17.parent = tn15

    priint_tree(tn20)

    #print(depth(None))
    priint_ca(common_ancestor(tn20, tn05, tn07))
    # priint_ca(common_ancestor(tn20, tn07, tn30))
    # priint_ca(common_ancestor(tn20, tn07, tn17))
    # priint_ca(common_ancestor(tn20, tn07, tn10))
def createMinimalBST_(arr, start, end, side):

    if end < start:
        if DEBUG:
            print(f'start {start}, end {end}, side {side}')
        return None

    # Find the mid-point
    mid = (start + end) // 2
    if DEBUG:
        print(
            f'start {start}, end {end}, side {side}, mid {mid}, value = {arr[mid]}'
        )

    mid_node = TreeNode(arr[mid])

    # Invoke BST Creation for Left Sub Tree
    mid_node.left = createMinimalBST_(arr, start, mid - 1, 'left')

    # Invoke BST Creation for Right Sub Tree
    mid_node.right = createMinimalBST_(arr, mid + 1, end, 'right')
    return mid_node


if __name__ == "__main__":
    #Test - Uncomment when needed to test
    array = range(1, 10)
    bst = createMinimalBST(array)
    print("\nPrinting Binary Search Tree...\n")
    priint_tree(bst, 'C')
Esempio n. 4
0
    
    binary_tree_to_array(node.right, arr)

def checkSorted(arr):
    for index in range(len(arr)-1):
        if arr[index] <= arr[index+1]:
            continue
        else:
            return False
    return True

if __name__ == "__main__":
    # Test-1
    print('Test-1 :: APPROACH#1')
    bst1 = bstBuilder.createMinimalBST(range(1, 11))
    priint_tree(bst1)
    # Now pass on this bst to our new function binary_tree_to_array to create an array out of it.
    arr = []
    binary_tree_to_array(bst1, arr)
    print(arr)
    # Now let's check if the arr comes out as sorted
    res = checkSorted(arr)
    result = 'is' if res else 'is not'
    print(f'Given Binary tree {result} Binary Search Tree.\n')
    
    # Test-2
    print('Test-2 :: APPROACH#1')
    bst2 = bstBuilder.createMinimalBST([19, 20, 21, 22, 24, 24, 25, 25])
    priint_tree(bst2)
    # Now pass on this bst to our new function binary_tree_to_array to create an array out of it.
    arr = []
Esempio n. 5
0
    tn4.parent = tn3

    tn8.parent = tn5
    tn8.left = tn6
    tn8.right = tn9

    tn6.parent = tn8
    tn6.right = tn7

    tn7.parent = tn6

    tn9.parent = tn8
    tn9.right = tn10

    priint_tree(tn5)

    # Let's find the next node for tn5
    node = get_next_node(tn5)
    name = node.name if node is not None else 'None'
    print(f'\nNext node for {tn5.name} = {name}')

    # Let's find the next node for tn1
    node = get_next_node(tn1)
    name = node.name if node is not None else 'None'
    print(f'\nNext node for {tn1.name} = {name}')

    # Let's find the next node for tn1
    node = get_next_node(tn7)
    name = node.name if node is not None else 'None'
    print(f'\nNext node for {tn7.name} = {name}')