Adding more algorithms

This commit is contained in:
Vinicius Silva 2024-05-13 12:13:28 -03:00
parent 956871e340
commit e32352b117
6 changed files with 178 additions and 0 deletions

View File

View File

@ -0,0 +1,17 @@
class Node():
def __init__(self, val):
self.val = val
self.right = None
self.left = None
class BinaryTree():
def __init__(self, root):
self.root = root
def insert(self, val):
node = self.root

View File

@ -0,0 +1,35 @@
class HashSet():
def __init__(self, type):
self.type = type
self.hash = []
def insert(self, data):
if type(data) == self.type and not self.__contains(data):
self.hash.append(data)
def remove(self, data):
self.hash.remove(data)
def __contains(self, data):
return data in self.hash
def __str__(self):
string = ''
for element in self.hash:
string += f'{element} '
return string
if __name__ == '__main__':
hashset = HashSet(str)
hashset.insert('Vinicius')
hashset.insert(1)
hashset.insert(4)
hashset.insert('Teste')
print(hashset.__str__())
hashset.remove('Vinicius')
print(hashset.__str__())

View File

@ -0,0 +1,51 @@
class Cell():
def __init__(self, data):
self.data = data
self.next = None
class LinkedList():
def __init__(self, root):
self.root = root
def insert(self, data):
cell = self.root
while cell.next:
cell = cell.next
cell.next = Cell(data)
def remove(self, data):
if self.root.data == data:
self.root = self.root.next
else:
cell = self.root
while cell.next and cell.next.data != data:
cell = cell.next
if not cell.next: cell.next = cell.next.next
def __str__(self):
string = ''
r = self.root
while r != None:
string += f'{r.data} '
r = r.next
return string
if __name__ == '__main__':
linked = LinkedList(Cell('Vinicius'))
linked.insert('Teste')
linked.insert('Felix')
linked.remove('Felix')
linked.remove('Felix')
print(linked.__str__())

View File

@ -0,0 +1,37 @@
class Queque():
def __init__(self):
self.data = []
self.total_elements = 0
def enqueque(self, element):
self.data = [element] + self.data
self.total_elements += 1
def dequeque(self):
if self.total_elements > 0:
self.data[self.total_elements-1]
self.total_elements -= 1
def __str__(self):
string = ''
for i in range(0, self.total_elements):
string += f'{self.data[i]} '
return string
if __name__ == '__main__':
queque = Queque()
queque.enqueque(1)
queque.enqueque(2)
queque.enqueque(3)
queque.enqueque(4)
queque.enqueque(5)
queque.dequeque()
queque.dequeque()
queque.dequeque()
queque.enqueque(44)
print(queque.__str__())

View File

@ -0,0 +1,38 @@
class Stack():
def __init__(self):
self.data = []
def pop(self):
if len(self.data) == 0:
raise IndexError('Stack out of range')
else:
last_element = self.data[0]
self.data.remove(last_element)
return last_element
def push(self, data):
self.data = self.data + [data]
def __str__(self):
string = ''
for element in self.data:
string += f'{element} '
return string
if __name__ == '__main__':
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
stack.pop()
stack.pop()
stack.pop()
stack.push(44)
print(stack.__str__())