-
Notifications
You must be signed in to change notification settings - Fork 0
/
Traversal_Iteratively.py
165 lines (137 loc) · 4.25 KB
/
Traversal_Iteratively.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
"""
This program builds the BST &
prints it using InOrder traversal iteratively
This program uses Stack implemented as Linkedlist
"""
# class to represent node
class StackNode:
# constructor to initialize a node
def __init__(self,data,purpose):
self.data = data
self.purpose = purpose
self.next = None #python uses None instead of NULL
class Stack_preNode:
def __init__(self,data):
self.data = data
self.next = None
class Stack:
# constructor to initialize root of linkedlist
def __init__(self):
self.root = None
def isEmpty(self):
if self.root is None:
return True
else:
return False
def push(self,data,purpose):
newNode = StackNode(data,purpose)
newNode.next = self.root
self.root = newNode
#print "(%d,%d) pushed to stack" %(data)%(purpose)
def pop(self):
if self.isEmpty():
return float("-inf")
temp = self.root
self.root = self.root.next
popped = temp.data
purpose = temp.purpose
#print "(%d,%d) poped from stack"%(popped)%(purpose)
return (popped,purpose)
class PreStack:
def __init__(self):
self.root = None
def isEmpty(self):
if self.root is None:
return True
else:
return False
def push(self,data):
newNode = Stack_preNode(data)
newNode.next = self.root
self.root = newNode
def pop(self):
if self.isEmpty():
return float("-inf")
temp = self.root
self.root = self.root.next
popped = temp.data
return popped
class Node:
def __init__(self,data):
self.data = data
self.left = None
self.right = None
def Insert(root,node):
if root is None:
root = node
else:
if root.data < node.data:
if root.right is None:
root.right = node
else:
root.right.Insert(node)
else:
if root.left is None:
root.left = node
else:
root.left.Insert(node)
def InOrder(self,pos): # pos gets the root
new = []
S = Stack()
S.push(pos,1)
while(not S.isEmpty()):
(ptr,purpose) = S.pop()
if ptr is not None:
if purpose:
S.push(ptr,0)
S.push(ptr.left,1)
else:# if not purpose
new.append(ptr.data)
S.push(ptr.right,1)
return new
def PostOrder(self,pos):
new = []
S= Stack()
S.push(pos,1)
while not S.isEmpty():
(ptr,purpose) = S.pop()
if ptr is not None:
if purpose:
S.push(ptr,0)
S.push(ptr.right,1)
S.push(ptr.left,1)
else:
new.append(ptr.data)
return new
def PreOrder(self,pos):
new = []
S = PreStack()
S.push(pos)
while not S.isEmpty():
ptr = S.pop()
if ptr is not None:
new.append(ptr.data)
S.push(ptr.right) # right child is pushed first as we need left child first
S.push(ptr.left)
return new
try:
print "\nEnter the inputs to create a BST"
arr = [int(x) for x in raw_input().split()]
print "\nINPUT -"
print " ".join(map(str,arr))
root = Node(arr[0])
for i in range(1,len(arr)):
node = Node(arr[i])
root.Insert(node)
print "\nInorder Traversal (iteratively) -"
inorder = root.InOrder(root)
print " ".join(map(str,inorder))
print "\nPostorder Traversal (iteratively) -"
postorder = root.PostOrder(root)
print " ".join(map(str,postorder))
print "\nPreorder Traversal (iteratively) -"
preorder = root.PreOrder(root)
print " ".join(map(str,preorder))
print "\n\n"
except ValueError:
print "\nERROR: Invalid Input\n"