Skip to content

Commit f938e2b

Browse files
add splay tree
1 parent 1d8355d commit f938e2b

11 files changed

+381
-99
lines changed

AVL Tree.py

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ def updateHeights(self, node):
3737
while True:
3838
if node is None:
3939
break
40-
node.height = max(self.heightNode(node.left),
41-
self.heightNode(node.right)) + 1
40+
node.height = (
41+
max(self.heightNode(node.left), self.heightNode(node.right)) + 1
42+
)
4243
node = node.parent
4344

4445
def balanceTree(self, node):
@@ -83,8 +84,9 @@ def returnNode(self, current):
8384
node2.left.parent = node1
8485
node2.left = current.left
8586
node1.parent = current.left
86-
node1.height = max(self.heightNode(node1.left),
87-
self.heightNode(node1.right)) + 1
87+
node1.height = (
88+
max(self.heightNode(node1.left), self.heightNode(node1.right)) + 1
89+
)
8890
node2.right = current.right
8991
if current.right != None:
9092
current.right.parent = node2
@@ -100,8 +102,7 @@ def rrRotation(self, root):
100102
"""Right rotation function"""
101103
node = self.copy(root.left)
102104
root.left = node.right
103-
root.height = max(self.heightNode(root.left),
104-
self.heightNode(root.right)) + 1
105+
root.height = max(self.heightNode(root.left), self.heightNode(root.right)) + 1
105106
node.right = self.copy(root)
106107
node.height = max(self.heightNode(node.left), root.height) + 1
107108
root.data = node.data
@@ -119,8 +120,7 @@ def llRotation(self, root):
119120
"""Left rotation function"""
120121
node = self.copy(root.right)
121122
root.right = node.left
122-
root.height = max(self.heightNode(root.left),
123-
self.heightNode(root.right)) + 1
123+
root.height = max(self.heightNode(root.left), self.heightNode(root.right)) + 1
124124
node.left = self.copy(root)
125125
node.height = max(self.heightNode(node.right), root.height) + 1
126126
root.data = node.data
@@ -306,7 +306,7 @@ def height(self, node):
306306
else:
307307
left_height = self.height(node.left)
308308
right_height = self.height(node.right)
309-
if (left_height > right_height):
309+
if left_height > right_height:
310310
return left_height + 1
311311
else:
312312
return right_height + 1
@@ -336,45 +336,44 @@ def printTree(self, node=None):
336336
def _printTree(self, node):
337337
"""Auxiliary function to print the Avl Tree"""
338338
if node.right is None and node.left is None: # No child
339-
line = '%s' % node.data
339+
line = "%s" % node.data
340340
width = len(line)
341341
height = 1
342342
middle = width // 2
343343
return [line], width, height, middle
344344

345345
if node.right is None: # Only left child
346346
lines, n, p, x = self._printTree(node.left)
347-
s = '%s' % node.data
347+
s = "%s" % node.data
348348
u = len(s)
349-
first_line = (x + 1) * ' ' + (n - x - 1) * '_' + s
350-
second_line = x * ' ' + '/' + (n - x - 1 + u) * ' '
351-
shifted_lines = [line + u * ' ' for line in lines]
349+
first_line = (x + 1) * " " + (n - x - 1) * "_" + s
350+
second_line = x * " " + "/" + (n - x - 1 + u) * " "
351+
shifted_lines = [line + u * " " for line in lines]
352352
return [first_line, second_line] + shifted_lines, n + u, p + 2, n + u // 2
353353

354354
if node.left is None: # Only right child
355355
lines, n, p, x = self._printTree(node.right)
356-
s = '%s' % node.data
356+
s = "%s" % node.data
357357
u = len(s)
358-
first_line = s + x * '_' + (n - x) * ' '
359-
second_line = (u + x) * ' ' + '\\' + (n - x - 1) * ' '
360-
shifted_lines = [u * ' ' + line for line in lines]
358+
first_line = s + x * "_" + (n - x) * " "
359+
second_line = (u + x) * " " + "\\" + (n - x - 1) * " "
360+
shifted_lines = [u * " " + line for line in lines]
361361
return [first_line, second_line] + shifted_lines, n + u, p + 2, u // 2
362362

