File tree Expand file tree Collapse file tree 2 files changed +22
-16
lines changed Expand file tree Collapse file tree 2 files changed +22
-16
lines changed Original file line number Diff line number Diff line change @@ -4,15 +4,18 @@ import replace_node from '../deletion/replace_node.js';
44import swap_color from './swap_color.js' ;
55
66/**
7- * Swap pointers and colors of a node and its left child.
7+ * Swap pointers and colors of a node and its left child B with one constraint:
8+ * - B's right child is a leaf
9+ *
10+ * NOTE: This constraint is implied because B is A's in-subtree predecessor.
811 *
912 * p p
1013 * | |
1114 * -A -B
1215 * / \ / \
1316 * +B c -> +A c
1417 * / \ / \
15- * a b a b
18+ * a - a -
1619 *
1720 * @param {Node } A - The node.
1821 * @return {Node } The node B.
@@ -23,7 +26,7 @@ const swap_left = (A) => {
2326 const B = A . left ;
2427 assert ( B instanceof Node ) ;
2528 const a = B . left ;
26- const b = B . right ;
29+ assert ( B . right === null ) ;
2730 const c = A . right ;
2831
2932 if ( A . parent === null ) {
@@ -34,8 +37,7 @@ const swap_left = (A) => {
3437
3538 A . parent = B ;
3639
37- A . right = b ;
38- if ( b !== null ) b . parent = A ;
40+ A . right = null ;
3941 B . right = c ;
4042 if ( c !== null ) c . parent = B ;
4143
Original file line number Diff line number Diff line change @@ -3,13 +3,19 @@ import Node from '../types/Node.js';
33import swap_color from './swap_color.js' ;
44
55/**
6- * Swap pointers and colors of two NON-ADJACENT nodes.
6+ * Swap pointers and colors of two NON-ADJACENT nodes with three constraints:
7+ * - B is not the root
8+ * - B is its parent right child
9+ * - B's right child is a leaf
710 *
8- * p q q p
9- * | | | |
11+ * NOTE: These three constraints are implied because B is A's in-subtree
12+ * predecessor without being A's left child.
13+ *
14+ * p q q p
15+ * | \ \ |
1016 * -A +B +A -B
1117 * / \ / \ / \ / \
12- * u v x y -> x y u v
18+ * u v x - -> x - u v
1319 *
1420 * @param {Node } A - The first node.
1521 * @param {Node } B - The second node.
@@ -23,27 +29,25 @@ const swap_non_adjacent = (A, B) => {
2329 const v = A . right ;
2430 const q = B . parent ;
2531 const x = B . left ;
26- const y = B . right ;
32+ assert ( B . right === null ) ;
33+ assert ( q !== null ) ;
34+ assert ( B === q . right ) ;
2735
2836 if ( p !== null ) {
2937 if ( A === p . left ) p . left = B ;
3038 else p . right = B ;
3139 }
3240
33- if ( q !== null ) {
34- if ( B === q . right ) q . right = A ;
35- else q . left = A ;
36- }
41+ q . right = A ;
3742
3843 A . parent = q ;
3944 A . left = x ;
40- A . right = y ;
45+ A . right = null ;
4146 B . parent = p ;
4247 B . left = u ;
4348 B . right = v ;
4449
4550 if ( x !== null ) x . parent = A ;
46- if ( y !== null ) y . parent = A ;
4751 if ( u !== null ) u . parent = B ;
4852 if ( v !== null ) v . parent = B ;
4953
You can’t perform that action at this time.
0 commit comments