22/**
33 * Swap parent's child
44 *
5- *
65 * @example Child on the left side (it also work for the right side)
76 *
87 * p = parent
@@ -34,7 +33,7 @@ function swapParentChild(oldChild, newChild, parent) {
3433/**
3534 * Single Left Rotation (LL Rotation)
3635 *
37- * @example : tree with values 1-2-3-4
36+ * @example : #1 tree with values 1-2-3-4
3837 *
3938 * 1 1
4039 * \ \
@@ -43,21 +42,34 @@ function swapParentChild(oldChild, newChild, parent) {
4342 * 3 2* 4
4443 * \
4544 * 4
46- * @param {TreeNode } node
47- * @returns {TreeNode } new parent after the rotation
45+ *
46+ * @example : #2 left rotation
47+ *
48+ * 1 1
49+ * \ \
50+ * 4* 16
51+ * / \ / \
52+ * 2 16 -- left-rotation(4) -> 4 32
53+ * / \ / \ \
54+ * 8 32 2 8 64
55+ * \
56+ * 64
57+ * @param {TreeNode } node current node to rotate (e.g. 4)
58+ * @returns {TreeNode } new parent after the rotation (e.g. 16)
4859 */
4960function leftRotation ( node ) {
50- const newParent = node . right ; // E.g., node 3
61+ const newParent = node . right ; // E.g., node 16
5162 const grandparent = node . parent ; // E.g., node 1
63+ const previousLeft = newParent . left ; // E.g., node 8
5264
53- // swap node 1 left child from 2 to 3.
65+ // swap parent of node 4 from node 1 to node 16
5466 swapParentChild ( node , newParent , grandparent ) ;
5567
56- // Update node 3 left child to be 2 , and
57- // updates node 2 parent to be node 3 (instead of 1).
68+ // Update node 16 left child to be 4 , and
69+ // updates node 4 parent to be node 16 (instead of 1).
5870 newParent . setLeftAndUpdateParent ( node ) ;
59- // remove node 2 left child (previouly was node 3 )
60- node . setRightAndUpdateParent ( null ) ;
71+ // set node4 right child to be previousLeft ( node 8 )
72+ node . setRightAndUpdateParent ( previousLeft ) ;
6173
6274 return newParent ;
6375}
@@ -66,8 +78,7 @@ function leftRotation(node) {
6678// tag::rightRotation[]
6779/**
6880 * Single Right Rotation (RR Rotation)
69- *
70- * @example rotate node 3 to the right
81+ * @example : #1 rotate node 3 to the right
7182 *
7283 * 4 4
7384 * / /
@@ -77,22 +88,34 @@ function leftRotation(node) {
7788 * /
7889 * 1
7990 *
91+ * @example : #2 rotate 16 to the right and preserve nodes
92+ * 64 64
93+ * / /
94+ * 16* 4
95+ * / \ / \
96+ * 4 32 -- right-rotation(16) --> 2 16
97+ * / \ / / \
98+ * 2 8 1 8 32
99+ * /
100+ * 1
101+ *
80102 * @param {TreeNode } node
81- * this is the node we want to rotate to the right. (E.g., node 3 )
82- * @returns {TreeNode } new parent after the rotation (E.g., node 2 )
103+ * this is the node we want to rotate to the right. (E.g., node 16 )
104+ * @returns {TreeNode } new parent after the rotation (E.g., node 4 )
83105 */
84106function rightRotation ( node ) {
85- const newParent = node . left ; // E.g., node 2
86- const grandparent = node . parent ; // E.g., node 4
107+ const newParent = node . left ; // E.g., node 4
108+ const grandparent = node . parent ; // E.g., node 64
109+ const previousRight = newParent . right ; // E.g., node 8
87110
88- // swap node 4 left children (node 3 ) with node 2 .
111+ // swap node 64's left children (node 16 ) with node 4 (newParent) .
89112 swapParentChild ( node , newParent , grandparent ) ;
90113
91- // update right child on node 2 to be node 3 ,
92- // also make node 2 the new parent of node 3 .
114+ // update node 4's right child to be node 16 ,
115+ // also make node 4 the new parent of node 16 .
93116 newParent . setRightAndUpdateParent ( node ) ;
94- // remove node 3 left child (so it doesn't point to node 2)
95- node . setLeftAndUpdateParent ( null ) ;
117+ // Update 16's left child to be the `previousRight` node.
118+ node . setLeftAndUpdateParent ( previousRight ) ;
96119
97120 return newParent ;
98121}
0 commit comments