363363
left, n, p, x = self._printTree(node.left) # Two children
364364
right, m, q, y = self._printTree(node.right)
365-
s = '%s' % node.data
365+
s = "%s" % node.data
366366
u = len(s)
367-
first_line = (x + 1) * ' ' + (n - x - 1) * \
368-
'_' + s + y * '_' + (m - y) * ' '
369-
second_line = x * ' ' + '/' + \
370-
(n - x - 1 + u + y) * ' ' + '\\' + (m - y - 1) * ' '
367+
first_line = (x + 1) * " " + (n - x - 1) * "_" + s + y * "_" + (m - y) * " "
368+
second_line = (
369+
x * " " + "/" + (n - x - 1 + u + y) * " " + "\\" + (m - y - 1) * " "
370+
)
371371
if p < q:
372-
left += [n * ' '] * (q - p)
372+
left += [n * " "] * (q - p)
373373
elif q < p:
374-
right += [m * ' '] * (p - q)
374+
right += [m * " "] * (p - q)
375375
zipped_lines = zip(left, right)
376-
lines = [first_line, second_line] + \
377-
[a + u * ' ' + b for a, b in zipped_lines]
376+
lines = [first_line, second_line] + [a + u * " " + b for a, b in zipped_lines]
378377
return lines, n + m + u, max(p, q) + 2, n + u // 2
379378

380379
def printHeights(self):
@@ -387,8 +386,7 @@ def _printHeights(self, cur_node):
387386
"""Auxiliary function to print the height of nodes"""
388387
if cur_node != None:
389388
self._printHeights(cur_node.left)
390-
print(f'{cur_node.data},'.center(5) +
391-
f' {str(cur_node.height).center(6)}')
389+
print(f"{cur_node.data},".center(5) + f" {str(cur_node.height).center(6)}")
392390
self._printHeights(cur_node.right)
393391

394392
def printParents(self):
@@ -402,9 +400,10 @@ def _printParents(self, cur_node):
402400
if cur_node != None:
403401
self._printParents(cur_node.left)
404402
if cur_node.parent != None:
405-
print(f'{cur_node.data},'.center(5) +
406-
f' {str(cur_node.parent.data).center(6)}')
403+
print(
404+
f"{cur_node.data},".center(5)
405+
+ f" {str(cur_node.parent.data).center(6)}"
406+
)
407407
else:
408-
print(f'{cur_node.data},'.center(5) +
409-
f' {str(None).center(6)}')
408+
print(f"{cur_node.data},".center(5) + f" {str(None).center(6)}")
410409
self._printParents(cur_node.right)

Binary Tree.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def height(self, node):
154154
else:
155155
left_height = self.height(node.left)
156156
right_height = self.height(node.right)
157-
if (left_height > right_height):
157+
if left_height > right_height:
158158
return left_height + 1
159159
else:
160160
return right_height + 1
@@ -172,55 +172,54 @@ def empty(self):
172172
if self._root is None:
173173
return True
174174
return False
175-
175+
176176
def printTree(self, node=None):
177-
"""Prints the Avl Tree"""
177+
"""Prints the Tree"""
178178
if node is None:
179179
node = self._root
180180
lines, *_ = self._printTree(node)
181181
for line in lines:
182182
print(line)
183183

184184
def _printTree(self, node):
185-
"""Auxiliary function to print the Avl Tree"""
185+
"""Auxiliary function to print the Tree"""
186186
if node.right is None and node.left is None: # No child
187-
line = '%s' % node.data
187+
line = "%s" % node.data
188188
width = len(line)
189189
height = 1
190190
middle = width // 2
191191
return [line], width, height, middle
192192

