-
Notifications
You must be signed in to change notification settings - Fork 4
/
balanceBST.py
50 lines (41 loc) · 1.07 KB
/
balanceBST.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from Tree.commons import Node, print_tree, insert
from Tree.buildBSTFromSortedArray import build_bst
def balance_tree(root):
inorder_arr = []
inorder(root, inorder_arr)
return build_bst(inorder_arr, 0, len(inorder_arr)-1)
def inorder(root, inorder_arr):
if root is not None:
inorder(root.left, inorder_arr)
inorder_arr.append(root.key)
inorder(root.right, inorder_arr)
# Driver program to test above function
if __name__ == "__main__":
""" Unbalanced BST
50
/ \
30 70
\
80
\
90
"""
root = None
root = insert(root, 50)
insert(root, 30)
insert(root, 70)
insert(root, 80)
insert(root, 90)
print ("\n ---- Unbalanced BST ----\n")
print_tree(root)
print ("\n ---- Balanced BST ----\n")
balanced_root = balance_tree(root)
print_tree(balanced_root)
"""
Balanced BST ->
70
/ \
30 80
\ \
50 90
"""