AlgoDatSoSe26/vorlesung/L05_binaere_baeume/analyze_binary_tree.py
2026-05-04 16:03:23 +02:00

29 lines
891 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from utils.algo_context import AlgoContext
from utils.algo_array import Array
from vorlesung.L05_binaere_baeume.bin_tree import BinaryTree
def analyze_complexity(sizes):
import random
sizes = list(sizes)
# Alle Elemente vorab erzeugen Baum wird inkrementell aufgebaut statt
# für jede Größe neu, dadurch O(n log n) statt O(n²) Gesamtaufwand.
elements = random.sample(range(max(sizes) * 10), max(sizes))
ctx = AlgoContext()
tree = BinaryTree(ctx)
prev = 0
for size in sizes:
for i in range(prev, size - 1): # Warm-up: ohne Messung einfügen
tree.insert(elements[i])
ctx.reset()
tree.insert(elements[size - 1]) # gemessener Insert
ctx.save_stats(size)
prev = size
ctx.plot_stats(["comparisons"])
if __name__ == "__main__":
sizes = range(10, 501, 10)
analyze_complexity(sizes)