|  | 
|  | 1 | + | 
|  | 2 | +class Node: | 
|  | 3 | + | 
|  | 4 | + def __init__(self, data=0, left=None, right=None): | 
|  | 5 | + """ | 
|  | 6 | + Initializes the node. | 
|  | 7 | + """ | 
|  | 8 | + | 
|  | 9 | + self.data = data | 
|  | 10 | + self.left = left | 
|  | 11 | + self.right = right | 
|  | 12 | + | 
|  | 13 | +def isSorted(arr): | 
|  | 14 | + """ | 
|  | 15 | + Returns whether the array is sorted or not. | 
|  | 16 | +
 | 
|  | 17 | + Time Complexity: O(n) | 
|  | 18 | + """ | 
|  | 19 | + | 
|  | 20 | + for i in range(1, len(arr)): | 
|  | 21 | + | 
|  | 22 | + if arr[i] < arr[i - 1]: | 
|  | 23 | + return False | 
|  | 24 | + | 
|  | 25 | + return True | 
|  | 26 | + | 
|  | 27 | +def build_balanced_tree(arr): | 
|  | 28 | + """ | 
|  | 29 | + Builds the balanced binary search tree from the sorted array. | 
|  | 30 | +
 | 
|  | 31 | + Time Complexity: O(n) | 
|  | 32 | + """ | 
|  | 33 | + | 
|  | 34 | + # Return None/NULL when there are no elements in the array.. | 
|  | 35 | + if arr == None or len(arr) == 0: | 
|  | 36 | + return None | 
|  | 37 | + | 
|  | 38 | + # Get the median index of the list. | 
|  | 39 | + mid = len(arr) // 2 | 
|  | 40 | + | 
|  | 41 | + # Set the node. | 
|  | 42 | + root = Node(arr[mid]) | 
|  | 43 | + | 
|  | 44 | + # Get the left node. | 
|  | 45 | + root.left = build_balanced_tree(arr[:mid]) | 
|  | 46 | + | 
|  | 47 | + # Get the right node. | 
|  | 48 | + root.right = build_balanced_tree(arr[mid + 1:]) | 
|  | 49 | + | 
|  | 50 | + return root | 
|  | 51 | + | 
|  | 52 | +def inorder_traversal(root): | 
|  | 53 | + """ | 
|  | 54 | + Traverses the binary tree with left-root-right order. | 
|  | 55 | +
 | 
|  | 56 | + Time Complexity: O(n) | 
|  | 57 | + """ | 
|  | 58 | + | 
|  | 59 | + if not root: | 
|  | 60 | + return | 
|  | 61 | + | 
|  | 62 | + inorder_traversal(root.left) | 
|  | 63 | + print(root.data, end=' ') | 
|  | 64 | + inorder_traversal(root.right) | 
|  | 65 | + | 
|  | 66 | +def preorder_traversal(root): | 
|  | 67 | + """ | 
|  | 68 | + Traverses the binary tree with root-left-right order. | 
|  | 69 | +
 | 
|  | 70 | + Time Complexity: O(n) | 
|  | 71 | + """ | 
|  | 72 | + | 
|  | 73 | + if not root: | 
|  | 74 | + return | 
|  | 75 | + | 
|  | 76 | + print(root.data, end=' ') | 
|  | 77 | + preorder_traversal(root.left) | 
|  | 78 | + preorder_traversal(root.right) | 
|  | 79 | + | 
|  | 80 | +def postorder_traversal(root): | 
|  | 81 | + """ | 
|  | 82 | + Traverses the binary tree with left-right-root order. | 
|  | 83 | +
 | 
|  | 84 | + Time Complexity: O(n) | 
|  | 85 | + """ | 
|  | 86 | + | 
|  | 87 | + if not root: | 
|  | 88 | + return | 
|  | 89 | + | 
|  | 90 | + postorder_traversal(root.left) | 
|  | 91 | + postorder_traversal(root.right) | 
|  | 92 | + print(root.data, end=' ') | 
|  | 93 | + | 
|  | 94 | +if __name__ == '__main__': | 
|  | 95 | + | 
|  | 96 | + # Initialize the array. | 
|  | 97 | + # arr = [1, 8, 3, 3, 6, 0, 2] | 
|  | 98 | + arr = [1, 2, 3, 4, 5, 8, 9, 10, 11] | 
|  | 99 | + | 
|  | 100 | + # Get the length of the list. | 
|  | 101 | + n = len(arr) | 
|  | 102 | + | 
|  | 103 | + # Sort the array if not sorted. | 
|  | 104 | + if not isSorted(arr): | 
|  | 105 | + print('List not sorted. Sorting list...') | 
|  | 106 | + | 
|  | 107 | + # Sort the array. | 
|  | 108 | + arr.sort() | 
|  | 109 | + else: | 
|  | 110 | + print('List is already sorted...') | 
|  | 111 | + | 
|  | 112 | + print('Building the balanced binary search tree...') | 
|  | 113 | + root = build_balanced_tree(arr) | 
|  | 114 | + | 
|  | 115 | + # Prints the In-order traversal of the tree. | 
|  | 116 | + print('In-order traversal:') | 
|  | 117 | + inorder_traversal(root) | 
|  | 118 | + print('') | 
|  | 119 | + | 
|  | 120 | + # Prints the Pre-order traversal of the tree. | 
|  | 121 | + print('Pre-order traversal:') | 
|  | 122 | + preorder_traversal(root) | 
|  | 123 | + print('') | 
|  | 124 | + | 
|  | 125 | + # Prints the Post-order traversal of the tree. | 
|  | 126 | + print('Post-order traversal:') | 
|  | 127 | + postorder_traversal(root) | 
|  | 128 | + print('') | 
|  | 129 | + | 
|  | 130 | +# Result: | 
|  | 131 | +# --- | 
|  | 132 | +# | 
|  | 133 | +# List is already sorted... | 
|  | 134 | +# Building the balanced binary search tree... | 
|  | 135 | +# In-order traversal: | 
|  | 136 | +# 1 2 3 4 5 8 9 10 11 | 
|  | 137 | +# Pre-order traversal: | 
|  | 138 | +# 5 3 2 1 4 10 9 8 11 | 
|  | 139 | +# Post-order traversal: | 
|  | 140 | +# 1 2 4 3 8 9 11 10 5 | 
0 commit comments