-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.py
379 lines (312 loc) · 9.61 KB
/
LinkedList.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
'''
Copyright: Reese Wells, 2017
'''
class LinkedQueue:
'''
A queue implementation using LinkedLists.
'''
_firstNode = None
_items = 0
def __init__(self):
'''
Creates an empty queue.
'''
self._firstNode = None
def enqueue(self, newData):
'''
Adds an item to the back of the queue.
'''
newNode = self._Node(newData)
if self._firstNode is None:
self._firstNode = newNode
else:
currentNode = self._firstNode
while currentNode._getNextNode() is not None:
currentNode = currentNode._getNextNode()
currentNode._setNextNode(newNode)
self._items = self._items + 1
def peek(self):
'''
Returns the item at the front of the queue but does not dequeue it.
*Returns None if the queue is empty
'''
result = None
if self._firstNode is not None:
result = self._firstNode._data
return result
def size(self):
'''
Returns the number of items in the queue.
'''
return self._items
def dequeue(self):
'''
Removes the item at the front of the queue and returns it.
*Returns None if the queue is empty.
'''
result = None
if self._firstNode is not None:
result = self._firstNode._getData()
self._firstNode = self._firstNode._getNextNode()
self._items = self._items - 1
return result
def toList(self):
'''
Returns a list containing all the items in the queue.
Returns an empty list if the queue is empty.
'''
currentNode = self._firstNode
result = []
while currentNode is not None:
result.append(currentNode._getData())
currentNode = currentNode._getNextNode()
return result;
def toQueue(listicle):
'''
Turns a list into a LinkedQueue
'''
returnQueue = LinkedQueue()
for item in listicle:
returnQueue.enqueue(item)
return returnQueue
def clear(self):
'''
Removes all objects from the queue.
'''
self._firstNode = None;
self._items = 0
class _Node:
_nextNode = None
_data = None
def __init__(self, dataInit = None, nextNodeInit = None):
if nextNodeInit is not None:
self._nextNode = nextNodeInit
if dataInit is not None:
self._data = dataInit
'''
returns the Node data
'''
def _getData(self):
return self._data
'''
returns the next Node
'''
def _getNextNode(self):
return self._nextNode
'''
sets the data in the specified node
'''
def _setData(self, newData = None):
if newData is None:
print("newData cannot be null!")
else:
self.data = _newData
'''
sets the next node of the specified node
'''
def _setNextNode(self, newNode = None):
self._nextNode = newNode;
class LinkedStack:
_top = None
def add(self, newData):
'''
adds an element to the top of the stack
'''
newNode = self._Node(newData, self._top)
self._top = newNode
def remove(self):
'''
removes the top element from the stack
'''
if self._top is not None:
returnData = self._top._getData()
self._top = self._top._getNextNode()
return returnData
else:
return None
def peek(self):
'''
returns the top element without removing it
'''
if self._top is not None:
return self._top._getData()
def isEmpty(self):
'''
returns true if the stack is empty, false if not
'''
return self._top is None
def clear(self):
'''
clears all elements from the stack
'''
self._top = None
class _Node:
_nextNode = None
_data = None
def __init__(self, dataInit = None, nextNodeInit = None):
if nextNodeInit is not None:
self._nextNode = nextNodeInit
if dataInit is not None:
self._data = dataInit
'''
returns the Node data
'''
def _getData(self):
return self._data
'''
returns the next Node
'''
def _getNextNode(self):
return self._nextNode
'''
sets the data in the specified node
'''
def _setData(self, newData = None):
if newData is None:
print("newData cannot be null!")
else:
self.data = _newData
'''
sets the next node of the specified node
'''
def _setNextNode(self, newNode = None):
self._nextNode = newNode;
class LinkedTree:
'''
A tree implementation using LinkedLists.
'''
_leftTree = None
_rightTree = None
_data = None
def __init__(self, newData = None):
'''
Creates a new empty tree.
'''
self._leftTree = None
self._rightTree = None
self._data = newData
def setRoot(self, newData):
'''
Sets the root of the given tree
rtype : boolean
returns False if unsuccessful
returns True if successful
'''
self._data = newData
def setLeftChild(self, newData):
'''
Sets the left child of the node
rtype : boolean
returns False if unsuccessful
returns True if successful
'''
if self._leftTree is None:
self._leftTree = LinkedTree()
self._leftTree._data = newData
else:
self._leftTree._data = newData
def setRightChild(self, newData):
'''
Sets the right child of the node
rtype : boolean
returns False if unsuccessful
returns True if successful
'''
if self._rightTree is None:
self._rightTree = LinkedTree()
self._rightTree._data = newData
else:
self._rightTree._data = newData
def getLeftChild(self):
'''
Returns the left child, a LinkedTree
rtype: LinkedTree
returns None if no child
'''
return self._leftTree
def getRightChild(self):
'''
Returns the Right child, a LinkedTree
rtype: LinkedTree
returns None if no child
'''
return self._rightTree
def getRoot(self):
'''
returns the root of the given tree
'''
return self._data
def clear(self):
'''
clears the tree of all children and data
'''
self._leftTree = None
self._rightTree = None
self._data = None
def getHeight(self):
'''
returns the height of the tree
'''
leftHeight = 0
rightHeight = 0
if self._leftTree is not None:
leftHeight = self._leftTree.getHeight()
if self._rightTree is not None:
rightHeight = self._rightTree.getHeight()
return 1 + max(rightHeight, leftHeight)
def printPreorder(self, depth = 0):
'''
prints a formatted version of the tree, preorder
'''
append = str(depth) + "_"
for i in range(0, depth):
append = append + "_"
print(append + str(self._data))
if self.getLeftChild() is not None:
self.getLeftChild().printPreorder(depth + 1)
if self.getRightChild() is not None:
self.getRightChild().printPreorder(depth + 1)
def printPostorder(self, depth = 0):
'''
prints a formatted version of the tree, postorder
'''
append = str(depth) + "_"
for i in range(0, depth):
append = append + "_"
if self.getLeftChild() is not None:
self.getLeftChild().printPostorder(depth + 1)
if self.getRightChild() is not None:
self.getRightChild().printPostorder(depth + 1)
print(append + str(self._data))
def printInorder(self, depth = 0):
'''
prints a formatted version of the tree, inorder
'''
append = str(depth) + "_"
for i in range(0, depth):
append = append + "_"
if self.getLeftChild() is not None:
self.getLeftChild().printInorder(depth + 1)
print(append + str(self._data))
if self.getRightChild() is not None:
self.getRightChild().printInorder(depth + 1)
def printLevelorder(self, depth = 0):
'''
prints a formatted version of the tree, levelorder
'''
append = str(depth) + "_"
for i in range(0, self.getHeight() + 1):
self._printGivenLevel(i,i)
def _printGivenLevel(self, depth, curDepth):
if self is None:
return;
append = str(curDepth) + ""
for i in range(0,curDepth):
append = append + "_"
if depth == 1:
print(append + str(self.getRoot()))
elif depth > 1:
if self.getLeftChild() is not None:
self.getLeftChild()._printGivenLevel(depth - 1, curDepth)
if self.getRightChild() is not None:
self.getRightChild()._printGivenLevel(depth - 1, curDepth)