Fehlerbehebung bei bin. Treesa
This commit is contained in:
parent
24d15e1e44
commit
a553d7fd01
@ -4,19 +4,23 @@ from vorlesung.L05_binaere_baeume.avl_tree import AVLTree
|
|||||||
|
|
||||||
|
|
||||||
def analyze_complexity(sizes):
|
def analyze_complexity(sizes):
|
||||||
|
import random
|
||||||
|
sizes = list(sizes)
|
||||||
|
elements = random.sample(range(max(sizes) * 10), max(sizes))
|
||||||
ctx = AlgoContext()
|
ctx = AlgoContext()
|
||||||
|
tree = AVLTree(ctx)
|
||||||
|
prev = 0
|
||||||
for size in sizes:
|
for size in sizes:
|
||||||
z = Array.random(size, -100, 100, ctx)
|
for i in range(prev, size - 1):
|
||||||
tree = AVLTree(ctx)
|
tree.insert(elements[i])
|
||||||
for i in range(size - 1):
|
|
||||||
tree.insert(z[i].value)
|
|
||||||
ctx.reset()
|
ctx.reset()
|
||||||
tree.insert(z[size - 1].value)
|
tree.insert(elements[size - 1])
|
||||||
ctx.save_stats(size)
|
ctx.save_stats(size)
|
||||||
|
prev = size
|
||||||
|
|
||||||
ctx.plot_stats(["comparisons"])
|
ctx.plot_stats(["comparisons"])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sizes = range(1, 1001, 2)
|
sizes = range(10, 501, 10)
|
||||||
analyze_complexity(sizes)
|
analyze_complexity(sizes)
|
||||||
|
|||||||
@ -4,19 +4,25 @@ from vorlesung.L05_binaere_baeume.bin_tree import BinaryTree
|
|||||||
|
|
||||||
|
|
||||||
def analyze_complexity(sizes):
|
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()
|
ctx = AlgoContext()
|
||||||
|
tree = BinaryTree(ctx)
|
||||||
|
prev = 0
|
||||||
for size in sizes:
|
for size in sizes:
|
||||||
z = Array.random(size, -100, 100, ctx)
|
for i in range(prev, size - 1): # Warm-up: ohne Messung einfügen
|
||||||
tree = BinaryTree(ctx)
|
tree.insert(elements[i])
|
||||||
for i in range(size - 1):
|
|
||||||
tree.insert(z[i].value)
|
|
||||||
ctx.reset()
|
ctx.reset()
|
||||||
tree.insert(z[size - 1].value)
|
tree.insert(elements[size - 1]) # gemessener Insert
|
||||||
ctx.save_stats(size)
|
ctx.save_stats(size)
|
||||||
|
prev = size
|
||||||
|
|
||||||
ctx.plot_stats(["comparisons"])
|
ctx.plot_stats(["comparisons"])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sizes = range(1, 1001, 2)
|
sizes = range(10, 501, 10)
|
||||||
analyze_complexity(sizes)
|
analyze_complexity(sizes)
|
||||||
|
|||||||
@ -42,7 +42,7 @@ class BinaryTree:
|
|||||||
def search(self, value):
|
def search(self, value):
|
||||||
current = self.root
|
current = self.root
|
||||||
value = self.new_node(value)
|
value = self.new_node(value)
|
||||||
while current:
|
while current is not None:
|
||||||
if value < current:
|
if value < current:
|
||||||
current = current.left
|
current = current.left
|
||||||
elif value > current:
|
elif value > current:
|
||||||
|
|||||||
@ -16,8 +16,8 @@ class BinaryTreeNode(Int):
|
|||||||
self.right = None
|
self.right = None
|
||||||
|
|
||||||
def height(self):
|
def height(self):
|
||||||
left_height = self.left.height() if self.left else 0
|
left_height = self.left.height() if self.left is not None else 0
|
||||||
right_height = self.right.height() if self.right else 0
|
right_height = self.right.height() if self.right is not None else 0
|
||||||
return 1 + max(left_height, right_height)
|
return 1 + max(left_height, right_height)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user