Spaces:
Running
Running
Update binary_tree_generator.py
Browse files- binary_tree_generator.py +170 -22
binary_tree_generator.py
CHANGED
@@ -1,3 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import graphviz
|
2 |
import json
|
3 |
from tempfile import NamedTemporaryFile
|
@@ -59,33 +216,28 @@ def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
|
|
59 |
name='BinaryTree',
|
60 |
format='png',
|
61 |
graph_attr={
|
62 |
-
'rankdir': 'TB',
|
63 |
-
'splines': 'line',
|
64 |
-
'bgcolor': 'white',
|
65 |
-
'pad': '0.5',
|
66 |
-
'nodesep': '0.8',
|
67 |
-
'ranksep': '1.0'
|
68 |
}
|
69 |
)
|
70 |
|
71 |
-
base_color = '#
|
72 |
|
73 |
def add_binary_tree_nodes(node, current_depth=0):
|
74 |
-
"""
|
75 |
-
Add binary tree nodes recursively with proper styling.
|
76 |
-
"""
|
77 |
if not node:
|
78 |
return
|
79 |
|
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 = '#
|
89 |
else:
|
90 |
base_color_safe = base_color
|
91 |
|
@@ -93,28 +245,24 @@ 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 = '
|
109 |
-
|
110 |
font_size = max(9, 14 - current_depth)
|
111 |
|
112 |
-
|
113 |
dot.node(
|
114 |
node_id,
|
115 |
node_label,
|
116 |
shape='circle',
|
117 |
-
style='filled',
|
118 |
fillcolor=node_color,
|
119 |
fontcolor=font_color,
|
120 |
fontsize=str(font_size),
|
@@ -129,7 +277,7 @@ def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
|
|
129 |
dot.edge(
|
130 |
node_id,
|
131 |
left_id,
|
132 |
-
color='#4a4a4a',
|
133 |
arrowsize='0.8'
|
134 |
)
|
135 |
|
@@ -140,7 +288,7 @@ def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
|
|
140 |
dot.edge(
|
141 |
node_id,
|
142 |
right_id,
|
143 |
-
color='#4a4a4a',
|
144 |
arrowsize='0.8'
|
145 |
)
|
146 |
|
|
|
1 |
+
# import graphviz
|
2 |
+
# import json
|
3 |
+
# from tempfile import NamedTemporaryFile
|
4 |
+
# import os
|
5 |
+
|
6 |
+
# def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
|
7 |
+
# """
|
8 |
+
# Generates a binary tree diagram from JSON input.
|
9 |
+
|
10 |
+
# Args:
|
11 |
+
# json_input (str): A JSON string describing the binary tree structure.
|
12 |
+
# It must follow the Expected JSON Format Example below.
|
13 |
+
|
14 |
+
# Expected JSON Format Example:
|
15 |
+
# {
|
16 |
+
# "root": {
|
17 |
+
# "id": "root",
|
18 |
+
# "label": "50",
|
19 |
+
# "left": {
|
20 |
+
# "id": "left_1",
|
21 |
+
# "label": "30",
|
22 |
+
# "left": {
|
23 |
+
# "id": "left_2",
|
24 |
+
# "label": "20"
|
25 |
+
# },
|
26 |
+
# "right": {
|
27 |
+
# "id": "right_2",
|
28 |
+
# "label": "40"
|
29 |
+
# }
|
30 |
+
# },
|
31 |
+
# "right": {
|
32 |
+
# "id": "right_1",
|
33 |
+
# "label": "70",
|
34 |
+
# "left": {
|
35 |
+
# "id": "left_3",
|
36 |
+
# "label": "60"
|
37 |
+
# },
|
38 |
+
# "right": {
|
39 |
+
# "id": "right_3",
|
40 |
+
# "label": "80"
|
41 |
+
# }
|
42 |
+
# }
|
43 |
+
# }
|
44 |
+
# }
|
45 |
+
|
46 |
+
# Returns:
|
47 |
+
# str: The filepath to the generated PNG image file.
|
48 |
+
# """
|
49 |
+
# try:
|
50 |
+
# if not json_input.strip():
|
51 |
+
# return "Error: Empty input"
|
52 |
+
|
53 |
+
# data = json.loads(json_input)
|
54 |
+
|
55 |
+
# if 'root' not in data:
|
56 |
+
# raise ValueError("Missing required field: root")
|
57 |
+
|
58 |
+
# dot = graphviz.Digraph(
|
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
|
66 |
+
# 'nodesep': '0.8', # Spacing between nodes
|
67 |
+
# 'ranksep': '1.0' # Spacing between levels
|
68 |
+
# }
|
69 |
+
# )
|
70 |
+
|
71 |
+
# base_color = '#19191a'
|
72 |
+
|
73 |
+
# def add_binary_tree_nodes(node, current_depth=0):
|
74 |
+
# """
|
75 |
+
# Add binary tree nodes recursively with proper styling.
|
76 |
+
# """
|
77 |
+
# if not node:
|
78 |
+
# return
|
79 |
+
|
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 |
+
|
92 |
+
# base_r = int(base_color_safe[1:3], 16)
|
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,
|
116 |
+
# shape='circle',
|
117 |
+
# style='filled',
|
118 |
+
# fillcolor=node_color,
|
119 |
+
# fontcolor=font_color,
|
120 |
+
# fontsize=str(font_size),
|
121 |
+
# width='0.8',
|
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)
|
128 |
+
# left_id = left_child.get('id', f'node_{current_depth + 1}_left')
|
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)
|
139 |
+
# right_id = right_child.get('id', f'node_{current_depth + 1}_right')
|
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:
|
150 |
+
# dot.render(tmp.name, format=output_format, cleanup=True)
|
151 |
+
# return f"{tmp.name}.{output_format}"
|
152 |
+
|
153 |
+
# except json.JSONDecodeError:
|
154 |
+
# return "Error: Invalid JSON format"
|
155 |
+
# except Exception as e:
|
156 |
+
# return f"Error: {str(e)}"
|
157 |
+
|
158 |
import graphviz
|
159 |
import json
|
160 |
from tempfile import NamedTemporaryFile
|
|
|
216 |
name='BinaryTree',
|
217 |
format='png',
|
218 |
graph_attr={
|
219 |
+
'rankdir': 'TB',
|
220 |
+
'splines': 'line',
|
221 |
+
'bgcolor': 'white',
|
222 |
+
'pad': '0.5',
|
223 |
+
'nodesep': '0.8',
|
224 |
+
'ranksep': '1.0'
|
225 |
}
|
226 |
)
|
227 |
|
228 |
+
base_color = '#BEBEBE'
|
229 |
|
230 |
def add_binary_tree_nodes(node, current_depth=0):
|
|
|
|
|
|
|
231 |
if not node:
|
232 |
return
|
233 |
|
234 |
node_id = node.get('id', f'node_{current_depth}')
|
235 |
node_label = node.get('label', 'Node')
|
236 |
|
237 |
+
lightening_factor = 0.06
|
|
|
|
|
238 |
|
239 |
if not isinstance(base_color, str) or not base_color.startswith('#') or len(base_color) != 7:
|
240 |
+
base_color_safe = '#BEBEBE'
|
241 |
else:
|
242 |
base_color_safe = base_color
|
243 |
|
|
|
245 |
base_g = int(base_color_safe[3:5], 16)
|
246 |
base_b = int(base_color_safe[5:7], 16)
|
247 |
|
|
|
248 |
current_r = base_r + int((255 - base_r) * current_depth * lightening_factor)
|
249 |
current_g = base_g + int((255 - base_g) * current_depth * lightening_factor)
|
250 |
current_b = base_b + int((255 - base_b) * current_depth * lightening_factor)
|
251 |
|
|
|
252 |
current_r = min(255, current_r)
|
253 |
current_g = min(255, current_g)
|
254 |
current_b = min(255, current_b)
|
255 |
|
256 |
node_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
|
257 |
|
258 |
+
font_color = 'black'
|
|
|
259 |
font_size = max(9, 14 - current_depth)
|
260 |
|
|
|
261 |
dot.node(
|
262 |
node_id,
|
263 |
node_label,
|
264 |
shape='circle',
|
265 |
+
style='filled,rounded',
|
266 |
fillcolor=node_color,
|
267 |
fontcolor=font_color,
|
268 |
fontsize=str(font_size),
|
|
|
277 |
dot.edge(
|
278 |
node_id,
|
279 |
left_id,
|
280 |
+
color='#4a4a4a',
|
281 |
arrowsize='0.8'
|
282 |
)
|
283 |
|
|
|
288 |
dot.edge(
|
289 |
node_id,
|
290 |
right_id,
|
291 |
+
color='#4a4a4a',
|
292 |
arrowsize='0.8'
|
293 |
)
|
294 |
|