-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.py
More file actions
186 lines (147 loc) · 4.21 KB
/
tree.py
File metadata and controls
186 lines (147 loc) · 4.21 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
INT_MIN = 4294967296
INT_MAX = -4294967296
class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
def insert(self, val):
if val <= self.data:
if self.left is None:
self.left = Node(val)
else:
self.left.insert(val)
else:
if self.right is None:
self.right = Node(val)
else:
self.right.insert(val)
def contains(self, val):
if val == self.data:
return True
elif val < self.data:
if self.left is None:
return False
else:
return self.left.contains(val)
else:
if self.right is None:
return False
else:
return self.right.contains(val)
def print_in_order(self):
if self.left:
self.left.print_in_order()
print(self.data)
if self.right:
self.right.print_in_order()
def print_in_pre_order(self):
print(self.data)
if self.left:
self.left.print_in_pre_order()
if self.right:
self.right.print_in_order()
# def print_in_post_order(self):
# if self.left:
# self.left.print_in_post_order()
def tree_max_height(tree):
if tree is None:
return 0
else:
left = tree_max_height(tree.left)
right = tree_max_height(tree.left)
return 1 + (left if left >= right else right)
def equal_trees(tree1, tree2):
if tree1 is None and tree2 is None:
return True
elif type(tree1) != type(tree2):
return False
elif tree1.data != tree2.data:
return False
else:
return equal_trees(tree1.left, tree2.left) and equal_trees(tree1.right, tree2.right)
def find_largest(tree):
if tree is None:
return 0
else:
left = find_largest(tree.left)
right = find_largest(tree.right)
maximum = left if left > right else right
maximum = tree.data if tree.data > maximum else maximum
return maximum
def mobile(tree1, tree2):
if tree1 is None or tree2 is None:
return tree1 == tree2
elif tree1.data != tree2.data:
return 0
else:
print("recursion")
return (mobile(tree1.left, tree2.left) and mobile(tree1.right, tree2.right)) or \
(mobile(tree1.left, tree2.right) and mobile(tree1.right, tree2.left))
def check_binary_search_tree_(node):
return (isBSTUtil(node, INT_MIN, INT_MAX))
# Return true if the given tree is a BST and its values
# >= min and <= max
def isBSTUtil(node, mini, maxi):
# An empty tree is BST
if node is None:
return True
# False if this node violates min/max constraint
if node.data < mini or node.data > maxi:
return False
# Otherwise check the subtrees recursively
# tightening the min or max constraint
return (isBSTUtil(node.left, mini, node.data - 1) and
isBSTUtil(node.right, node.data + 1, maxi))
tree = Node(4)
tree.insert(2)
tree.insert(5)
tree.insert(7)
tree.insert(1)
tree.insert(6)
tree.insert(3)
test = Node(21)
test.insert(2)
test.insert(7)
test.insert(11)
test.insert(0)
print(test.data)
print(test.left.data)
print(test.left.left.data)
print(test.left.right.data)
print(test.left.right.right.data)
tree2 = Node(4)
tree2.insert(2)
tree2.insert(5)
tree2.insert(7)
tree2.insert(1)
tree2.insert(6)
tree2.insert(3)
tree2.insert(8)
mirror = Node(8)
mirror.insert(4)
mirror.insert(12)
mirror2 = Node(8)
mirror2.insert(12)
mirror2.insert(4)
mirror3 = Node(8)
mirror3.left = Node(4)
mirror3.right = Node(12)
mirror4 = Node(8)
mirror4.left = Node(12)
mirror4.left.left = Node(24)
print(check_binary_search_tree_(tree))
print(check_binary_search_tree_(tree2))
print(check_binary_search_tree_(mirror))
print(check_binary_search_tree_(mirror2))
print(check_binary_search_tree_(mirror3))
print(check_binary_search_tree_(mirror4))
# print(mobile(mirror3, mirror4))
# print(equal_trees(tree, tree2))
# print(find_largest(tree))
# print(tree.contains(15))
# print(tree.data)
#
# tree.print_in_order()
# tree.print_in_pre_order()
# print(tree_max_height(tree))