Spaces:
Running
Running
Update binary_tree_generator.py
Browse files- binary_tree_generator.py +1 -78
binary_tree_generator.py
CHANGED
@@ -1,81 +1,4 @@
|
|
1 |
-
|
2 |
-
"""
|
3 |
-
Add binary tree nodes recursively with proper styling.
|
4 |
-
"""
|
5 |
-
if not node:
|
6 |
-
return
|
7 |
-
|
8 |
-
node_id = node.get('id', f'node_{current_depth}')
|
9 |
-
node_label = node.get('label', 'Node')
|
10 |
-
|
11 |
-
# Calculate color for current depth using the same logic as graph_generator_utils
|
12 |
-
lightening_factor = 0.12
|
13 |
-
|
14 |
-
# Convert base_color hex to RGB for interpolation
|
15 |
-
if not isinstance(base_color, str) or not base_color.startswith('#') or len(base_color) != 7:
|
16 |
-
base_color_safe = '#19191a' # Fallback to default dark if invalid
|
17 |
-
else:
|
18 |
-
base_color_safe = base_color
|
19 |
-
|
20 |
-
base_r = int(base_color_safe[1:3], 16)
|
21 |
-
base_g = int(base_color_safe[3:5], 16)
|
22 |
-
base_b = int(base_color_safe[5:7], 16)
|
23 |
-
|
24 |
-
# Calculate current node color by blending towards white
|
25 |
-
current_r = base_r + int((255 - base_r) * current_depth * lightening_factor)
|
26 |
-
current_g = base_g + int((255 - base_g) * current_depth * lightening_factor)
|
27 |
-
current_b = base_b + int((255 - base_b) * current_depth * lightening_factor)
|
28 |
-
|
29 |
-
# Clamp values to 255 to stay within valid RGB range
|
30 |
-
current_r = min(255, current_r)
|
31 |
-
current_g = min(255, current_g)
|
32 |
-
current_b = min(255, current_b)
|
33 |
-
|
34 |
-
node_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
|
35 |
-
|
36 |
-
# Font color: white for dark nodes, black for very light nodes for readability
|
37 |
-
font_color = 'white' if current_depth * lightening_factor < 0.6 else 'black'
|
38 |
-
|
39 |
-
# Font size adjusts based on depth, ensuring a minimum size
|
40 |
-
font_size = max(9, 14 - (current_depth * 2))
|
41 |
-
|
42 |
-
# Add the current node
|
43 |
-
dot.node(
|
44 |
-
node_id,
|
45 |
-
node_label,
|
46 |
-
shape='circle',
|
47 |
-
style='filled',
|
48 |
-
fillcolor=node_color,
|
49 |
-
fontcolor=font_color,
|
50 |
-
fontsize=str(font_size),
|
51 |
-
width='0.8',
|
52 |
-
height='0.8'
|
53 |
-
)
|
54 |
-
|
55 |
-
# Edge color
|
56 |
-
edge_color = '#4a4a4a' # Dark gray for lines
|
57 |
-
|
58 |
-
# Process left child
|
59 |
-
left_child = node.get('left')
|
60 |
-
if left_child:
|
61 |
-
add_binary_tree_nodes(left_child, current_depth + 1)
|
62 |
-
left_id = left_child.get('id', f'node_{current_depth + 1}_left')
|
63 |
-
dot.edge(
|
64 |
-
node_id,
|
65 |
-
left_id,
|
66 |
-
color=edge_color,
|
67 |
-
arrowsize='0.8'
|
68 |
-
)
|
69 |
-
|
70 |
-
# Process right child
|
71 |
-
right_child = node.get('right')
|
72 |
-
if right_child:
|
73 |
-
add_binary_tree_nodes(right_child, current_depth + 1)
|
74 |
-
right_id = right_child.get('id', f'node_{current_depth + 1}_right')
|
75 |
-
dot.edge(
|
76 |
-
node_id,
|
77 |
-
right_id,
|
78 |
-
color=edge_import graphviz
|
79 |
import json
|
80 |
from tempfile import NamedTemporaryFile
|
81 |
import os
|
|
|
1 |
+
import graphviz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import json
|
3 |
from tempfile import NamedTemporaryFile
|
4 |
import os
|