ZahirJS commited on
Commit
3de15e1
·
verified ·
1 Parent(s): d5fc08e

Update binary_tree_generator.py

Browse files
Files changed (1) hide show
  1. binary_tree_generator.py +108 -15
binary_tree_generator.py CHANGED
@@ -1,4 +1,81 @@
1
- import graphviz
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import json
3
  from tempfile import NamedTemporaryFile
4
  import os
@@ -80,20 +157,36 @@ 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
- # Calculate color opacity based on depth
84
- max_depth = 5 # Assume maximum depth for color calculation
85
- if current_depth >= max_depth:
86
- opacity_value = 128 # Minimum opacity (50%)
 
 
87
  else:
88
- opacity_value = int(255 * (1.0 - (current_depth * 0.6 / max_depth)))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
- opacity = format(opacity_value, '02x')
91
- node_color = f"{base_color}{opacity}"
92
 
93
- # Determine font color based on background brightness
94
- # If opacity_value is high (dark background), use white text
95
- # If opacity_value is low (light background), use black text
96
- font_color = 'white' if opacity_value > 180 else 'black'
97
 
98
  # Add the current node
99
  dot.node(
@@ -103,7 +196,7 @@ def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
103
  style='filled',
104
  fillcolor=node_color,
105
  fontcolor=font_color,
106
- fontsize='14',
107
  width='0.8',
108
  height='0.8'
109
  )
@@ -116,7 +209,7 @@ def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
116
  dot.edge(
117
  node_id,
118
  left_id,
119
- color='#666666',
120
  arrowsize='0.8'
121
  )
122
 
@@ -128,7 +221,7 @@ def generate_binary_tree_diagram(json_input: str, output_format: str) -> str:
128
  dot.edge(
129
  node_id,
130
  right_id,
131
- color='#666666',
132
  arrowsize='0.8'
133
  )
134
 
 
1
+ def add_binary_tree_nodes(node, current_depth=0):
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
 
157
  node_id = node.get('id', f'node_{current_depth}')
158
  node_label = node.get('label', 'Node')
159
 
160
+ # Calculate color for current depth using same logic as graph_generator_utils
161
+ lightening_factor = 0.12
162
+
163
+ # Convert base_color hex to RGB for interpolation
164
+ if not isinstance(base_color, str) or not base_color.startswith('#') or len(base_color) != 7:
165
+ base_color_safe = '#19191a' # Fallback to default dark if invalid
166
  else:
167
+ base_color_safe = base_color
168
+
169
+ base_r = int(base_color_safe[1:3], 16)
170
+ base_g = int(base_color_safe[3:5], 16)
171
+ base_b = int(base_color_safe[5:7], 16)
172
+
173
+ # Calculate current node color by blending towards white
174
+ current_r = base_r + int((255 - base_r) * current_depth * lightening_factor)
175
+ current_g = base_g + int((255 - base_g) * current_depth * lightening_factor)
176
+ current_b = base_b + int((255 - base_b) * current_depth * lightening_factor)
177
+
178
+ # Clamp values to 255 to stay within valid RGB range
179
+ current_r = min(255, current_r)
180
+ current_g = min(255, current_g)
181
+ current_b = min(255, current_b)
182
+
183
+ node_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
184
 
185
+ # Font color: white for dark nodes, black for very light nodes for readability
186
+ font_color = 'white' if current_depth * lightening_factor < 0.6 else 'black'
187
 
188
+ # Font size adjusts based on depth, ensuring a minimum size
189
+ font_size = max(9, 14 - (current_depth * 2))
 
 
190
 
191
  # Add the current node
192
  dot.node(
 
196
  style='filled',
197
  fillcolor=node_color,
198
  fontcolor=font_color,
199
+ fontsize=str(font_size),
200
  width='0.8',
201
  height='0.8'
202
  )
 
209
  dot.edge(
210
  node_id,
211
  left_id,
212
+ color='#4a4a4a', # Dark gray for lines
213
  arrowsize='0.8'
214
  )
215
 
 
221
  dot.edge(
222
  node_id,
223
  right_id,
224
+ color='#4a4a4a', # Dark gray for lines
225
  arrowsize='0.8'
226
  )
227