forked from girishgupta211-zz/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bst.py
98 lines (77 loc) · 1.88 KB
/
bst.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Python program to demonstrate insert operation in binary search tree
# A utility class that represents an individual node in a BST
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
# A utility function to insert a new node with the given key
def insert(root, key):
if root is None:
root = Node(key)
else:
if key > root.val:
root.right = insert(root.right, key)
else:
root.left = insert(root.left, key)
return root
def find_less_eq_n(root, key):
if root is None:
return -1
if root.val == key:
return key
if key > root.val:
k = find_less_eq_n(root.right, key)
if k == -1:
return root.val
else:
return k
elif key < root.val:
return find_less_eq_n(root.left, key)
# A utility function to do inorder tree traversal
def inorder(root):
if root:
inorder(root.left)
print(root.val)
inorder(root.right)
# A utility function to do inorder tree traversal
def preorder(root):
if root:
print(root.val)
inorder(root.left)
inorder(root.right)
# A utility function to do inorder tree traversal
def postorder(root):
if root:
inorder(root.left)
inorder(root.right)
print(root.val)
# Driver program to test the above functions
# Let us create the following BST
# 50
# / \
# 30 70
# / \ / \
# 20 40 60 80
# r = Node(50)
# insert(r, Node(30))
# insert(r, Node(20))
# insert(r, Node(40))
# insert(r, Node(70))
# insert(r, Node(60))
# insert(r, Node(80))
#
# # Print inoder traversal of the BST
# inorder(r)
root = None
root = insert(root, 25)
insert(root, 2)
insert(root, 1)
insert(root, 3)
insert(root, 12)
insert(root, 9)
insert(root, 21)
insert(root, 19)
# inorder(root)
result = find_less_eq_n(root, 4)
print(result)