forked from hofmannol/AlgoDatSoSe25
Compare commits
2 Commits
b12e39952d
...
c382641234
Author | SHA1 | Date | |
---|---|---|---|
c382641234 | |||
a6d652d607 |
26
vorlesung/L05_binaere_baeume/analyze_binary_tree.py
Normal file
26
vorlesung/L05_binaere_baeume/analyze_binary_tree.py
Normal file
@ -0,0 +1,26 @@
|
||||
from utils.memory_manager import MemoryManager
|
||||
from utils.memory_array import MemoryArray
|
||||
from utils.literal import Literal
|
||||
from vorlesung.L05_binaere_baeume.bin_tree import BinaryTree
|
||||
|
||||
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 = BinaryTree()
|
||||
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)]))
|
||||
MemoryManager.save_stats(size)
|
||||
|
||||
MemoryManager.plot_stats(["cells", "compares"])
|
||||
|
||||
if __name__ == "__main__":
|
||||
sizes = range(1, 1001, 2)
|
||||
analyze_complexity(sizes)
|
@ -1,4 +1,6 @@
|
||||
from vorlesung.L05_binaere_baeume.bin_tree_node import BinaryTreeNode
|
||||
from utils.memory_manager import MemoryManager
|
||||
from utils.memory_array import MemoryArray
|
||||
from utils.project_dir import get_path
|
||||
from datetime import datetime
|
||||
|
||||
@ -14,23 +16,24 @@ class BinaryTree:
|
||||
|
||||
def insert(self, value):
|
||||
self.size += 1
|
||||
value = self.new_node(value)
|
||||
if self.root is None:
|
||||
self.root = self.new_node(value)
|
||||
self.root = value
|
||||
return self.root, None
|
||||
else:
|
||||
current = self.root
|
||||
while True:
|
||||
if value < current.value:
|
||||
if value < current:
|
||||
if current.left:
|
||||
current = current.left
|
||||
else:
|
||||
current.left = self.new_node(value)
|
||||
current.left = value
|
||||
return current.left, current
|
||||
elif value >= current.value:
|
||||
elif value >= current:
|
||||
if current.right:
|
||||
current = current.right
|
||||
else:
|
||||
current.right = self.new_node(value)
|
||||
current.right = value
|
||||
return current.right, current
|
||||
else:
|
||||
return None, None
|
||||
@ -200,3 +203,5 @@ if __name__ == "__main__":
|
||||
tree.level_order_traversal(print_node)
|
||||
print("\nTree structure traversal after deletion:")
|
||||
tree.tree_structure_traversal(print_node)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user