@@ -52,11 +52,84 @@ include::{codedir}/data-structures/trees/tree-rotations.js[tag=rightRotation]
5252- `grandparent` this the current's node parent. E.g. `node 4`.
5353
5454The `swapParentChild` as it name says, swap the children.
55+ For our example, it swaps `node 4`'s left children from `node 3` to `node 2`.
56+
57+ Take a look at the implementation
5558
5659.Swap Parent and Child Implementation
5760[source, javascript]
5861----
5962include::{codedir}/data-structures/trees/tree-rotations.js[tag=swapParentChild]
6063----
6164
65+ After `swapParentChild`, we have the following:
66+ ----
67+ 4
68+ /
69+ 2 - 3*
70+ /
71+ 1
72+ ----
73+
74+ Still not quite what we want.
75+ So, `newParent.setRightAndUpdateParent(node)` will make `node 3` the right child of `node 2`.
76+ Finally, we remove left child of `node 3` to be `null`.
77+
78+ ----
79+ 4
80+ /
81+ 2
82+ / \
83+ 1 3*
84+ ----
85+
86+ Check out the <<Single Right Rotation, rightRotation>> implementation again. It should make more sense to you now.
87+
88+ This rotation is also known as `RR rotation`.
89+
90+
91+ === Single Left Rotation
92+
93+ Left rotation is similar to the `rightRotation` we explained above.
94+
95+ .Single left rotation implementation
96+ [source, javascript]
97+ ----
98+ include::{codedir}/data-structures/trees/tree-rotations.js[tag=leftRotation]
99+ ----
100+
101+ As you can see, this function is just the opposite of `rightRotation`. Where ever we used the right now we use the left here and vice versa.
102+ This rotation is also known as `LL rotation`.
103+
104+ If you are curious about the `setRightAndUpdateParent`. Here's the implementation:
105+
106+ .Set and update parent implementation
107+ [source, javascript]
108+ ----
109+ include::{codedir}/data-structures/trees/binary-tree-node.js[tag=setAndUpdateParent]
110+ ----
111+
112+ === Left Right Rotation
113+
114+ This time are we going to do a double rotation.
115+
116+ .Left-Right rotation implementation
117+ [source, javascript]
118+ ----
119+ include::{codedir}/data-structures/trees/tree-rotations.js[tag=leftRightRotation]
120+ ----
121+
122+ As you can see we do a left and then a right rotation. This is also called `LR rotation`
123+
124+ === Right Left Rotation
125+
126+ Very similar to `leftRightRotation`. The difference is that we rotate right and then left.
127+
128+
129+ .Right-Left rotation implementation
130+ [source, javascript]
131+ ----
132+ include::{codedir}/data-structures/trees/tree-rotations.js[tag=rightLeftRotation]
133+ ----
62134
135+ This rotation is also refered as `RL rotation`.
0 commit comments