From 2b2a97d1dab3b60719e3b27e43c3966e79accfa6 Mon Sep 17 00:00:00 2001 From: Oliver Hofmann Date: Sat, 3 May 2025 09:39:02 +0200 Subject: [PATCH] AVL implementation --- requirements.txt | 1 + vorlesung/L06_b_baeume/analyze_b_tree.py | 58 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 vorlesung/L06_b_baeume/analyze_b_tree.py diff --git a/requirements.txt b/requirements.txt index 7f12edc..ce33b86 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ matplotlib numpy pygame +graphviz diff --git a/vorlesung/L06_b_baeume/analyze_b_tree.py b/vorlesung/L06_b_baeume/analyze_b_tree.py new file mode 100644 index 0000000..e142ee8 --- /dev/null +++ b/vorlesung/L06_b_baeume/analyze_b_tree.py @@ -0,0 +1,58 @@ +from utils.memory_manager import MemoryManager +from utils.memory_array import MemoryArray +from utils.literal import Literal +from b_tree import BTree +from b_tree_node import BTreeNode + +class MemoryManagerBTree(MemoryManager): + """ + Diese Klasse erweitert den MemoryManager, um spezifische Statistiken für B-Bäume zu speichern. + """ + + @staticmethod + def count_loads(): + return sum([cell.loaded_count for cell in MemoryManager().cells if isinstance(cell, BTreeNode)]) + + @staticmethod + def count_saves(): + return sum([cell.saved_count for cell in MemoryManager().cells if isinstance(cell, BTreeNode)]) + + @staticmethod + def save_stats(count): + data = { "cells": MemoryManager.count_cells(), + "reads": MemoryManager.count_reads(), + "writes": MemoryManager.count_writes(), + "compares": MemoryManager.count_compares(), + "adds": MemoryManager.count_adds(), + "subs": MemoryManager.count_subs(), + "muls": MemoryManager.count_muls(), + "divs": MemoryManager.count_divs(), + "bitops": MemoryManager.count_bitops(), + "loads": MemoryManagerBTree.count_loads(), + "saves": MemoryManagerBTree.count_saves() } + MemoryManager.stats[count] = data + + + + +def analyze_complexity(sizes): + """ + Analysiert die Komplexität + + :param sizes: Eine Liste von Eingabegrößen für die Analyse. + """ + for size in sizes: + MemoryManager.purge() # Speicher zurücksetzen + tree = BTree(5) + random_array = MemoryArray.create_random_array(size, -100, 100) + for i in range(size-1): + tree.insert(int(random_array[Literal(i)])) + MemoryManager.reset() + tree.insert(int(random_array[Literal(size-1)])) + MemoryManagerBTree.save_stats(size) + + MemoryManager.plot_stats(["cells", "compares", "loads", "saves"]) + +if __name__ == "__main__": + sizes = range(1, 1001, 2) + analyze_complexity(sizes) \ No newline at end of file