Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e8b623

Browse files
bst
1 parent 70a87a0 commit 8e8b623

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

‎book/chapters/tree--self-balancing-bst.adoc‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5454
The `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
----
5962
include::{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`.

‎src/data-structures/trees/binary-tree-node.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class BinaryTreeNode {
1717
this.parentSide = null;
1818
}
1919

20+
// tag::setAndUpdateParent[]
2021
/**
2122
* Set a left node descendents.
2223
* Also, children get associated to parent.
@@ -40,6 +41,7 @@ class BinaryTreeNode {
4041
node.parentSide = RIGHT;
4142
}
4243
}
44+
// end::setAndUpdateParent[]
4345

4446
/**
4547
* Tell if is parent's left or right child

‎src/data-structures/trees/tree-rotations.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ function leftRotation(node) {
5050
const newParent = node.right;
5151
const grandparent = node.parent;
5252

53-
5453
swapParentChild(node, newParent, grandparent);
5554

5655
// do LL rotation
@@ -82,10 +81,13 @@ function rightRotation(node) {
8281
const newParent = node.left; // E.g., node 2
8382
const grandparent = node.parent; // E.g., node 4
8483

84+
// swap node 4 left children (node 3) with node 2.
8585
swapParentChild(node, newParent, grandparent);
8686

87-
// do RR rotation
87+
// update right child on node 2 to be node 3,
88+
// also make node 2 the new parent of node 3.
8889
newParent.setRightAndUpdateParent(node);
90+
// remove node 3 left child (so it doesn't point to node 2)
8991
node.setLeftAndUpdateParent(null);
9092

9193
return newParent;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /