commit affab00a7035448f7fcff8cf46d7a2304aae6e58 Author: schuster Date: Wed Dec 4 08:58:55 2024 +0100 add code diff --git a/main.py b/main.py new file mode 100644 index 0000000..9ff5177 --- /dev/null +++ b/main.py @@ -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() + diff --git a/queue.py b/queue.py new file mode 100644 index 0000000..61dc934 --- /dev/null +++ b/queue.py @@ -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) diff --git a/stack.py b/stack.py new file mode 100644 index 0000000..a2d0609 --- /dev/null +++ b/stack.py @@ -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) \ No newline at end of file diff --git a/tree.py b/tree.py new file mode 100644 index 0000000..ee13f85 --- /dev/null +++ b/tree.py @@ -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 [] +