-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.py
196 lines (167 loc) · 4.82 KB
/
tree.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
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
187
188
189
190
191
192
193
194
195
class BinTree:
def __init__(self, rootVal=None):
self.rootVal = rootVal
self.leftChild = None
self.rightChild = None
self.rheight = 0
self.lheight = 0
def getLeftChild(self):
return self.leftChild
def getRightChild(self):
return self.rightChild
def getRootVal(self):
return self.rootVal
def setRootVal(self, val):
self.rootVal = val
def height(self):
if self.leftChild is None and self.rightChild is None:
return 0
else:
if self.leftChild:
self.lheight = self.leftChild.height() + 1
if self.rightChild:
self.rheight = self.rightChild.height() + 1
return max([self.lheight, self.rheight])
def insertLeft(self, item):
if self.leftChild is None:
self.leftChild = BinTree(item)
else:
t = BinTree(item)
t.leftChild = self.leftChild
self.leftChild = t
def insertRight(self, item):
if self.rightChild is None:
self.rightChild = BinTree(item)
else:
t = BinTree(item)
t.rightChild = self.rightChild
self.rightChild = t
def spinLeaves(self):
if self.leftChild and self.rightChild:
if self.rightChild.getRootVal() != 'X' or self.leftChild.getRootVal() != 'X':
tval = self.rightChild.getRootVal()
self.rightChild.setRootVal(self.leftChild.getRootVal())
self.leftChild.setRootVal(tval)
if self.leftChild and not self.rightChild:
self.rightChild = self.leftChild
self.leftChild = BinTree('X')
if self.rightChild and not self.leftChild:
self.leftChild = self.rightChild
self.rightChild = BinTree('X')
if self.leftChild:
self.leftChild.spinLeaves()
if self.rightChild:
self.rightChild.spinLeaves()
if self.leftChild:
if self.leftChild.getRootVal() == 'X':
self.leftChild = None
if self.rightChild:
if self.rightChild.getRootVal() == 'X':
self.rightChild = None
def preorder(self, visited=[]):
if self.getRootVal() != 'root':
visited.append(self.getRootVal())
if self.leftChild:
self.leftChild.preorder()
if self.rightChild:
self.rightChild.preorder()
return visited
def inorder(self, visited=[]):
if self.leftChild:
self.leftChild.inorder()
if self.getRootVal() != 'root':
visited.append(self.getRootVal())
if self.rightChild:
self.rightChild.inorder()
return visited
def postorder(self, visited=[]):
if self.leftChild:
self.leftChild.postorder()
if self.rightChild:
self.rightChild.postorder()
if self.getRootVal() != 'root':
visited.append(self.getRootVal())
return visited
"""
T1:
0
/ \
/ \
1 2
/ \ / \
3 4 5 6
T2: T1 Reflective
0
/ \
/ \
2 1
/ \ / \
6 5 4 3
"""
import random
th = 2
c = th + 1
num_items = 2**(c) - 1
rv1 = list(range(num_items))
rv2 = list(range(num_items))
#random.shuffle(rv1)
#random.shuffle(rv2)
tree1 = BinTree(rv1[0])
tree2 = BinTree(rv2[0])
print("\nIN TREE T1:\n")
value_index_counter = 1
stack = []
for i, item in enumerate(rv1[1:]):
if i == 0:
next_roots = ['leftChild', 'rightChild']
print(value_index_counter, item, next_roots[0])
print(value_index_counter+1, rv1[value_index_counter+1], next_roots[1])
exec("tree1.leftChild = BinTree(%d)"%item)
exec("tree1.rightChild = BinTree(%d)"%(rv1[value_index_counter+1]))
stack.extend(next_roots)
value_index_counter += 2
else:
next_root = stack.pop(0)
next_roots = [next_root+'.'+'leftChild', next_root+'.'+'rightChild' ]
for j, root in enumerate(next_roots):
if value_index_counter >= len(rv1):
break
print(value_index_counter, rv1[value_index_counter], root)
exec("tree1.%s = BinTree(%d)"%(root, rv1[value_index_counter]))
value_index_counter += 1
stack.extend(next_roots)
print("\nMIRROR TREE T2:\n")
value_index_counter = 1
stack = []
for i, item in enumerate(rv2[1:]):
if i == 0:
next_roots = ['rightChild', 'leftChild']
print(value_index_counter, item, next_roots[0])
print(value_index_counter+1, rv2[value_index_counter+1], next_roots[1])
exec("tree2.rightChild = BinTree(%d)"%(rv2[value_index_counter]))
exec("tree2.leftChild = BinTree(%d)"%(rv2[value_index_counter+1]))
stack.extend(next_roots)
value_index_counter += 2
else:
next_root = stack.pop(0)
next_roots = [next_root+'.'+'rightChild', next_root+'.'+'leftChild' ]
for j, root in enumerate(next_roots):
if value_index_counter >= len(rv2):
break
print(value_index_counter, rv2[value_index_counter], root)
exec("tree2.%s = BinTree(%d)"%(root, rv2[value_index_counter]))
value_index_counter += 1
stack.extend(next_roots)
print("\nT1 height: %d"% tree1.height())
print("\nT2 height: %d"% tree2.height())
t1_pre = tree1.preorder()
t2_post = tree2.postorder()
print("\nT1 PRE-ORDER:")
print(t1_pre)
print("\nT2 POST-ORDER + REVERSE:")
print(t2_post[::-1])
if t2_post[::-1] == t1_pre:
print("\nMIRROR: TRUE")
else:
print("\nMIRROR: FALSE")
print("\nNUMBER OF NODES: %d"%num_items)