You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are different ways to visit all the nodes or search for a value in a binary tree. Tree traversalimplementations are often recursive since it's more elegant and concise. Let's explore them.
3
+
As mentioned before, there are different ways to visit all the nodes or search for a value in a binary tree. On this section we are going to focus on depth-first tree traversal. The implementations are recursive since it's more elegant and concise. Let's explore them.
4
4
5
5
== In Order Traversal
6
6
7
-
If you tree happens to be a binary search tree (BST), then could use "in order" traversal to get the values sorted in ascending order.
7
+
If you tree happens to be a binary search tree (BST), then could use "in order" traversal to get the values sorted in ascending order. To accomplish this, you have to visit the nodes in a `left-root-right` order.
This function goes recursively to the leftmost element and then yield that node, then we go to the right child (if any) and repeat the process. This will get us the values ordered.
31
31
32
-
Note the asterisk (`*`) in front of the function means that this functino is a generator that yield values.
33
-
34
-
.JavaScript Generators
35
-
****
36
-
37
-
JavaScript generators were added as part of ES6, they allow process possibly expensive operations one by one. You can convert any function into a generator by adding the asterisk in front and `yield`ing a value.
38
-
39
-
Then you can use `next()` to get the value and also `done` to know if it's the last value. Here are some examples:
Why do we care? Well, there are certain problems that you solve more optimally using one or another traversal method. For instance to get the size of a subtree, finding maximums/minimums, and so on.
27
+
28
+
Let's cover Breadth-first search (BFS) and Depth-first search (DFS).
29
+
30
+
== Breadth-First Search
31
+
32
+
Breadth-first search goeas wide (breadth) before going deep. Hence, the name. In other words, it goes level by level. It visits all the inmediate nodes or children and then move on to the children's children.
As you see, the BFS uses a <<Queue>> data structure. We enqueue all the children of the current node and then dequeue them as we visit them.
42
+
43
+
Note the asterisk (`*`) in front of the function means that this function is a generator that yield values.
44
+
45
+
.JavaScript Generators
46
+
****
47
+
48
+
JavaScript generators were added as part of ES6, they allow process possibly expensive operations one by one. You can convert any function into a generator by adding the asterisk in front and `yield`ing a value.
49
+
50
+
Then you can use `next()` to get the value and also `done` to know if it's the last value. Here are some examples:
Depth-First search goes deep before going wide. It means, that starting for the root it goes as deep as it can until it found a leaf node (node without children), then it visits all the remaing nodes that were in the path.
82
+
83
+
.Depth-First Search (DFS) Implementation with a Stack
0 commit comments