-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataStructuresSoln.py
More file actions
189 lines (177 loc) · 4.05 KB
/
dataStructuresSoln.py
File metadata and controls
189 lines (177 loc) · 4.05 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
187
188
189
"""
You are given the following classes
"""
class Node():
def __init__(self, value=None, next=None):
self.value = value
self.next = next
class TreeNode:
def __init__(self, value=None, left=None, right=None):
self.value = value
self.left = left
self.right = right
"""
1) Implement a stack with push and pop functions
"""
class Stack():
def __init__(self, top=None):
self.top = top
def push(self, elem):
newLL = Node(elem, self.top)
self.top = newLL
def pop(self):
if self.top != None:
topElem = self.top.value
self.top = self.top.next
return topElem
else:
return None
#tests
s = Stack()
s.push(4)
s.push(5)
assert s.pop() == 5
s.push(6)
assert s.pop() == 6
assert s.pop() == 4
assert s.pop() == None
"""
2) Implement a queue with queue and dequeue functions
"""
class Queue():
# First: newest element
def __init__(self, first=None, last=None):
self.first = first
self.last = last
def queue(self, elem):
newElem = Node(elem)
if self.first == None:
self.last = newElem
else:
self.first.next = newElem
self.first = newElem
def dequeue(self):
if self.last == None:
return None
else:
ret = self.last.value
self.last = self.last.next
return ret
#tests:
q = Queue()
q.queue(4)
q.queue(5)
assert q.dequeue() == 4
q.queue(6)
assert q.dequeue() == 5
assert q.dequeue() == 6
assert q.dequeue() == None
"""
3) Implement a Binary Min Heap using an array
"""
class Heap: #minHeap
def __init__(self, size=1):
self.size = size
self.ar = [-1 for i in range(size)]
#build up
def createFromArray(self, ar):
self.size = len(ar)
#loop through indexes backwards starting from midpoint
for i in range(len(ar)/2, -1 , -1):
index = i
#coninually swap down left side of heap until property statisfied
while(index < self.size):
print ar, index, 'l'
left = index*2 + 1
right = index*2 + 2
if left < self.size and ar[left] < ar[index]:
#swap
temp = ar[index]
ar[index] = ar[left]
ar[left] = temp
#for every swap left, make sure right still holds
indexRight = index
while(indexRight < self.size):
print ar, index, 'r - in'
right = indexRight*2 + 2
if right < self.size and ar[right] < ar[indexRight]:
#swap
temp = ar[indexRight]
ar[indexRight] = ar[right]
ar[right] = temp
indexRight = right
else:
break
index = left
elif right < self.size and ar[right] < ar[index]:
print ar, index, 'r - out'
#swap
temp = ar[index]
ar[index] = ar[right]
ar[right] = temp
index = right
else:
break
self.ar = ar
def push(self, val):
#insert into first instance of -1
newPos = -1
for i in range(self.size):
if self.ar[i] == -1:
newPos = i
break
#double length of heap
if newPos == -1:
newPos = self.size
self.ar.extend([-1 for i in range(self.size)])
self.size = self.size * 2
#insert
self.ar[newPos] = val
#swap backwards until fit
curPos = newPos
while(curPos > 0):
if curPos%2 == 0:
parent = (curPos-2)/2
else:
parent = (curPos-1)/2
if self.ar[curPos] > self.ar[parent]:
break
#swap
self.ar[curPos] = self.ar[parent]
self.ar[parent] = val
curPos = parent
#Always pop root
def pop(self):
index = 0
#pop root and swap down heap
while index*2+1 < self.size:
#right is too big
if index*2+2 >= self.size:
#left DNE
if self.ar[index*2+1] == -1:
break
#swap left
self.ar[index] = self.ar[index*2 + 1]
index = index*2+1
else:
#left does not exist
if self.ar[index*2+1] == -1:
#right DNE
if self.ar[index*2+2] == -1:
break
#swap right
self.ar[index] = self.ar[index*2 + 2]
index=index*2+2
#right DNE, swap left
elif self.ar[index*2+2] == -1:
self.ar[index] = self.ar[index*2 + 1]
index=index*2+1
#both exist
else:
if self.ar[index*2 + 1] < self.ar[index*2 + 2]:
self.ar[index] = self.ar[index*2 + 1]
index = index*2 + 1
else:
self.ar[index] = self.ar[index*2 + 2]
index = index*2 + 2
self.ar[index] = -1