Update binary_tree_generator.py
Browse files- binary_tree_generator.py +11 -16
binary_tree_generator.py
CHANGED
|
@@ -59,7 +59,7 @@ def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
|
|
| 59 |
name='BinaryTree',
|
| 60 |
format='png',
|
| 61 |
graph_attr={
|
| 62 |
-
'rankdir': 'TB', # Top-to-Bottom layout
|
| 63 |
'splines': 'line', # Straight lines
|
| 64 |
'bgcolor': 'white', # White background
|
| 65 |
'pad': '0.5', # Padding around the graph
|
|
@@ -68,7 +68,7 @@ def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
|
|
| 68 |
}
|
| 69 |
)
|
| 70 |
|
| 71 |
-
base_color = '#19191a'
|
| 72 |
|
| 73 |
def add_binary_tree_nodes(node, current_depth=0):
|
| 74 |
"""
|
|
@@ -80,12 +80,12 @@ def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
|
|
| 80 |
node_id = node.get('id', f'node_{current_depth}')
|
| 81 |
node_label = node.get('label', 'Node')
|
| 82 |
|
| 83 |
-
|
| 84 |
lightening_factor = 0.12
|
| 85 |
|
| 86 |
-
|
| 87 |
if not isinstance(base_color, str) or not base_color.startswith('#') or len(base_color) != 7:
|
| 88 |
-
base_color_safe = '#19191a'
|
| 89 |
else:
|
| 90 |
base_color_safe = base_color
|
| 91 |
|
|
@@ -93,25 +93,23 @@ def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
|
|
| 93 |
base_g = int(base_color_safe[3:5], 16)
|
| 94 |
base_b = int(base_color_safe[5:7], 16)
|
| 95 |
|
| 96 |
-
|
| 97 |
current_r = base_r + int((255 - base_r) * current_depth * lightening_factor)
|
| 98 |
current_g = base_g + int((255 - base_g) * current_depth * lightening_factor)
|
| 99 |
current_b = base_b + int((255 - base_b) * current_depth * lightening_factor)
|
| 100 |
|
| 101 |
-
|
| 102 |
current_r = min(255, current_r)
|
| 103 |
current_g = min(255, current_g)
|
| 104 |
current_b = min(255, current_b)
|
| 105 |
|
| 106 |
node_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
|
| 107 |
|
| 108 |
-
# Font color: white for dark nodes, black for very light nodes for readability
|
| 109 |
font_color = 'white' if current_depth * lightening_factor < 0.6 else 'black'
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
|
| 114 |
-
# Add the current node
|
| 115 |
dot.node(
|
| 116 |
node_id,
|
| 117 |
node_label,
|
|
@@ -124,7 +122,6 @@ def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
|
|
| 124 |
height='0.8'
|
| 125 |
)
|
| 126 |
|
| 127 |
-
# Process left child
|
| 128 |
left_child = node.get('left')
|
| 129 |
if left_child:
|
| 130 |
add_binary_tree_nodes(left_child, current_depth + 1)
|
|
@@ -132,11 +129,10 @@ def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
|
|
| 132 |
dot.edge(
|
| 133 |
node_id,
|
| 134 |
left_id,
|
| 135 |
-
color='#4a4a4a',
|
| 136 |
arrowsize='0.8'
|
| 137 |
)
|
| 138 |
|
| 139 |
-
# Process right child
|
| 140 |
right_child = node.get('right')
|
| 141 |
if right_child:
|
| 142 |
add_binary_tree_nodes(right_child, current_depth + 1)
|
|
@@ -144,11 +140,10 @@ def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
|
|
| 144 |
dot.edge(
|
| 145 |
node_id,
|
| 146 |
right_id,
|
| 147 |
-
color='#4a4a4a',
|
| 148 |
arrowsize='0.8'
|
| 149 |
)
|
| 150 |
|
| 151 |
-
# Add binary tree nodes and edges recursively
|
| 152 |
add_binary_tree_nodes(data['root'], current_depth=0)
|
| 153 |
|
| 154 |
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
|
|
|
| 59 |
name='BinaryTree',
|
| 60 |
format='png',
|
| 61 |
graph_attr={
|
| 62 |
+
'rankdir': 'TB', # Top-to-Bottom layout
|
| 63 |
'splines': 'line', # Straight lines
|
| 64 |
'bgcolor': 'white', # White background
|
| 65 |
'pad': '0.5', # Padding around the graph
|
|
|
|
| 68 |
}
|
| 69 |
)
|
| 70 |
|
| 71 |
+
base_color = '#19191a'
|
| 72 |
|
| 73 |
def add_binary_tree_nodes(node, current_depth=0):
|
| 74 |
"""
|
|
|
|
| 80 |
node_id = node.get('id', f'node_{current_depth}')
|
| 81 |
node_label = node.get('label', 'Node')
|
| 82 |
|
| 83 |
+
|
| 84 |
lightening_factor = 0.12
|
| 85 |
|
| 86 |
+
|
| 87 |
if not isinstance(base_color, str) or not base_color.startswith('#') or len(base_color) != 7:
|
| 88 |
+
base_color_safe = '#19191a'
|
| 89 |
else:
|
| 90 |
base_color_safe = base_color
|
| 91 |
|
|
|
|
| 93 |
base_g = int(base_color_safe[3:5], 16)
|
| 94 |
base_b = int(base_color_safe[5:7], 16)
|
| 95 |
|
| 96 |
+
|
| 97 |
current_r = base_r + int((255 - base_r) * current_depth * lightening_factor)
|
| 98 |
current_g = base_g + int((255 - base_g) * current_depth * lightening_factor)
|
| 99 |
current_b = base_b + int((255 - base_b) * current_depth * lightening_factor)
|
| 100 |
|
| 101 |
+
|
| 102 |
current_r = min(255, current_r)
|
| 103 |
current_g = min(255, current_g)
|
| 104 |
current_b = min(255, current_b)
|
| 105 |
|
| 106 |
node_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
|
| 107 |
|
|
|
|
| 108 |
font_color = 'white' if current_depth * lightening_factor < 0.6 else 'black'
|
| 109 |
|
| 110 |
+
font_size = max(9, 14 - current_depth)
|
| 111 |
+
|
| 112 |
|
|
|
|
| 113 |
dot.node(
|
| 114 |
node_id,
|
| 115 |
node_label,
|
|
|
|
| 122 |
height='0.8'
|
| 123 |
)
|
| 124 |
|
|
|
|
| 125 |
left_child = node.get('left')
|
| 126 |
if left_child:
|
| 127 |
add_binary_tree_nodes(left_child, current_depth + 1)
|
|
|
|
| 129 |
dot.edge(
|
| 130 |
node_id,
|
| 131 |
left_id,
|
| 132 |
+
color='#4a4a4a',
|
| 133 |
arrowsize='0.8'
|
| 134 |
)
|
| 135 |
|
|
|
|
| 136 |
right_child = node.get('right')
|
| 137 |
if right_child:
|
| 138 |
add_binary_tree_nodes(right_child, current_depth + 1)
|
|
|
|
| 140 |
dot.edge(
|
| 141 |
node_id,
|
| 142 |
right_id,
|
| 143 |
+
color='#4a4a4a',
|
| 144 |
arrowsize='0.8'
|
| 145 |
)
|
| 146 |
|
|
|
|
| 147 |
add_binary_tree_nodes(data['root'], current_depth=0)
|
| 148 |
|
| 149 |
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|