forked from hofmannol/AlgoDatSoSe25
switched to Graphviz module
Better filename
This commit is contained in:
parent
1c37ed46bf
commit
63a3460c21
@ -1,3 +1,4 @@
|
||||
from utils.memory_array import MemoryArray
|
||||
from vorlesung.L05_binaere_baeume.avl_tree_node import AVLTreeNode
|
||||
from vorlesung.L05_binaere_baeume.bin_tree import BinaryTree
|
||||
import logging
|
||||
@ -54,6 +55,8 @@ class AVLTree(BinaryTree):
|
||||
if parent:
|
||||
self.balance(parent)
|
||||
|
||||
def graph_filename(self):
|
||||
return "AVLTree"
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@ -61,7 +64,8 @@ if __name__ == "__main__":
|
||||
print((indent * 3) * " ", node.value)
|
||||
|
||||
tree = AVLTree()
|
||||
values = [5, 3, 7, 2, 4, 6, 5, 8]
|
||||
#values = [5, 3, 7, 2, 4, 6, 5, 8]
|
||||
values = MemoryArray.create_array_from_file("data/seq2.txt")
|
||||
|
||||
for value in values:
|
||||
tree.insert(value)
|
||||
|
@ -9,9 +9,8 @@ class AVLTreeNode(BinaryTreeNode):
|
||||
def __repr__(self):
|
||||
return f"TreeNode(id={id(self)} value={self.value}, left={self.left}, right={self.right})"
|
||||
|
||||
def gv_rep(self, row, col):
|
||||
"""Returns the graphviz representation of the node."""
|
||||
return f"{id(self)} [label=\"{self.value}\", pos=\"{col},{-row}!\", xlabel=\"{self.balance}\"];\n"
|
||||
def graphviz_rep(self, row, col, dot):
|
||||
dot.node(str(id(self)), label=str(self.value), pos=f"{col},{-row}!", xlabel=str(self.balance))
|
||||
|
||||
def update_balance(self):
|
||||
left_height = self.left.height() if self.left else 0
|
||||
|
@ -3,6 +3,7 @@ from utils.memory_manager import MemoryManager
|
||||
from utils.memory_array import MemoryArray
|
||||
from utils.project_dir import get_path
|
||||
from datetime import datetime
|
||||
import graphviz
|
||||
|
||||
|
||||
class BinaryTree:
|
||||
@ -69,7 +70,7 @@ class BinaryTree:
|
||||
break
|
||||
else:
|
||||
# Wert nicht gefunden
|
||||
return
|
||||
return None, None
|
||||
return self.delete_node(current, parent)
|
||||
|
||||
def delete_node(self, current, parent):
|
||||
@ -144,37 +145,35 @@ class BinaryTree:
|
||||
line = 0
|
||||
tree_structure_traversal_recursive(callback, self.root, 0)
|
||||
|
||||
def graph_filename(self):
|
||||
return "BinaryTree"
|
||||
|
||||
def graph_traversal(self):
|
||||
|
||||
def define_node(node, level, line):
|
||||
nonlocal file
|
||||
nonlocal dot
|
||||
if node is not None:
|
||||
file.write(node.gv_rep(level, line))
|
||||
node.graphviz_rep(level, line, dot)
|
||||
|
||||
def graph_traversal_recursive(current):
|
||||
nonlocal file
|
||||
nonlocal dot
|
||||
if current is not None:
|
||||
if current.left:
|
||||
file.write(f"{id(current)} -> {id(current.left)}; \n")
|
||||
dot.edge(str(id(current)), str(id(current.left)))
|
||||
graph_traversal_recursive(current.left)
|
||||
if current.right:
|
||||
file.write(f"{id(current)} -> {id(current.right)}; \n")
|
||||
dot.edge(str(id(current)), str(id(current.right)))
|
||||
graph_traversal_recursive(current.right)
|
||||
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H:%M:%S")
|
||||
filename = f"graph_{timestamp}.gv"
|
||||
dot = graphviz.Digraph( name="BinaryTree",
|
||||
engine="neato",
|
||||
node_attr={"shape": "circle", "fontname": "Arial"},
|
||||
format="pdf" )
|
||||
self.tree_structure_traversal(define_node)
|
||||
graph_traversal_recursive(self.root)
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
filename = f"{self.graph_filename()}_{timestamp}.gv"
|
||||
filename = get_path(filename)
|
||||
with open(filename, "w") as file:
|
||||
file.write("digraph BST {\n")
|
||||
file.write("layout=neato;\n")
|
||||
file.write("node [shape=circle, fontname=\"Arial\"];\n")
|
||||
self.tree_structure_traversal(define_node)
|
||||
graph_traversal_recursive(self.root)
|
||||
file.write("}")
|
||||
|
||||
|
||||
|
||||
dot.render(filename)
|
||||
|
||||
if __name__ == "__main__":
|
||||
tree = BinaryTree()
|
||||
|
@ -18,7 +18,5 @@ class BinaryTreeNode(MemoryCell):
|
||||
def __str__(self):
|
||||
return str(self.value)
|
||||
|
||||
def gv_rep(self, row, col):
|
||||
"""Returns the graphviz representation of the node."""
|
||||
return f"{id(self)} [label=\"{self.value}\", pos=\"{col},{-row}!\"];\n"
|
||||
|
||||
def graphviz_rep(self, row, col, dot):
|
||||
dot.node(str(id(self)), label=str(self.value), pos=f"{col},{-row}!")
|
Loading…
x
Reference in New Issue
Block a user