Skip to content

Commit

Permalink
Fixed binary trees
Browse files Browse the repository at this point in the history
  • Loading branch information
karkkieila authored Sep 18, 2024
1 parent 8e864f4 commit da5ca73
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions data/part-11/4-more-recursion-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ After this section

The real advantages of recursion become evident when we come across problems where iterative solutions are difficult to write. Let's take a look at _binary trees_, for instance. A binary tree is a branched structure where we have nodes, and at each node the structure branches, at most, into two child branches with nodes of their own. A binary tree could then look like this (computer science is often considered a branch of the natural sciences, but our understanding of trees is a little topsy-turvy, as you'll notice):

<img src="11_4_1.png">
<img src="11_4_1_2.png">

Binary trees should at least theoretically be easy to handle recursively: if we want to perform some operation on every node in the tree, our algorithm simply needs to

1. Process the current node
2. Call itself on the child node on the left
3. Call itself on the child node on the right

<img src="11_4_2.png">
<img src="11_4_2_2e.png">

As you can see from the image above, both the left and right "subtrees" are fully fledged binary trees themselves, and the only node left outside the recursive calls is the parent node, which is processed in step 1, before calling the function recursively. So, we can be sure that when the execution of the function finishes, each node has been visited exactly once.

Expand Down

0 comments on commit da5ca73

Please sign in to comment.