193193
if node.right is None: # Only left child
194194
lines, n, p, x = self._printTree(node.left)
195-
s = '%s' % node.data
195+
s = "%s" % node.data
196196
u = len(s)
197-
first_line = (x + 1) * ' ' + (n - x - 1) * '_' + s
198-
second_line = x * ' ' + '/' + (n - x - 1 + u) * ' '
199-
shifted_lines = [line + u * ' ' for line in lines]
197+
first_line = (x + 1) * " " + (n - x - 1) * "_" + s
198+
second_line = x * " " + "/" + (n - x - 1 + u) * " "
199+
shifted_lines = [line + u * " " for line in lines]
200200
return [first_line, second_line] + shifted_lines, n + u, p + 2, n + u // 2
201201

202202
if node.left is None: # Only right child
203203
lines, n, p, x = self._printTree(node.right)
204-
s = '%s' % node.data
204+
s = "%s" % node.data
205205
u = len(s)
206-
first_line = s + x * '_' + (n - x) * ' '
207-
second_line = (u + x) * ' ' + '\\' + (n - x - 1) * ' '
208-
shifted_lines = [u * ' ' + line for line in lines]
206+
first_line = s + x * "_" + (n - x) * " "
207+
second_line = (u + x) * " " + "\\" + (n - x - 1) * " "
208+
shifted_lines = [u * " " + line for line in lines]
209209
return [first_line, second_line] + shifted_lines, n + u, p + 2, u // 2
210210

211211
left, n, p, x = self._printTree(node.left) # Two children
212212
right, m, q, y = self._printTree(node.right)
213-
s = '%s' % node.data
213+
s = "%s" % node.data
214214
u = len(s)
215-
first_line = (x + 1) * ' ' + (n - x - 1) * \
216-
'_' + s + y * '_' + (m - y) * ' '
217-
second_line = x * ' ' + '/' + \
218-
(n - x - 1 + u + y) * ' ' + '\\' + (m - y - 1) * ' '
215+
first_line = (x + 1) * " " + (n - x - 1) * "_" + s + y * "_" + (m - y) * " "
216+
second_line = (
217+
x * " " + "/" + (n - x - 1 + u + y) * " " + "\\" + (m - y - 1) * " "
218+
)
219219
if p < q:
220-
left += [n * ' '] * (q - p)
220+
left += [n * " "] * (q - p)
221221
elif q < p:
222-
right += [m * ' '] * (p - q)
222+
right += [m * " "] * (p - q)
223223
zipped_lines = zip(left, right)
224-
lines = [first_line, second_line] + \
225-
[a + u * ' ' + b for a, b in zipped_lines]
224+
lines = [first_line, second_line] + [a + u * " " + b for a, b in zipped_lines]
226225
return lines, n + m + u, max(p, q) + 2, n + u // 2

Circular Queue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def last(self):
5353
"""Returns the last element from queue"""
5454
if self._size == 0:
5555
raise Exception("Empty queue")
56-
return self._queue[self._back-1]
56+
return self._queue[self._back - 1]
5757

5858
def empty(self):
5959
"""Verifys if the queue is empty"""

