Skip to content

Commit d686bdd

Browse files
build(book): optimize for different formats
1 parent 8415302 commit d686bdd

File tree

104 files changed

+1311
-2266
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+1311
-2266
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ jobs: # a collection of steps
104104

105105
- run:
106106
name: generate PDF
107-
command: cd book/config && make VERSION="$(npx -c 'echo "$npm_package_version"')" pdf
107+
command: cd book/config && make VERSION="$(npx -c 'echo "$npm_package_version"')"
108108

109109
- store_artifacts:
110110
path: book/dist

.gitignore

+7-8
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ local.properties
7272
# Ruby
7373
######################
7474
/.bundle/
75-
/Gemfile.lock
7675

7776
######################
7877
# Package Files
@@ -136,13 +135,13 @@ Desktop.ini
136135
# ln -s ~/OneDrive/Authoring/dsaJS/asciidoc/book/fonts .
137136
# ln -s ~/OneDrive/Authoring/dsaJS/asciidoc/book/images .
138137
######################
139-
Gemfile
140-
Gemfile.lock
141-
Makefile
142-
_conf
143-
_resources
144-
extensions
145-
fonts
138+
# Gemfile
139+
# Gemfile.lock
140+
# Makefile
141+
# _conf
142+
# _resources
143+
# extensions
144+
# fonts
146145

147146

148147
######################

README.md

+51-50
Large diffs are not rendered by default.

book/A-time-complexity-cheatsheet.asc

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[appendix]
2+
[[a-time-complexity-cheatsheet]]
3+
== Cheatsheet
4+
5+
This section summerize what we are going to cover in the rest of this book.
6+
7+
=== Runtimes
8+
9+
include::content/part01/big-o-examples.asc[tag=table]
10+
11+
include::content/part01/algorithms-analysis.asc[tag=table]
12+
13+
=== Linear Data Structures
14+
15+
include::content/part02/array-vs-list-vs-queue-vs-stack.asc[tag=table]
16+
17+
=== Trees and Maps Data Structures
18+
19+
This section covers Binary Search Tree (BST) time complexity (Big O).
20+
21+
include::content/part03/time-complexity-graph-data-structures.asc[tag=table]
22+
23+
include::content/part03/graph.asc[tag=table]
24+
25+
=== Sorting Algorithms
26+
27+
include::content/part04/sorting-algorithms.asc[tag=table]
28+
29+
// // https://algs4.cs.princeton.edu/cheatsheet/
30+
// // http://bigocheatsheet.com/
31+
32+
// // https://en.wikipedia.org/wiki/Timsort (Tim Peters)
33+
// // https://bugs.python.org/file4451/timsort.txt
34+
// // https://www.youtube.com/watch?v=emeME__917E&list=PLMCXHnjXnTntLcLmA5SqhMspm7burHi3m
35+
36+
// // https://en.wikipedia.org/wiki/Sorting_algorithm
37+
// // http://sorting.at/
38+
// // https://www.toptal.com/developers/sorting-algorithms
39+
// // https://www.infopulse.com/blog/timsort-sorting-algorithm/

book/chapters/tree-self-balancing-rotations.adoc book/B-self-balancing-binary-search-trees.asc

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
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
74

85
Binary Search Trees (BST) are an excellent data structure to find elements very fast _O(log n)_.
96
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.
3128
To be more specific we rotated node `1` to the left to balance the tree.
3229
Let's examine all the possible rotation we can do to balance a tree.
3330

34-
== Tree Rotations
31+
[[tree-rotations]]
32+
=== Tree Rotations
3533
(((Tree Rotations)))
3634
We can do single rotations left and right and also we can do double rotations.
3735
Let's go one by one.
3836

39-
=== Single Right Rotation
37+
==== Single Right Rotation
4038

4139
Right rotation moves a node on the right as a child of another node.
4240

@@ -48,7 +46,7 @@ So, we move node 3 as the right child of the previous child.
4846
.Single right rotation implementation
4947
[source, javascript]
5048
----
51-
include::{codedir}/data-structures/trees/tree-rotations.js[tag=rightRotation]
49+
include::../src/data-structures/trees/tree-rotations.js[tag=rightRotation]
5250
----
5351

5452
.In the `rightRotation` we identify 3 nodes:
@@ -64,7 +62,7 @@ Take a look at the implementation.
6462
.Swap Parent and Child Implementation
6563
[source, javascript]
6664
----
67-
include::{codedir}/data-structures/trees/tree-rotations.js[tag=swapParentChild]
65+
include::../src/data-structures/trees/tree-rotations.js[tag=swapParentChild]
6866
----
6967

7068
After `swapParentChild`, we have the following:
@@ -93,14 +91,14 @@ Check out the <<Single Right Rotation, rightRotation>> implementation again. It
9391
This rotation is also known as `RR rotation`.
9492

9593

96-
=== Single Left Rotation
94+
==== Single Left Rotation
9795

9896
Left rotation is similar to the `rightRotation` we explained above.
9997

