add code
This commit is contained in:
commit
affab00a70
18
main.py
Normal file
18
main.py
Normal file
@ -0,0 +1,18 @@
|
||||
from tree import Tree
|
||||
from queue import Queue
|
||||
from stack import Stack
|
||||
|
||||
# Beispielbaum
|
||||
baum = Tree()
|
||||
baum.insert(9)
|
||||
baum.insert(6)
|
||||
baum.insert(8)
|
||||
baum.insert(5)
|
||||
baum.insert(4)
|
||||
baum.insert(11)
|
||||
baum.insert(12)
|
||||
baum.insert(15)
|
||||
baum.insert(10)
|
||||
baum.insert(13)
|
||||
baum.show()
|
||||
|
19
queue.py
Normal file
19
queue.py
Normal file
@ -0,0 +1,19 @@
|
||||
class Queue:
|
||||
def __init__(self):
|
||||
self.items = []
|
||||
|
||||
def is_empty(self):
|
||||
return len(self.items) == 0
|
||||
|
||||
def enqueue(self, item):
|
||||
self.items.append(item)
|
||||
|
||||
def dequeue(self):
|
||||
if not self.is_empty():
|
||||
return self.items.pop(0)
|
||||
|
||||
def size(self):
|
||||
return len(self.items)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.items)
|
27
stack.py
Normal file
27
stack.py
Normal file
@ -0,0 +1,27 @@
|
||||
class Stack:
|
||||
def __init__(self):
|
||||
self.items = []
|
||||
|
||||
def is_empty(self):
|
||||
return len(self.items) == 0
|
||||
|
||||
def push(self, item):
|
||||
self.items.append(item)
|
||||
|
||||
def pop(self):
|
||||
if not self.is_empty():
|
||||
return self.items.pop()
|
||||
else:
|
||||
raise IndexError("Pop von leerem Stack")
|
||||
|
||||
def peek(self):
|
||||
if not self.is_empty():
|
||||
return self.items[-1]
|
||||
else:
|
||||
raise IndexError("Peek von leerem Stack")
|
||||
|
||||
def size(self):
|
||||
return len(self.items)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.items)
|
105
tree.py
Normal file
105
tree.py
Normal file
@ -0,0 +1,105 @@
|
||||
from queue import Queue
|
||||
from stack import Stack
|
||||
|
||||
class Node:
|
||||
def __init__(self, value):
|
||||
self.value = value # Wert des Knotens
|
||||
self.left = None # Linkes Kind
|
||||
self.right = None # Rechtes Kind
|
||||
|
||||
def insert(self, value):
|
||||
if self.value == value:
|
||||
return print("Die Daten existieren schon.")
|
||||
else:
|
||||
if self.value > value:
|
||||
if self.left is not None:
|
||||
return self.left.insert(value)
|
||||
else:
|
||||
self.left = Node(value)
|
||||
else:
|
||||
if self.right is not None:
|
||||
return self.right.insert(value)
|
||||
else:
|
||||
self.right = Node(value)
|
||||
|
||||
def show(self, level):
|
||||
if self.left is not None:
|
||||
self.left.show(level + 1)
|
||||
print(' ' * 4 * level + "->" + str(self.value))
|
||||
if self.right is not None:
|
||||
self.right.show(level + 1)
|
||||
|
||||
def search(self, value):
|
||||
if self.value == value:
|
||||
return True
|
||||
elif self.value > value:
|
||||
if self.left is not None:
|
||||
return self.left.search(value)
|
||||
else:
|
||||
if self.right is not None:
|
||||
return self.right.search(value)
|
||||
|
||||
def delete(self, value):
|
||||
if value < self.value:
|
||||
if self.left is not None:
|
||||
self.left = self.left.delete(value)
|
||||
elif value > self.value:
|
||||
if self.right is not None:
|
||||
self.right = self.right.delete(value)
|
||||
else:
|
||||
if self.left is None:
|
||||
return self.right
|
||||
elif self.right is None:
|
||||
return self.left
|
||||
else:
|
||||
min_larger_node = self.right
|
||||
while min_larger_node.left is not None:
|
||||
min_larger_node = min_larger_node.left
|
||||
self.value = min_larger_node.value
|
||||
self.right = self.right.delete(min_larger_node.value)
|
||||
return self
|
||||
|
||||
def treeToList(self, output):
|
||||
output.append(self.value)
|
||||
if self.left is not None:
|
||||
self.left.treeToList(output)
|
||||
if self.right is not None:
|
||||
self.right.treeToList(output)
|
||||
return output
|
||||
|
||||
|
||||
class Tree:
|
||||
def __init__(self):
|
||||
self.root = None # Wurzel des Baums
|
||||
|
||||
def insert(self, value):
|
||||
if self.root is None:
|
||||
self.root = Node(value)
|
||||
else:
|
||||
return self.root.insert(value)
|
||||
|
||||
def show(self):
|
||||
if self.root is not None:
|
||||
self.root.show(0)
|
||||
else:
|
||||
print("Baum ist leer")
|
||||
|
||||
def search(self, value):
|
||||
if self.root is not None:
|
||||
print(f"gesuchter Knoten: {self.root.search(value)}")
|
||||
else:
|
||||
print("gesuchter Knoten: Baum ist leer")
|
||||
|
||||
def delete(self, value):
|
||||
if self.root is not None:
|
||||
self.root = self.root.delete(value)
|
||||
else:
|
||||
print("Baum ist leer, nichts zu löschen.")
|
||||
|
||||
def treeToList(self):
|
||||
output = []
|
||||
if self.root is not None:
|
||||
return self.root.treeToList(output)
|
||||
else:
|
||||
return []
|
||||
|
Loading…
Reference in New Issue
Block a user