-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst-validator.py
53 lines (48 loc) · 1.46 KB
/
bst-validator.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
#DFS more space efficient than BFS as tree's breadth grows at each level
#Both DFS and BFS will take same time O(n)
class Node:
def __init__(self, k):
self.key = k
self.left = None
self.right = None
def tree_max(node):
if not node:
return float("-inf")
maxleft = tree_max(node.left)
maxright = tree_max(node.right)
print 'NODE:', node.key
print 'MAX_RIGHT:', maxright
print 'MAX_LEFT:', maxleft
print 'MAX:', max(node.key, maxleft, maxright)
return max(node.key, maxleft, maxright)
def tree_min(node):
if not node:
return float("inf")
minleft = tree_min(node.left)
minright = tree_min(node.right)
print 'NODE:', node.key
print 'MIN_RIGHT', minright
print 'MIN_LEFT', minleft
print 'MIN:', min(node.key, minleft, minright)
return min(node.key, minleft, minright)
def verify(node):
if not node:
return True
if (tree_max(node.left) <= node.key <= tree_min(node.right) and
verify(node.left) and verify(node.right)):
return True
else:
return False
root= Node(10)
root.left = Node(5)
root.right= Node(30)
root.left.right = Node(7)
root.left.left = Node(4)
root.left.left.left = Node(2)
root.left.left.right = Node(6)
print(verify(root)) # prints FALSE, since 6 is to the left of 5
# root = Node(10)
# root.right = Node(20)
# root.left = Node(5)
# root.left.right = Node(7)
# print(verify(root)) # prints False, since 15 is to the left of 10