10098
.Single left rotation implementation
10199
[source, javascript]
102100
----
103-
include::{codedir}/data-structures/trees/tree-rotations.js[tag=leftRotation]
101+
include::../src/data-structures/trees/tree-rotations.js[tag=leftRotation]
104102
----
105103

106104
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
111109
.Set and update parent implementation
112110
[source, javascript]
113111
----
114-
include::{codedir}/data-structures/trees/binary-tree-node.js[tag=setAndUpdateParent]
112+
include::../src/data-structures/trees/binary-tree-node.js[tag=setAndUpdateParent]
115113
----
116114

117115
You can also check out the full
118116
https://github.com/amejiarosario/dsa.js/blob/adfd8a660bbe0a7068fd7881aff9f51bdb9f92ae/src/data-structures/trees/binary-tree-node.js#L9[binary tree node implementation].
119117

120-
=== Left Right Rotation
118+
==== Left Right Rotation
121119

122120
This time are we going to do a double rotation.
123121

124122
.Left-Right rotation implementation
125123
[source, javascript]
126124
----
127-
include::{codedir}/data-structures/trees/tree-rotations.js[tag=leftRightRotation]
125+
include::../src/data-structures/trees/tree-rotations.js[tag=leftRightRotation]
128126
----
129127

130128
As you can see we do a left and then a right rotation. This rotation is also known as `LR rotation`
131129

132-
=== Right Left Rotation
130+
==== Right Left Rotation
133131

134132
Very similar to `leftRightRotation`. The difference is that we rotate right and then left.
135133

136134
.Right-Left rotation implementation
137135
[source, javascript]
138136
----
139-
include::{codedir}/data-structures/trees/tree-rotations.js[tag=rightLeftRotation]
137+
include::../src/data-structures/trees/tree-rotations.js[tag=rightLeftRotation]
140138
----
141139

142140
This rotation is also referred to as `RL rotation`.
143141

144-
== Self-balancing trees implementations
142+
=== Self-balancing trees implementations
145143

146144
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+
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,41 @@
1-
ifndef::imagesdir[]
2-
:imagesdir: ../images
3-
:codedir: ../../src
4-
endif::[]
5-
6-
= AVL Tree
1+
[appendix]
2+
[[c-avl-tree]]
3+
== AVL Tree
74
(((AVL Tree)))
85
(((Tree, AVL)))
96
AVL Tree is named after their inventors (**A**delson-**V**elsky and **L**andis).
107
This self-balancing tree keeps track of subtree sizes to know if a rebalance is needed or not.
118
We can compare the size of the left and right subtrees using a balance factor.
129

1310
[NOTE]
14-
====
11+
=====
1512
1613
The *balanced factor* on each node is calculated recursively as follows:
1714
1815
----
1916
Balance Factor = (left subtree height) - (right subtree height)
2017
----
2118
22-
====
19+
=====
2320

2421
The implementation will go in the BST node class.
2522
We will need two methods to calculate the left and right subtree, and with those, we can get the balance factor.
2623

2724
.Balance Factor methods on the BST node
2825
[source, javascript]
2926
----
30-
include::{codedir}/data-structures/trees/binary-tree-node.js[tag=avl, indent=0]
27+
include::../src/data-structures/trees/binary-tree-node.js[tag=avl, indent=0]
3128
----
3229

3330

34-
== Implementing AVL Tree
31+
=== Implementing AVL Tree
3532

3633
Implementing an AVL Tree is not too hard since it builds upon what we did in the Binary Search Tree.
3734

3835
.AVL Tree class
3936
[source, javascript]
4037
----
41-
include::{codedir}/data-structures/trees/avl-tree.js[tag=AvlTree]
38+
include::../src/data-structures/trees/avl-tree.js[tag=AvlTree]
4239
----
4340

4441
As you can see, the AVL tree inherits from the BST class.
@@ -48,7 +45,7 @@ This function checks if the tree is symmetrical after every change to the tree.
4845
.Balance Upstream for AVL tree
4946
[source, javascript]
5047
----
51-
include::{codedir}/data-structures/trees/avl-tree.js[tag=balanceUpstream]
48+
include::../src/data-structures/trees/avl-tree.js[tag=balanceUpstream]
5249
----
5350

5451
This function recursively goes from the modified node to the root checking if each node in between is balanced.
@@ -57,10 +54,10 @@ Now, let's examine how does the balancing works on AVL tree.
5754
.Balance method for AVL tree
5855
[source, javascript]
5956
----
60-
include::{codedir}/data-structures/trees/avl-tree.js[tag=balance]
57+
include::../src/data-structures/trees/avl-tree.js[tag=balance]
6158
----
6259

6360
The first thing we do is to see if one subtree is longer than the other.
6461
If so, then we check the children balance to determine if need a single or double rotation and in which direction.
6562

66-
You can review <<Tree Rotations>> in case you want a refresher.
63+
You can review <<b-self-balancing-binary-search-trees#tree-rotations>> in case you want a refresher.

0 commit comments

Comments
 (0)