-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortedsinglylinkedlist.py
217 lines (187 loc) · 5.66 KB
/
sortedsinglylinkedlist.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
class SortedSinglyLinkedList:
def __init__(self, array=None):
self.head = None
self.last_node = None
self.length = 0
if array:
for data in array:
index = self.index_of(data)
if index is None:
self.insert(data)
else:
self.update(index)
self.length = len(self)
def insert(self, data):
"""Inserts node at correct (sorted) position
Params: data - data to be inserted
Node -> ()
"""
node = Node(data)
if not self.head:
node.next_node = self.last_node
self.head = node
elif self.head and not self.last_node:
if self.head.data < node.data:
self.head.next_node = node
self.last_node = node
else:
self.last_node = self.head
node.next_node = self.last_node
self.head = node
else:
index = 0
current_node = self.head
added = False
while current_node:
if node.data < current_node.data:
self.insert_before(index, node)
added = True
break
current_node = current_node.next_node
index += 1
if not added:
self.append(node)
def insert_after(self, index, node):
"""Inserts node after current node at Index
Params: index - index to insert after
node - node to insert
int, Node -> ()
"""
current_node = self[index]
next_node = current_node.next_node
current_node.next_node = node
node.next_node = next_node
def insert_before(self, index, node):
"""Inserts node before current node at Index
Params: index - index to insert before
node - node to insert
int, Node -> ()
"""
current_node = self[index]
if current_node == self.head:
self.head = node
node.next_node = current_node
else:
previous_node = self[index-1]
previous_node.next_node = node
node.next_node = current_node
def append(self, node):
"""Appends node to end of self
Params: node - Node to append
Node -> ()
"""
# head and last node
if self.head and self.last_node:
self.last_node.next_node = node
self.last_node = node
# head but no last node
elif self.head:
self.head.next_node = node
self.last_node = node
# no head
else:
node.next_node = self.last_node
self.head = node
def getNode(self, data):
"""returns node given data
Params: data - key of node
str -> Node
"""
index = 0
node = self.head
while index < self.length:
if node.data == data:
return node
if node.next_node:
node = node.next_node
index += 1
def index_of(self, data):
"""Returns index of node
Params: data - key of node
str -> (str -> int)
"""
index = 0
node = self.head
while node:
if node.data == data:
return index
if node.next_node:
node = node.next_node
index += 1
def binary_search(self, data):
"""Binary search through tree
Params: data - key of Node
str -> int
"""
found = False
low = 0
high = self.length-1
if self.length == 0:
found = True
while not found:
midpoint = int((low+high)/2)
if data == self[midpoint].data:
found = True
return midpoint
elif data > self[midpoint].data:
low = midpoint + 1
else:
high = midpoint
def update(self, index):
"""Increases counter of node at index"""
self[index].count += 1
def remove(self, index):
"""Remove node at index
int -> ()
"""
current_node = self[index]
if current_node == self.head:
self.head = current_node.next_node
current_node = None
else:
previous_node = self[index-1]
previous_node.next_node = current_node.next_node
del current_node
def __iter__(self):
"""Required for iterable"""
node = self.head
while node:
yield node
node = node.next_node
def __getitem__(self, index):
"""Required for iterable; returns Node at index
int -> Node
"""
counter = 0
node = self.head
while counter < index:
if node.next_node:
node = node.next_node
counter += 1
else:
print("Error: Index out of bounds")
return None
return node
def __len__(self):
"""Define for len() to work
() -> int
"""
counter = 0
node = self.head
while node:
counter += 1
node = node.next_node
return counter
def __str__(self):
"""Returns self as formatted string
() -> str
"""
nodes = "["
for node in self:
nodes += "[{}, {}]".format(node.data, node.count)
return nodes + "]"
class Node:
def __init__(self, data=None):
self.data = data
self.count = 1
self.next_node = None