Doubly Linked List.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def insert(self, index, elem):
7878
self._size += 1
7979
elif index > 0:
8080
node_next = self.getItemByIndex(index)
81-
node_prev = self.getItemByIndex(index-1)
81+
node_prev = self.getItemByIndex(index - 1)
8282
node.next = node_next
8383
node.prev = node_prev
8484
node_next.prev = node
@@ -110,7 +110,7 @@ def count(self, elem):
110110
"""Returns the number of elements with the specified value"""
111111
pointer = self._first
112112
cont = 0
113-
while(pointer != None):
113+
while pointer != None:
114114
if pointer.data == elem:
115115
cont += 1
116116
pointer = pointer.next
@@ -120,7 +120,7 @@ def index(self, elem):
120120
"""Returns the index of specified element"""
121121
pointer = self._first
122122
cont = 0
123-
while(pointer):
123+
while pointer:
124124
if pointer.data == elem:
125125
return cont
126126
else:
@@ -133,7 +133,7 @@ def reverse(self):
133133
if self._size == 0:
134134
raise IndexError("Empty list")
135135
pointer = self._first
136-
while(pointer):
136+
while pointer:
137137
pointer.next, pointer.prev = pointer.prev, pointer.next
138138
pointer = pointer.prev
139139
self._first, self._last = self._last, self._first
@@ -144,7 +144,7 @@ def createReversed(self):
144144
raise IndexError("Empty list")
145145
new = DoublyLinkedList()
146146
pointer = self._last
147-
while(pointer):
147+
while pointer:
148148
new.append(pointer.data)
149149
pointer = pointer.prev
150150
return new
@@ -177,12 +177,12 @@ def __delitem__(self, index):
177177
if index == 0:
178178
self._first = self._first.next
179179
self._first.prev = None
180-
elif index == self._size-1:
180+
elif index == self._size - 1:
181181
self._last = self._last.prev
182182
self._last.next = None
183183
else:
184-
node_next = self.getItemByIndex(index+1)
185-
node_prev = self.getItemByIndex(index-1)
184+
node_next = self.getItemByIndex(index + 1)
185+
node_prev = self.getItemByIndex(index - 1)
186186
node_next.prev = node_prev
187187
node_prev.next = node_next
188188
self._size -= 1
@@ -197,7 +197,7 @@ def __str__(self):
197197
rep = "\033[1;34mNone\033[0;0m" + " <- {} " + "\033[1;34mNone\033[0;0m"
198198
pointer = self._first
199199
aux = ""
200-
while(pointer != None):
200+
while pointer != None:
201201
if pointer == self._first and pointer.next is None:
202202
aux += "\033[1;31m" + str(pointer.data) + "\033[0;0m" + " -> "
203203
break
@@ -206,8 +206,7 @@ def __str__(self):
206206
break
207207
else:
208208
if self._first == pointer:
209-
aux += "\033[1;31m" + \
210-
str(pointer.data) + "\033[0;0m" + " <-> "
209+
aux += "\033[1;31m" + str(pointer.data) + "\033[0;0m" + " <-> "
211210
else:
212211
aux += f"{pointer.data} <-> "
213212
pointer = pointer.next

Hash Table.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ def hashing(self, key, table_size):
4646
"""Transforms a key in index (Division method)"""
4747
if (isinstance(key, str) and key.isalnum()) or isinstance(key, int):
4848
if isinstance(key, str):
49-
key = Hash.stringProcess(key)
49+
key = HashTable.stringProcess(key)
5050
index = (key & int("0x7FFFFFFF", 16)) % table_size
5151
else:
5252
raise Exception("Unsupported type")
5353
return index
5454

5555
def doubleHashing(self, index, key, count):
5656
"""Apply a new hash function"""
57-
new_index = self.hashing(key, self._max-1) + 1
58-
return ((index + count*new_index) & int("0x7FFFFFFF", 16)) % self._max
57+
new_index = self.hashing(key, self._max - 1) + 1
58+
return ((index + count * new_index) & int("0x7FFFFFFF", 16)) % self._max
5959

6060
def reset(self):
6161
"""Restores the hash table to its starting point(Empty)"""
@@ -67,7 +67,10 @@ def processCollisions(self, index, key):
6767
count = 0
6868
while True:
6969
new_index = self.doubleHashing(index, key, count)
70-
if self._hash_table[new_index] is None or self._hash_table[new_index].key == key:
70+
if (
71+
self._hash_table[new_index] is None
72+
or self._hash_table[new_index].key == key
73+
):
7174
return new_index
7275
else:
7376
index = new_index
@@ -90,16 +93,16 @@ def __getitem__(self, key):
9093
raise Exception("Hash table is empty")
9194
index = self.hashing(key, self._max)
9295
if self._hash_table[index] is None:
93-
raise Exception(
94-
f"The index corresponding to the key: {key}, is empty")
96+
raise Exception(f"The index corresponding to the key: {key}, is empty")
9597
else:
9698
if self._hash_table[index].key == key:
9799
return self._hash_table[index].data
98100
else:
99101
new_index = self.processCollisions(index, key)
100102
if self._hash_table[new_index] is None:
101103
raise Exception(
102-
f"The index corresponding to the key: {key}, is empty")
104+
f"The index corresponding to the key: {key}, is empty"
105+
)
103106
return self._hash_table[new_index].data
104107

105108
def __len__(self):

0 commit comments

Comments
 (0)