Repository navigation
Expand file tree
/
Copy pathbinary-tree.php
More file actions
522 lines (414 loc) · 11.5 KB
/
Copy pathbinary-tree.php
File metadata and controls
522 lines (414 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<?php
/**
* For fun
*
* A binary tree is a hierarchical data structure where each node has at most two children,
* called the left child and the right child.
*
* This example inverts the tree, showing both the original and the inverted version in a visual representation.
*
*/
declare(strict_types=1);
/**
* A binary-tree node with an integer value and optional children.
*/
class Node {
public ?Node $left = null;
public ?Node $right = null;
/** Create a node without children. */
public function __construct( public int $value ) {}
}
/**
* Invert a tree in place without recursive calls.
* Time: O(N). Auxiliary space: O(N) in the worst case.
*/
function invertTreeIterative( ?Node $root ): ?Node {
if ( $root === null ) {
return null;
}
$stack = array( $root );
while ( $stack !== array() ) {
$current = array_pop( $stack );
[ $current->left, $current->right ] = array( $current->right, $current->left );
if ( $current->left !== null ) {
$stack[] = $current->left;
}
if ( $current->right !== null ) {
$stack[] = $current->right;
}
}
return $root;
}
/**
* Return path-to-value pairs, preserving left/right structure for verification.
*/
function treeSnapshot( ?Node $root ): array {
$values = array();
$stack = array( array( $root, 'root' ) );
while ( $stack !== array() ) {
[ $node, $path ] = array_pop( $stack );
if ( $node === null ) {
continue;
}
$values[$path] = $node->value;
$stack[] = array( $node->right, $path . '.R' );
$stack[] = array( $node->left, $path . '.L' );
}
return $values;
}
/**
* Escape text for HTML and SVG output.
*/
function escape( string $text ): string {
return htmlspecialchars( $text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' );
}
/**
* Render a connected SVG diagram and an accessible textual description.
*/
function renderTree( ?Node $root, string $id, string $title ): string {
if ( $root === null ) {
return '<p>Empty tree.</p>';
}
// Split each parent's horizontal interval equally, retaining missing slots.
$stack = array( array( $root, 0, 640, 0, null, null, 'Root' ) );
$edges = '';
$nodes = '';
$descriptions = array();
$maxDepth = 0;
while ( $stack !== array() ) {
[ $node, $min, $max, $depth, $parentX, $parentY, $side ] = array_pop( $stack );
$x = ( $min + $max ) / 2;
$y = 48 + $depth * 110;
$maxDepth = max( $maxDepth, $depth );
$empty = $node === null;
$class = $empty ? 'edge empty-edge' : 'edge';
if ( $parentX !== null ) {
$labelX = ( $parentX + $x ) / 2;
$labelY = ( $parentY + $y ) / 2;
$edges .=
'<line class="' .
$class .
'" x1="' .
$parentX .
'" y1="' .
( $parentY + 24 ) .
'" x2="' .
$x .
'" y2="' .
( $y - 24 ) .
'" />';
$edges .= '<text class="edge-label" x="' . $labelX . '" y="' . $labelY . '">' . escape( $side ) . '</text>';
}
$nodes .=
'<circle class="' . ( $empty ? 'empty-node' : 'node' ) . '" cx="' . $x . '" cy="' . $y . '" r="24" />';
$nodes .=
'<text class="node-label" x="' .
$x .
'" y="' .
$y .
'">' .
( $empty ? '∅' : escape( (string) $node->value ) ) .
'</text>';
if ( $empty ) {
continue;
}
$descriptions[] =
'Node ' .
$node->value .
': left ' .
( $node->left === null ? 'empty' : $node->left->value ) .
', right ' .
( $node->right === null ? 'empty' : $node->right->value ) .
".\n";
if ( $node->left !== null || $node->right !== null ) {
$stack[] = array( $node->right, $x, $max, $depth + 1, $x, $y, 'R' );
$stack[] = array( $node->left, $min, $x, $depth + 1, $x, $y, 'L' );
}
}
$description = 'Root: ' . $root->value . '. ' . implode( ' ', $descriptions );
$height = 96 + $maxDepth * 110;
return '<svg viewBox="0 0 640 ' .
$height .
'" role="img" aria-labelledby="' .
escape( $id ) .
'-title ' .
escape( $id ) .
'-desc">' .
'<title id="' .
escape( $id ) .
'-title">' .
escape( $title ) .
'</title>' .
'<desc id="' .
escape( $id ) .
'-desc">' .
escape( $description ) .
'</desc>' .
$edges .
$nodes .
'</svg><details><summary>Text description</summary><p>' .
nl2br( escape( $description ) ) .
'</p></details>';
}
/**
* Build a tree from level-order integers/nulls; null parents consume no child slots.
*/
function buildTree( array $values ): ?Node {
foreach ( $values as $value ) {
if ( $value !== null && ! is_int( $value ) ) {
throw new InvalidArgumentException( 'Tree values must be integers or null.' );
}
}
$values = array_values( $values );
$root = isset( $values[0] ) ? new Node( $values[0] ) : null;
$queue = $root === null ? array() : array( $root );
$index = 1;
for ( $head = 0; $head < count( $queue ); $head++ ) {
foreach ( array( 'left', 'right' ) as $side ) {
if ( $index >= count( $values ) ) {
break;
}
$value = $values[$index++];
if ( $value !== null ) {
$child = new Node( $value );
$queue[$head]->{$side} = $child;
$queue[] = $child;
}
}
}
foreach ( array_slice( $values, $index ) as $value ) {
if ( $value !== null ) {
throw new InvalidArgumentException( 'A non-empty node has no parent.' );
}
}
return $root;
}
/**
* Serialize in level order, retaining interior nulls and omitting trailing nulls.
*/
function treeValues( ?Node $root ): array {
$values = array();
$queue = $root === null ? array() : array( $root );
for ( $head = 0; $head < count( $queue ); $head++ ) {
$node = $queue[$head];
$values[] = $node === null ? null : $node->value;
if ( $node !== null ) {
$queue[] = $node->left;
$queue[] = $node->right;
}
}
while ( $values !== array() && end( $values ) === null ) {
array_pop( $values );
}
return $values;
}
/**
* Verify mirrored paths and double inversion independently of example values.
*/
function verifyInversion( ?Node $invertedRoot, array $originalSnapshot ): void {
$expected = array();
foreach ( $originalSnapshot as $path => $value ) {
$expected[strtr( $path, array(
'L' => 'R',
'R' => 'L',
) )] = $value;
}
$actual = treeSnapshot( $invertedRoot );
ksort( $expected );
ksort( $actual );
if ( $actual !== $expected || invertTreeIterative( null ) !== null ) {
throw new RuntimeException( 'Tree inversion failed the mirrored structure check.' );
}
invertTreeIterative( $invertedRoot );
$restored = treeSnapshot( $invertedRoot ) === $originalSnapshot;
invertTreeIterative( $invertedRoot );
if ( ! $restored ) {
throw new RuntimeException( 'Inverting twice did not restore the original tree.' );
}
}
// Edit this level-order array to try another tree. Use null for missing children.
$input = array( 4, 2, 7, 1, 3, 6, 9 );
$input = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 );
$root = buildTree( $input );
$originalSnapshot = treeSnapshot( $root );
$inputValues = treeValues( $root );
$originalDiagram = renderTree( $root, 'original', 'Original tree' );
invertTreeIterative( $root );
verifyInversion( $root, $originalSnapshot );
$outputValues = treeValues( $root );
$invertedDiagram = renderTree( $root, 'inverted', 'Inverted tree' );
// Display a nice visual output.
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Compare a binary tree before and after iterative inversion, with labeled branches and empty child positions.">
<title>Binary tree inversion — visual comparison</title>
<style>
:root {
color-scheme: light dark;
--background: #f4f7fb;
--surface: #ffffff;
--text: #182638;
--muted: #475569;
--border: #cbd5e1;
--accent: #1e40af;
--node-fill: #eff6ff;
--success: #166534;
--radius: 1rem;
--space: 1rem;
--font: system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
:root {
--background: #101827;
--surface: #192537;
--text: #f1f5f9;
--muted: #cbd5e1;
--border: #52647c;
--accent: #93c5fd;
--node-fill: #243b59;
--success: #86efac;
}
}
* {
box-sizing: border-box;
}
body {
margin: 0;
background: var(--background);
color: var(--text);
font-family: var(--font);
line-height: 1.6;
}
main {
max-width: 85rem;
margin: auto;
padding: calc(var(--space) * 2) var(--space);
}
h1 {
font-size: 2rem;
line-height: 1.2;
}
h2 {
margin-top: 0;
font-size: 1.25rem;
}
p {
color: var(--muted);
}
code {
overflow-wrap: anywhere;
}
.comparison {
display: grid;
gap: var(--space);
margin-block: calc(var(--space) * 2);
}
.tree-panel {
min-width: 0;
padding: var(--space);
border: 1px solid var(--border);
border-radius: var(--radius);
background: var(--surface);
}
svg {
display: block;
width: 100%;
height: auto;
}
.edge {
stroke: var(--muted);
stroke-width: 2;
}
.empty-edge,
.empty-node {
stroke-dasharray: 5 4;
}
.node {
fill: var(--node-fill);
stroke: var(--accent);
stroke-width: 2;
}
.empty-node {
fill: var(--surface);
stroke: var(--muted);
stroke-width: 2;
}
.node-label,
.edge-label {
fill: var(--text);
text-anchor: middle;
dominant-baseline: middle;
font-family: var(--font);
}
.node-label {
font-size: 20px;
font-weight: 700;
}
.edge-label {
font-size: 16px;
font-weight: 600;
stroke: var(--surface);
stroke-width: 6;
paint-order: stroke;
}
summary {
cursor: pointer;
padding-block: var(--space);
color: var(--accent);
}
:focus-visible {
outline: 3px solid var(--accent);
outline-offset: 4px;
}
.checks {
color: var(--success);
font-weight: 600;
}
.skip-link {
position: absolute;
top: 0;
left: var(--space);
padding: var(--space);
background: var(--surface);
color: var(--accent);
transform: translateY(-110%);
}
.skip-link:focus {
transform: translateY(0);
}
@media (min-width: 60rem) {
.comparison {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
</style>
</head>
<body>
<a class="skip-link" href="#main-content">Skip to comparison</a>
<main id="main-content" tabindex="-1">
<header>
<h1>Binary tree inversion</h1>
<p>Every node swaps its left and right children. The root is the first input value, not necessarily 4.</p>
</header>
<p><strong>Legend:</strong> L = left child · R = right child · dashed ∅ = empty child slot. Leaves have no children.</p>
<div class="comparison">
<section class="tree-panel" aria-labelledby="original-heading">
<h2 id="original-heading">Before — original tree</h2>
<p>Input: <code><?= escape(json_encode($inputValues, JSON_THROW_ON_ERROR)) ?></code></p>
<?= $originalDiagram ?>
</section>
<section class="tree-panel" aria-labelledby="inverted-heading">
<h2 id="inverted-heading">After — inverted tree</h2>
<p>Output: <code><?= escape(json_encode($outputValues, JSON_THROW_ON_ERROR)) ?></code></p>
<?= $invertedDiagram ?>
</section>
</div>
<p>Arrays are read level by level, left to right. Use <code>null</code> for missing children; missing parents do not consume child slots.</p>
<p class="checks">✓ Structure checks passed: mirrored paths, empty input, and original restored after two inversions.</p>
</main>
</body>
</html>