1
- ifndef::imagesdir[]
2
- :imagesdir: ../images
3
- :codedir: ../../src
4
- endif::[]
5
-
6
- = Self-balancing Binary Search Trees
1
+ [appendix]
2
+ [[b-self-balancing-binary-search-trees]]
3
+ == Self-balancing Binary Search Trees
7
4
8
5
Binary Search Trees (BST) are an excellent data structure to find elements very fast _O(log n)_.
9
6
However, when the BST branches have different branch sizes, then the performance suffers.
@@ -31,12 +28,13 @@ As you might notice, we balanced the tree in the example by doing a rotation.
31
28
To be more specific we rotated node `1` to the left to balance the tree.
32
29
Let's examine all the possible rotation we can do to balance a tree.
33
30
34
- == Tree Rotations
31
+ [[tree-rotations]]
32
+ === Tree Rotations
35
33
(((Tree Rotations)))
36
34
We can do single rotations left and right and also we can do double rotations.
37
35
Let's go one by one.
38
36
39
- === Single Right Rotation
37
+ ==== Single Right Rotation
40
38
41
39
Right rotation moves a node on the right as a child of another node.
42
40
@@ -48,7 +46,7 @@ So, we move node 3 as the right child of the previous child.
48
46
.Single right rotation implementation
49
47
[source, javascript]
50
48
----
51
- include::{codedir} /data-structures/trees/tree-rotations.js[tag=rightRotation]
49
+ include::../src /data-structures/trees/tree-rotations.js[tag=rightRotation]
52
50
----
53
51
54
52
.In the `rightRotation` we identify 3 nodes:
@@ -64,7 +62,7 @@ Take a look at the implementation.
64
62
.Swap Parent and Child Implementation
65
63
[source, javascript]
66
64
----
67
- include::{codedir} /data-structures/trees/tree-rotations.js[tag=swapParentChild]
65
+ include::../src /data-structures/trees/tree-rotations.js[tag=swapParentChild]
68
66
----
69
67
70
68
After `swapParentChild`, we have the following:
@@ -93,14 +91,14 @@ Check out the <<Single Right Rotation, rightRotation>> implementation again. It
93
91
This rotation is also known as `RR rotation`.
94
92
95
93
96
- === Single Left Rotation
94
+ ==== Single Left Rotation
97
95
98
96
Left rotation is similar to the `rightRotation` we explained above.
99
97
100
98
.Single left rotation implementation
101
99
[source, javascript]
102
100
----
103
- include::{codedir} /data-structures/trees/tree-rotations.js[tag=leftRotation]
101
+ include::../src /data-structures/trees/tree-rotations.js[tag=leftRotation]
104
102
----
105
103
106
104
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.
@@ -111,36 +109,37 @@ If you are curious about the `setRightAndUpdateParent` and `setLeftAndUpdatePare
111
109
.Set and update parent implementation
112
110
[source, javascript]
113
111
----
114
- include::{codedir} /data-structures/trees/binary-tree-node.js[tag=setAndUpdateParent]
112
+ include::../src /data-structures/trees/binary-tree-node.js[tag=setAndUpdateParent]
115
113
----
116
114
117
115
You can also check out the full
118
116
https://github.com/amejiarosario/dsa.js/blob/adfd8a660bbe0a7068fd7881aff9f51bdb9f92ae/src/data-structures/trees/binary-tree-node.js#L9[binary tree node implementation].
119
117
120
- === Left Right Rotation
118
+ ==== Left Right Rotation
121
119
122
120
This time are we going to do a double rotation.
123
121
124
122
.Left-Right rotation implementation
125
123
[source, javascript]
126
124
----
127
- include::{codedir} /data-structures/trees/tree-rotations.js[tag=leftRightRotation]
125
+ include::../src /data-structures/trees/tree-rotations.js[tag=leftRightRotation]
128
126
----
129
127
130
128
As you can see we do a left and then a right rotation. This rotation is also known as `LR rotation`
131
129
132
- === Right Left Rotation
130
+ ==== Right Left Rotation
133
131
134
132
Very similar to `leftRightRotation`. The difference is that we rotate right and then left.
135
133
136
134
.Right-Left rotation implementation
137
135
[source, javascript]
138
136
----
139
- include::{codedir} /data-structures/trees/tree-rotations.js[tag=rightLeftRotation]
137
+ include::../src /data-structures/trees/tree-rotations.js[tag=rightLeftRotation]
140
138
----
141
139
142
140
This rotation is also referred to as `RL rotation`.
143
141
144
- == Self-balancing trees implementations
142
+ === Self-balancing trees implementations
145
143
146
144
So far, we have study how to make tree rotations which are the basis for self-balancing trees. There are different implementations of self-balancing trees such a Red-Black Tree and AVL Tree.
145
+
0 commit comments