-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.py
More file actions
41 lines (29 loc) · 723 Bytes
/
bst.py
File metadata and controls
41 lines (29 loc) · 723 Bytes
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
#Binary Search tree
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
def insert(root, key):
if root is None:
return Node(key)
else:
if root.val < key:
root.right = insert(root.right, key)
else:
root.left = insert(root.left, key)
return root
def inorder(root, res):
if root:
inorder(root.left, res)
res.append(root.val)
inorder(root.right, res)
def tree_sort(arr):
res = []
root = None
for i in arr:
root = insert(root, i)
inorder(root, res)
return res
arr = [64, 34, 25, 12, 22, 11, 90]
print("Sorted array is:", tree_sort(arr))