-
Notifications
You must be signed in to change notification settings - Fork 0
/
7-Tree Traversal.py
194 lines (156 loc) · 4.02 KB
/
7-Tree Traversal.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
"""
Tree Traversal Implementation
Functional Type--
"""
__author__ = 'Metehan Özcan'
class Node:
def __init__(self, data=None, right=None, left=None):
self.data = data
self.right = right
self.left = left
def insert(root, data):
# Return a new node if the tree is empty
if root is None:
return Node(data)
# Traverse to the right place and insert the node
if data < root.data:
root.left = insert(root.left, data)
else:
root.right = insert(root.right, data)
return root
def findmin(root):
current = root
while current.left:
current = current.left
return current
def delete(root, data):
if not root:
return None
elif data < root.data: #* If
root.left = delete(root.left, data)
elif data > root.data:
root.right = delete(root.right, data)
else: # * Found
if not root.right and not root.left: # * No child case. Root is None
root = None
elif not root.left: # * One child case
root = root.right
elif not root.right: # * One child case
root = root.left
else: # * Both child case
minval = findmin(root.right)
root.data = minval.data
root.right = delete(root.right, minval.data)
return root
def inorder(root):
if root:
inorder(root.left)
print(root.data, end='->')
inorder(root.right)
return 'END'
def inorder_iter(root):
s = []
curr = root
while s or curr:
if curr:
s.append(curr)
curr = curr.left
else:
curr = s.pop()
print(curr.data, end='->')
curr = curr.right
return 'END'
def preorder(root):
if root:
print(root.data, end='->')
preorder(root.left)
preorder(root.right)
return 'END'
def preorder_iter(root):
s = []
s.append(root)
while s:
node = s.pop()
print(node.data, end='->')
if node.right:
s.append(node.right)
if node.left:
s.append(node.left)
return 'END'
def postorder(root):
if root:
postorder(root.left)
postorder(root.right)
print(root.data, end='->')
return 'End'
def postorder_iter(root):
if root is None:
return
s1 = []
s2 = []
s1.append(root)
while s1:
node = s1.pop()
s2.append(node)
if node.left:
s1.append(node.left)
if node.right:
s1.append(node.right)
while s2:
node = s2.pop()
print(node.data, end='->')
return 'End'
def levelorder_traversal(root):
q = [root]
while len(q) > 0:
root = q.pop(0)
if root is not None:
print(root.data, end='->')
q.append(root.left)
q.append(root.right)
return 'End'
def reverse_levelorder_traversal(root):
q = [root]
s = []
while len(q) > 0:
root = q.pop(0)
if root is not None:
s.append(root)
q.append(root.right)
q.append(root.left)
while s:
node = s.pop()
print(node.data, end='->')
return 'End'
root = None
root = insert(root, 5)
root = insert(root, 2)
root = insert(root, 3)
root = insert(root, 4)
root = insert(root, 7)
root = insert(root, 6)
root = delete(root, 8)
# root = Node(2)
# root.left = Node(1)
# root.right = Node(3)
# root.left.left = Node(4)
# root.left.right = Node(5)
# root.right.left = Node(6)
# root.right.right = Node(7)
# root.left.left.left = Node(8)
print('inorder traversal')
inorder(root)
print('\ninorder iter traversal')
print(inorder_iter(root))
print('preorder traversal')
print(preorder(root))
print('preorder iter traversal')
print(preorder_iter(root))
print('postorder traversal')
print(postorder(root))
print('postorder iter traversal')
print(postorder_iter(root))
print('levelorder traversal')
print(levelorder_traversal(root))
print('reverse level order traversal')
print(reverse_levelorder_traversal(root))