{"id":410,"date":"2024-04-14T21:19:00","date_gmt":"2024-04-14T20:19:00","guid":{"rendered":"https:\/\/noiseonthenet.space\/noise\/?p=410"},"modified":"2024-04-18T17:27:47","modified_gmt":"2024-04-18T16:27:47","slug":"climbing-a-binary-tree","status":"publish","type":"post","link":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/","title":{"rendered":"Climbing a (binary) Tree"},"content":{"rendered":"<div id=\"orgdee7174\" class=\"figure\"> <p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/annie-spratt-gNI4_88t9Rs-unsplash_reduced.jpg?ssl=1\" alt=\"annie-spratt-gNI4_88t9Rs-unsplash_reduced.jpg\" \/> <\/p> <\/div>\n\n<p> Photo by <a href=\"https:\/\/unsplash.com\/@anniespratt?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\">Annie Spratt<\/a> on <a href=\"https:\/\/unsplash.com\/photos\/girl-sitting-on-tree-branch-during-daytime-gNI4_88t9Rs?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\">Unsplash<\/a> <\/p>\n\n<p> In this post I will show how to transform a recursive depth first traversal function into an iterator for a binary tree <\/p>\n\n<p> The complete source code for this post is available <a href=\"https:\/\/github.com\/noiseOnTheNet\/post012_climbing_a_tree\">here<\/a>. <\/p>\n\n<p> After a couple of digressions we&rsquo;re back to my beloved tree; here are the link of our journey so far: <\/p>\n\n<ol class=\"org-ol\">\n<li><a href=\"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/\">Growing a (binary)Tree<\/a><\/li>\n<li><a href=\"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-sorting-tree\/\">Growing a (sorting) Tree<\/a><\/li>\n<li><a href=\"https:\/\/noiseonthenet.space\/noise\/2024\/03\/stacking-bits\/\">Stacking Bits<\/a><\/li>\n<li><a href=\"https:\/\/noiseonthenet.space\/noise\/2024\/03\/prime-time\/\">Prime Time<\/a><\/li>\n<\/ol>\n\n<p> While in previous posts we traversed and print the content of the tree data structure with a recursive function, this may not be very convenient in the general case: what can we do to make it more general? <\/p>\n\n<ol class=\"org-ol\">\n<li>pass a lambda argument to the recursive traversal function<\/li>\n<li>transform the traversal function into an iterator coroutine<\/li>\n<\/ol>\n\n<p> The second approach leverage the whole Rust iterator interface, providing an easy connection with other data structures. <\/p>\n<div id=\"outline-container-orgd6942ec\" class=\"outline-2\">\n<h2 id=\"orgd6942ec\">Fixing our tree<\/h2>\n<div class=\"outline-text-2\" id=\"text-orgd6942ec\">\n<p> Let&rsquo;s first recall our previous implementation with some small fixes: first is to have a public tree interface which clearly separates from nodes <\/p>\n\n<div class=\"org-src-container\">\n<label class=\"org-src-name\"><em><\/em><\/label>\n<pre class=\"src src-rust\" id=\"nil\"><span style=\"color: #51afef;\">use<\/span> <span style=\"color: #a9a1e1;\">std<\/span>::<span style=\"color: #a9a1e1;\">fmt<\/span>::<span style=\"color: #ECBE7B;\">Debug<\/span>;\n\n<span style=\"color: #51afef; font-weight: bold;\">#<\/span><span style=\"color: #51afef; font-weight: bold;\">[<\/span><span style=\"color: #51afef; font-weight: bold;\">derive<\/span><span style=\"color: #c678dd; font-weight: bold;\">(<\/span><span style=\"color: #51afef; font-weight: bold;\">Debug<\/span><span style=\"color: #c678dd; font-weight: bold;\">)<\/span><span style=\"color: #51afef; font-weight: bold;\">]<\/span>\n<span style=\"color: #51afef;\">pub<\/span> <span style=\"color: #51afef;\">struct<\/span> <span style=\"color: #ECBE7B;\">Node<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n    <span style=\"color: #dcaeea;\">value<\/span> : <span style=\"color: #ECBE7B;\">T<\/span>,\n    <span style=\"color: #dcaeea;\">left<\/span> : <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Box<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Node<\/span><span style=\"color: #a9a1e1;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #a9a1e1;\">&gt;<\/span><span style=\"color: #98be65;\">&gt;<\/span><span style=\"color: #c678dd;\">&gt;<\/span>,\n    <span style=\"color: #dcaeea;\">right<\/span> : <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Box<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Node<\/span><span style=\"color: #a9a1e1;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #a9a1e1;\">&gt;<\/span><span style=\"color: #98be65;\">&gt;<\/span><span style=\"color: #c678dd;\">&gt;<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n\n<span style=\"color: #51afef;\">impl<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">Node<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">new<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #dcaeea;\">value<\/span> : <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #c678dd;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">Node<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #c678dd;\">&gt;{<\/span>\n        <span style=\"color: #ECBE7B;\">Node<\/span><span style=\"color: #98be65;\">{<\/span>\n            value,\n            <span style=\"color: #dcaeea;\">left<\/span> : <span style=\"color: #ECBE7B;\">None<\/span>,\n            <span style=\"color: #dcaeea;\">right<\/span> : <span style=\"color: #ECBE7B;\">None<\/span>\n        <span style=\"color: #98be65;\">}<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n\n<span style=\"color: #51afef; font-weight: bold;\">#<\/span><span style=\"color: #51afef; font-weight: bold;\">[<\/span><span style=\"color: #51afef; font-weight: bold;\">derive<\/span><span style=\"color: #c678dd; font-weight: bold;\">(<\/span><span style=\"color: #51afef; font-weight: bold;\">Debug,Default<\/span><span style=\"color: #c678dd; font-weight: bold;\">)<\/span><span style=\"color: #51afef; font-weight: bold;\">]<\/span>\n<span style=\"color: #51afef;\">pub<\/span> <span style=\"color: #51afef;\">struct<\/span> <span style=\"color: #ECBE7B;\">Tree<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n    <span style=\"color: #dcaeea;\">root<\/span>: <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Box<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Node<\/span><span style=\"color: #a9a1e1;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #a9a1e1;\">&gt;<\/span><span style=\"color: #98be65;\">&gt;<\/span><span style=\"color: #c678dd;\">&gt;<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n\n<span style=\"color: #51afef;\">impl<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">Tree<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">new<\/span><span style=\"color: #c678dd;\">()<\/span> -&gt; <span style=\"color: #ECBE7B;\">Tree<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #c678dd;\">&gt;{<\/span>\n        <span style=\"color: #ECBE7B;\">Tree<\/span><span style=\"color: #98be65;\">{<\/span>\n            <span style=\"color: #dcaeea;\">root<\/span> : <span style=\"color: #ECBE7B;\">None<\/span>\n        <span style=\"color: #98be65;\">}<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> the second fix is quite unnecessary but remove some boilerplate code when creating a node <\/p>\n\n<div class=\"org-src-container\">\n<label class=\"org-src-name\"><em><\/em><\/label>\n<pre class=\"src src-rust\" id=\"nil\"><span style=\"color: #51afef;\">impl<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">From<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Node<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #c678dd;\">&gt;<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #51afef;\">for<\/span> <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Box<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Node<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #98be65;\">&gt;<\/span><span style=\"color: #c678dd;\">&gt;<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">from<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #dcaeea;\">value<\/span>: <span style=\"color: #ECBE7B;\">Node<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #98be65;\">&gt;<\/span><span style=\"color: #c678dd;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">Self<\/span> <span style=\"color: #c678dd;\">{<\/span>\n        <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Box<\/span>::new<span style=\"color: #a9a1e1;\">(<\/span>value<span style=\"color: #a9a1e1;\">)<\/span><span style=\"color: #98be65;\">)<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> finally let&rsquo;s copy our simple implementation of the sorting insertion limited to those values which implement a total order trait <\/p>\n\n<div class=\"org-src-container\">\n<label class=\"org-src-name\"><em><\/em><\/label>\n<pre class=\"src src-rust\" id=\"nil\"><span style=\"color: #51afef;\">impl<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #dcaeea;\">T<\/span> : <span style=\"color: #ECBE7B;\">Ord<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">Tree<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">insert<\/span><span style=\"color: #c678dd;\">(<\/span>&amp; <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #51afef;\">self<\/span>, <span style=\"color: #dcaeea;\">value<\/span> : <span style=\"color: #ECBE7B;\">T<\/span> <span style=\"color: #c678dd;\">)<\/span> <span style=\"color: #c678dd;\">{<\/span>\n        <span style=\"color: #51afef;\">match<\/span> <span style=\"color: #51afef;\">self<\/span>.root<span style=\"color: #98be65;\">{<\/span>\n            <span style=\"color: #ECBE7B;\">None<\/span> =&gt; <span style=\"color: #a9a1e1;\">{<\/span>\n                <span style=\"color: #51afef;\">self<\/span>.root = <span style=\"color: #ECBE7B;\">Node<\/span>::new<span style=\"color: #51afef;\">(<\/span>value<span style=\"color: #51afef;\">)<\/span>.into<span style=\"color: #51afef;\">()<\/span>;\n            <span style=\"color: #a9a1e1;\">}<\/span>\n            <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #51afef;\">ref<\/span> <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #dcaeea;\">node<\/span><span style=\"color: #a9a1e1;\">)<\/span> =&gt; <span style=\"color: #a9a1e1;\">{<\/span>\n                <span style=\"color: #ECBE7B;\">Tree<\/span>::<span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;<\/span>::insert_recursive<span style=\"color: #51afef;\">(<\/span>node, value<span style=\"color: #51afef;\">)<\/span>;\n            <span style=\"color: #a9a1e1;\">}<\/span>\n        <span style=\"color: #98be65;\">}<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">insert_recursive<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #dcaeea;\">node<\/span> : &amp; <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #ECBE7B;\">Node<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #98be65;\">&gt;<\/span>, <span style=\"color: #dcaeea;\">value<\/span> : <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #c678dd;\">){<\/span>\n        <span style=\"color: #51afef;\">if<\/span> value &gt; node.value<span style=\"color: #98be65;\">{<\/span>\n            <span style=\"color: #51afef;\">match<\/span> node.right<span style=\"color: #a9a1e1;\">{<\/span>\n                <span style=\"color: #ECBE7B;\">None<\/span> =&gt; <span style=\"color: #51afef;\">{<\/span>\n                    node.right = <span style=\"color: #ECBE7B;\">Node<\/span>::new<span style=\"color: #c678dd;\">(<\/span>value<span style=\"color: #c678dd;\">)<\/span>.into<span style=\"color: #c678dd;\">()<\/span>;\n                <span style=\"color: #51afef;\">}<\/span>\n                <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #51afef;\">ref<\/span> <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #dcaeea;\">n<\/span><span style=\"color: #51afef;\">)<\/span> =&gt; <span style=\"color: #51afef;\">{<\/span>\n                    <span style=\"color: #ECBE7B;\">Tree<\/span>::<span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #c678dd;\">&gt;<\/span>::insert_recursive<span style=\"color: #c678dd;\">(<\/span>n, value<span style=\"color: #c678dd;\">)<\/span>;\n                <span style=\"color: #51afef;\">}<\/span>\n            <span style=\"color: #a9a1e1;\">}<\/span>\n        <span style=\"color: #98be65;\">}<\/span><span style=\"color: #51afef;\">else<\/span> <span style=\"color: #51afef;\">if<\/span> value &lt; node.value<span style=\"color: #98be65;\">{<\/span>\n            <span style=\"color: #51afef;\">match<\/span> node.left<span style=\"color: #a9a1e1;\">{<\/span>\n                <span style=\"color: #ECBE7B;\">None<\/span> =&gt; <span style=\"color: #51afef;\">{<\/span>\n                    node.left = <span style=\"color: #ECBE7B;\">Node<\/span>::new<span style=\"color: #c678dd;\">(<\/span>value<span style=\"color: #c678dd;\">)<\/span>.into<span style=\"color: #c678dd;\">()<\/span>;\n                <span style=\"color: #51afef;\">}<\/span>\n                <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #51afef;\">ref<\/span> <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #dcaeea;\">n<\/span><span style=\"color: #51afef;\">)<\/span> =&gt; <span style=\"color: #51afef;\">{<\/span>\n                    <span style=\"color: #ECBE7B;\">Tree<\/span>::<span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #c678dd;\">&gt;<\/span>::insert_recursive<span style=\"color: #c678dd;\">(<\/span>n, value<span style=\"color: #c678dd;\">)<\/span>;\n                <span style=\"color: #51afef;\">}<\/span>\n            <span style=\"color: #a9a1e1;\">}<\/span>\n        <span style=\"color: #98be65;\">}<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n\n<\/pre>\n<\/div>\n\n<p> Now we&rsquo;re ready to start the journey. <\/p>\n<\/div>\n<\/div>\n<div id=\"outline-container-org045fa0c\" class=\"outline-2\">\n<h2 id=\"org045fa0c\">Iterable and iterators<\/h2>\n<div class=\"outline-text-2\" id=\"text-org045fa0c\">\n<p> A data structure is <b>iterable<\/b> when you can get a suitable <b>iterator<\/b> from it; this object may be different from the source data structure, as each iterator must keep an internal status. <\/p>\n\n<p> Iterators have a wealth of useful methods, like <code>map<\/code> or <code>filter<\/code> which allow to easily create lazy pipelines. If you need you can directly use them in a <code>for<\/code> loop. <\/p>\n\n<p> I personally do not like iterators which allow mutations to the source data structure while looping, so I won&rsquo;t focus on this subject in this post. <\/p>\n\n<p> In Rust a struct is iterable if it implements the <code>IntoIter<\/code> trait which defines the <code>into_iter<\/code> method, returning an iterator. <\/p>\n\n<p> Iterators are structs which implement the <code>Iterator<\/code> trait which defines the <code>next<\/code> method. At each call the <code>next<\/code> method returns either <code>Some(value)<\/code> or <code>None<\/code> if the iterator exhausted its sequence of values or nor respectively. <\/p>\n\n<p> So let&rsquo;s create some stub for our goal with a couple of caveats: <\/p>\n\n<ol class=\"org-ol\">\n<li>we want to have a generic content type <code>T<\/code> in our tree which may possibly have no restriction, so instead of returning it by value we may want to return it as a borrowed reference <code>&amp; T<\/code><\/li>\n<li>the lifetime of these reference must be the same of the tree so if the returned type has lifetime <code>'a<\/code> also the iterator should be have at least the same lifetime<\/li>\n<\/ol>\n<div class=\"org-src-container\">\n<label class=\"org-src-name\"><em><\/em><\/label>\n<pre class=\"src src-rust\" id=\"nil\"><span style=\"color: #51afef;\">pub<\/span> <span style=\"color: #51afef;\">struct<\/span> <span style=\"color: #ECBE7B;\">TreeIter<\/span><span style=\"color: #51afef;\">{<\/span>\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">we have to figure out what to put here<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n\n<span style=\"color: #51afef;\">impl<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span>, <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">Iterator<\/span> <span style=\"color: #51afef;\">for<\/span> <span style=\"color: #ECBE7B;\">TreeIter<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span>, <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">this is the type signature of what we are returning<\/span>\n    <span style=\"color: #51afef;\">type<\/span> <span style=\"color: #ECBE7B;\">Item<\/span> = &amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">T<\/span>;\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">next<\/span><span style=\"color: #c678dd;\">(<\/span>&amp; <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #51afef;\">self<\/span><span style=\"color: #c678dd;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Self<\/span>::<span style=\"color: #ECBE7B;\">Item<\/span><span style=\"color: #c678dd;\">&gt;<\/span> <span style=\"color: #c678dd;\">{<\/span>\n        <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">what do we put here?<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n\n<span style=\"color: #51afef;\">impl<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span> , <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">IntoIterator<\/span> <span style=\"color: #51afef;\">for<\/span> &amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">Tree<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">this is the type signature of what we are returning<\/span>\n     <span style=\"color: #51afef;\">type<\/span> <span style=\"color: #ECBE7B;\">Item<\/span> = &amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">T<\/span>;\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">this is the type signature of the iterator<\/span>\n     <span style=\"color: #51afef;\">type<\/span> <span style=\"color: #ECBE7B;\">IntoIter<\/span> = <span style=\"color: #ECBE7B;\">TreeIter<\/span><span style=\"color: #c678dd;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span>, <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #c678dd;\">&gt;<\/span>;\n     <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">into_iter<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #51afef;\">self<\/span><span style=\"color: #c678dd;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">Self<\/span>::<span style=\"color: #ECBE7B;\">IntoIter<\/span> <span style=\"color: #c678dd;\">{<\/span>\n         <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">here we create the iterator from a Tree reference<\/span>\n     <span style=\"color: #c678dd;\">}<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"outline-container-orgaa8ee7d\" class=\"outline-2\">\n<h2 id=\"orgaa8ee7d\">Transform recursive into iterative<\/h2>\n<div class=\"outline-text-2\" id=\"text-orgaa8ee7d\">\n<p> Ok, this is going to be quite complex. <\/p>\n\n<p> In order to understand this transformation I will first write a pseudo-assembler sequence showing how a compiler could transform the recursive call of our traversal function <\/p>\n\n<ol class=\"org-ol\">\n<li>Set node with input variable<\/li>\n<li>If node.left null jump to 7<\/li>\n<li>Push stack frame<\/li>\n<li>Set return address to 7<\/li>\n<li>Set input variable to node.left<\/li>\n<li>Jump to 1<\/li>\n<li>Print node.value<\/li>\n<li>If node.right null jump to 13<\/li>\n<li>Push stack frame<\/li>\n<li>Set return address to 13<\/li>\n<li>Set input to node.right<\/li>\n<li>Jump to 1.<\/li>\n<li>Pop stack frame<\/li>\n<li>Jump to return address<\/li>\n<\/ol>\n\n<p> Then I will create an iteration which performs an equivalent algorithm: instead of the application stack I need a real stack where I push all the variable bindings and the return address <\/p>\n\n<ol class=\"org-ol\">\n<li>While the stack is not empty\n\n<ol class=\"org-ol\">\n<li>pop address, node<\/li>\n<li>match address\n\n<ol class=\"org-ol\">\n<li><p> case A \/\/ enter node <\/p>\n\n<ol class=\"org-ol\">\n<li>if node.left Some(left)\n\n<ol class=\"org-ol\">\n<li>push B, node<\/li>\n<li>push A, left<\/li>\n<\/ol><\/li>\n<li>else<\/li>\n<\/ol>\n<p> 2.1. push B, node <\/p><\/li>\n<li>case B \/\/ left explored\n\n<ol class=\"org-ol\">\n<li>print node.value<\/li>\n<li>push C, node<\/li>\n<\/ol><\/li>\n<li>case C \/\/ yielded node\n\n<ol class=\"org-ol\">\n<li>if node.right Some(right)\n\n<ol class=\"org-ol\">\n<li>push D, node<\/li>\n<li>push A, right<\/li>\n<\/ol><\/li>\n<li>else\n\n<ol class=\"org-ol\">\n<li>push D, node<\/li>\n<\/ol><\/li>\n<\/ol><\/li>\n<li>case D \/\/ completed\n\n<ol class=\"org-ol\">\n<li>no op<\/li>\n<\/ol><\/li>\n<\/ol><\/li>\n<\/ol><\/li>\n<\/ol>\n\n<p> This may sound quite <i>redundant<\/i> but please bear with me as clarity is more important now than optimizations we can add later <\/p>\n<\/div>\n<\/div>\n<div id=\"outline-container-org51ea89b\" class=\"outline-2\">\n<h2 id=\"org51ea89b\">Implementing the coroutine object<\/h2>\n<div class=\"outline-text-2\" id=\"text-org51ea89b\">\n<p> The more important point we did here is to transform address jump into an enumeration of states, which can then be used when creating an iterator coroutine; the magic step here is composed of two ideas: <\/p>\n\n<ol class=\"org-ol\">\n<li>to mess up the execution stack changing the return address<\/li>\n<li>to return the value instead of printing it<\/li>\n<\/ol>\n\n<p> First let&rsquo;s create an enum representing our return addresses <\/p>\n\n<div class=\"org-src-container\">\n<label class=\"org-src-name\"><em><\/em><\/label>\n<pre class=\"src src-rust\" id=\"nil\"><span style=\"color: #51afef; font-weight: bold;\">#<\/span><span style=\"color: #51afef; font-weight: bold;\">[<\/span><span style=\"color: #51afef; font-weight: bold;\">derive<\/span><span style=\"color: #c678dd; font-weight: bold;\">(<\/span><span style=\"color: #51afef; font-weight: bold;\">Debug, Copy, Clone<\/span><span style=\"color: #c678dd; font-weight: bold;\">)<\/span><span style=\"color: #51afef; font-weight: bold;\">]<\/span>\n<span style=\"color: #51afef;\">enum<\/span> <span style=\"color: #ECBE7B;\">Address<\/span><span style=\"color: #51afef;\">{<\/span>\n    <span style=\"color: #ECBE7B;\">Enter<\/span>,\n    <span style=\"color: #ECBE7B;\">LeftCompleted<\/span>,\n    <span style=\"color: #ECBE7B;\">ValueYield<\/span>,\n    <span style=\"color: #ECBE7B;\">Completed<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> Then we need to host our stack reification into our main coroutine object, each stack frame will contain the return address and our variable environment which luckily is composed of just one variable: the current node. <\/p>\n\n<div class=\"org-src-container\">\n<label class=\"org-src-name\"><em><\/em><\/label>\n<pre class=\"src src-rust\" id=\"nil\"><span style=\"color: #51afef;\">pub<\/span> <span style=\"color: #51afef;\">struct<\/span> <span style=\"color: #ECBE7B;\">TreeIter<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span>, <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #51afef;\">{<\/span>\n    <span style=\"color: #dcaeea;\">stack<\/span> : <span style=\"color: #ECBE7B;\">Vec<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Address<\/span>, &amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">Node<\/span><span style=\"color: #a9a1e1;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #a9a1e1;\">&gt;<\/span><span style=\"color: #98be65;\">)<\/span><span style=\"color: #c678dd;\">&gt;<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> in our implementation let&rsquo;s first have a creator that initialize the stack if any root node is available <\/p>\n\n<div class=\"org-src-container\">\n<label class=\"org-src-name\"><em><\/em><\/label>\n<pre class=\"src src-rust\" id=\"nil\"><span style=\"color: #51afef;\">impl<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span>, <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">TreeIter<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span>, <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">this creator initialize the stack<\/span>\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">with the root element if it exists<\/span>\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">new<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #dcaeea;\">tree<\/span> : &amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">Tree<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #98be65;\">&gt;<\/span><span style=\"color: #c678dd;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">TreeIter<\/span><span style=\"color: #c678dd;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span>, <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #c678dd;\">&gt;{<\/span>\n        <span style=\"color: #51afef;\">match<\/span> tree.root <span style=\"color: #98be65;\">{<\/span>\n            <span style=\"color: #ECBE7B;\">None<\/span> =&gt; <span style=\"color: #a9a1e1;\">{<\/span>\n                <span style=\"color: #ECBE7B;\">TreeIter<\/span><span style=\"color: #51afef;\">{<\/span>\n                    <span style=\"color: #dcaeea;\">stack<\/span> : <span style=\"color: #ECBE7B;\">Vec<\/span>::new<span style=\"color: #c678dd;\">()<\/span>\n                <span style=\"color: #51afef;\">}<\/span>\n            <span style=\"color: #a9a1e1;\">}<\/span>\n            <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #51afef;\">ref<\/span> <span style=\"color: #dcaeea;\">node<\/span><span style=\"color: #a9a1e1;\">)<\/span> =&gt; <span style=\"color: #a9a1e1;\">{<\/span>\n                <span style=\"color: #ECBE7B;\">TreeIter<\/span><span style=\"color: #51afef;\">{<\/span>\n                    <span style=\"color: #dcaeea;\">stack<\/span>: <span style=\"color: #51afef; font-weight: bold;\">vec!<\/span><span style=\"color: #c678dd;\">[<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Enter<\/span>, &amp; node<span style=\"color: #98be65;\">)<\/span><span style=\"color: #c678dd;\">]<\/span>\n                <span style=\"color: #51afef;\">}<\/span>\n            <span style=\"color: #a9a1e1;\">}<\/span>\n        <span style=\"color: #98be65;\">}<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> then we can add the method implementing the coroutine call <\/p>\n\n<div class=\"org-src-container\">\n<label class=\"org-src-name\"><em><\/em><\/label>\n<pre class=\"src src-rust\" id=\"nil\"><span style=\"color: #51afef;\">impl<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span>, <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">TreeIter<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span>, <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">here I cut the creator<\/span>\n\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">next_item<\/span><span style=\"color: #c678dd;\">(<\/span>&amp; <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #51afef;\">self<\/span><span style=\"color: #c678dd;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span>&amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #c678dd;\">&gt;{<\/span>\n        <span style=\"color: #51afef;\">while<\/span> <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #a9a1e1;\">(<\/span>address,node<span style=\"color: #a9a1e1;\">)<\/span><span style=\"color: #98be65;\">)<\/span> = <span style=\"color: #51afef;\">self<\/span>.stack.pop<span style=\"color: #98be65;\">(){<\/span>\n            <span style=\"color: #51afef;\">match<\/span> address <span style=\"color: #a9a1e1;\">{<\/span>\n                <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Enter<\/span> =&gt; <span style=\"color: #51afef;\">{<\/span>\n                    <span style=\"color: #51afef;\">match<\/span> node.left<span style=\"color: #c678dd;\">{<\/span>\n                        <span style=\"color: #ECBE7B;\">None<\/span> =&gt; <span style=\"color: #98be65;\">{<\/span>\n                            <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">if no left node jumps to yield stage<\/span>\n                            <span style=\"color: #51afef;\">self<\/span>.stack.push<span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">LeftCompleted<\/span>, node<span style=\"color: #51afef;\">)<\/span><span style=\"color: #a9a1e1;\">)<\/span>;\n                        <span style=\"color: #98be65;\">}<\/span>,\n                        <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #51afef;\">ref<\/span> <span style=\"color: #dcaeea;\">left<\/span><span style=\"color: #98be65;\">)<\/span> =&gt; <span style=\"color: #98be65;\">{<\/span>\n                            <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">otherwise set the return address to yield stage<\/span>\n                            <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">and call recursively<\/span>\n                            <span style=\"color: #51afef;\">self<\/span>.stack.push<span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">LeftCompleted<\/span>, node<span style=\"color: #51afef;\">)<\/span><span style=\"color: #a9a1e1;\">)<\/span>;\n                            <span style=\"color: #51afef;\">self<\/span>.stack.push<span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Enter<\/span>, left<span style=\"color: #51afef;\">)<\/span><span style=\"color: #a9a1e1;\">)<\/span>;\n                        <span style=\"color: #98be65;\">}<\/span>\n                    <span style=\"color: #c678dd;\">}<\/span>\n                <span style=\"color: #51afef;\">}<\/span>,\n                <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">LeftCompleted<\/span> =&gt; <span style=\"color: #51afef;\">{<\/span>\n                    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">the coroutine step<\/span>\n                    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">set the return address to the next sttep and<\/span>\n                    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">yield the value<\/span>\n                    <span style=\"color: #51afef;\">self<\/span>.stack.push<span style=\"color: #c678dd;\">(<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">ValueYield<\/span>, node<span style=\"color: #98be65;\">)<\/span><span style=\"color: #c678dd;\">)<\/span>;\n                    <span style=\"color: #51afef;\">return<\/span> <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #c678dd;\">(<\/span>&amp; node.value<span style=\"color: #c678dd;\">)<\/span>;\n                <span style=\"color: #51afef;\">}<\/span>,\n                <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">ValueYield<\/span> =&gt; <span style=\"color: #51afef;\">{<\/span>\n                    <span style=\"color: #51afef;\">match<\/span> node.right<span style=\"color: #c678dd;\">{<\/span>\n                        <span style=\"color: #ECBE7B;\">None<\/span> =&gt; <span style=\"color: #98be65;\">{<\/span>\n                            <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">jump to to end of function<\/span>\n                            <span style=\"color: #51afef;\">self<\/span>.stack.push<span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Completed<\/span>, node<span style=\"color: #51afef;\">)<\/span><span style=\"color: #a9a1e1;\">)<\/span>;\n                        <span style=\"color: #98be65;\">}<\/span>,\n                        <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #51afef;\">ref<\/span> <span style=\"color: #dcaeea;\">right<\/span><span style=\"color: #98be65;\">)<\/span> =&gt; <span style=\"color: #98be65;\">{<\/span>\n                            <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">set the reurn address to end of function<\/span>\n                            <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">recursive call on the right node<\/span>\n                            <span style=\"color: #51afef;\">self<\/span>.stack.push<span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Completed<\/span>, node<span style=\"color: #51afef;\">)<\/span><span style=\"color: #a9a1e1;\">)<\/span>;\n                            <span style=\"color: #51afef;\">self<\/span>.stack.push<span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Enter<\/span>, right<span style=\"color: #51afef;\">)<\/span><span style=\"color: #a9a1e1;\">)<\/span>;\n                        <span style=\"color: #98be65;\">}<\/span>\n                    <span style=\"color: #c678dd;\">}<\/span>\n                <span style=\"color: #51afef;\">}<\/span>,\n                <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Completed<\/span> =&gt; <span style=\"color: #51afef;\">{<\/span>\n                    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">ok this is just an address<\/span>\n                <span style=\"color: #51afef;\">}<\/span>,\n            <span style=\"color: #a9a1e1;\">}<\/span>\n        <span style=\"color: #98be65;\">}<\/span>\n        <span style=\"color: #ECBE7B;\">None<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"outline-container-orgeb06520\" class=\"outline-2\">\n<h2 id=\"orgeb06520\">Wrapping up traits<\/h2>\n<div class=\"outline-text-2\" id=\"text-orgeb06520\">\n<p> Now we can return to implement the <code>IntoIter<\/code> and <code>Iterator<\/code> traits for our tree: <\/p>\n\n<div class=\"org-src-container\">\n<label class=\"org-src-name\"><em><\/em><\/label>\n<pre class=\"src src-rust\" id=\"nil\"><span style=\"color: #51afef;\">impl<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span>, <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">Iterator<\/span> <span style=\"color: #51afef;\">for<\/span> <span style=\"color: #ECBE7B;\">TreeIter<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span>, <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n    <span style=\"color: #51afef;\">type<\/span> <span style=\"color: #ECBE7B;\">Item<\/span> = &amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">T<\/span>;\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">next<\/span><span style=\"color: #c678dd;\">(<\/span>&amp; <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #51afef;\">self<\/span><span style=\"color: #c678dd;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Self<\/span>::<span style=\"color: #ECBE7B;\">Item<\/span><span style=\"color: #c678dd;\">&gt;<\/span> <span style=\"color: #c678dd;\">{<\/span>\n        <span style=\"color: #51afef;\">self<\/span>.next_item<span style=\"color: #98be65;\">()<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n\n<span style=\"color: #51afef;\">impl<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span> , <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">IntoIterator<\/span> <span style=\"color: #51afef;\">for<\/span> &amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">Tree<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n     <span style=\"color: #51afef;\">type<\/span> <span style=\"color: #ECBE7B;\">Item<\/span> = &amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">T<\/span>;\n     <span style=\"color: #51afef;\">type<\/span> <span style=\"color: #ECBE7B;\">IntoIter<\/span> = <span style=\"color: #ECBE7B;\">TreeIter<\/span><span style=\"color: #c678dd;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span>, <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #c678dd;\">&gt;<\/span>;\n     <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">into_iter<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #51afef;\">self<\/span><span style=\"color: #c678dd;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">Self<\/span>::<span style=\"color: #ECBE7B;\">IntoIter<\/span> <span style=\"color: #c678dd;\">{<\/span>\n         <span style=\"color: #ECBE7B;\">TreeIter<\/span>::new<span style=\"color: #98be65;\">(<\/span><span style=\"color: #51afef;\">self<\/span><span style=\"color: #98be65;\">)<\/span>\n     <span style=\"color: #c678dd;\">}<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n\n<\/pre>\n<\/div>\n\n<p> and we can also test it; here are a couple of details: <\/p>\n\n<ul class=\"org-ul\">\n<li>iterators allow us to use <code>map<\/code> and <code>collect<\/code><\/li>\n<li>as returned values are of type <code>&amp; i64<\/code> we need to clone their value to easily make the test<\/li>\n<\/ul>\n<div class=\"org-src-container\">\n<label class=\"org-src-name\"><em><\/em><\/label>\n<pre class=\"src src-rust\" id=\"nil\"><span style=\"color: #51afef; font-weight: bold;\">#<\/span><span style=\"color: #51afef; font-weight: bold;\">[<\/span><span style=\"color: #51afef; font-weight: bold;\">cfg<\/span><span style=\"color: #c678dd; font-weight: bold;\">(<\/span><span style=\"color: #51afef; font-weight: bold;\">test<\/span><span style=\"color: #c678dd; font-weight: bold;\">)<\/span><span style=\"color: #51afef; font-weight: bold;\">]<\/span>\n<span style=\"color: #51afef;\">mod<\/span> <span style=\"color: #a9a1e1;\">tests<\/span> <span style=\"color: #51afef;\">{<\/span>\n    <span style=\"color: #51afef;\">use<\/span> <span style=\"color: #51afef;\">super<\/span>::*;\n\n    <span style=\"color: #51afef; font-weight: bold;\">#<\/span><span style=\"color: #c678dd; font-weight: bold;\">[<\/span><span style=\"color: #51afef; font-weight: bold;\">test<\/span><span style=\"color: #c678dd; font-weight: bold;\">]<\/span>\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">create_a_root_node<\/span><span style=\"color: #c678dd;\">()<\/span> <span style=\"color: #c678dd;\">{<\/span>\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #dcaeea;\">tree<\/span> : <span style=\"color: #ECBE7B;\">Tree<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">i64<\/span><span style=\"color: #98be65;\">&gt;<\/span>= <span style=\"color: #ECBE7B;\">Tree<\/span>::new<span style=\"color: #98be65;\">()<\/span>;\n        tree.insert<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">8<\/span><span style=\"color: #98be65;\">)<\/span>;\n        tree.insert<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">10<\/span><span style=\"color: #98be65;\">)<\/span>;\n        tree.insert<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">4<\/span><span style=\"color: #98be65;\">)<\/span>;\n        tree.insert<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">6<\/span><span style=\"color: #98be65;\">)<\/span>;\n        tree.insert<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">5<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #c678dd;\">println!<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #98be65;\">\"<\/span><span style=\"color: #98be65; font-style: italic;\">{:?}<\/span><span style=\"color: #98be65;\">\"<\/span>,tree<span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">result<\/span> : <span style=\"color: #ECBE7B;\">Vec<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">i64<\/span><span style=\"color: #98be65;\">&gt;<\/span> = tree.into_iter<span style=\"color: #98be65;\">()<\/span>\n            .map<span style=\"color: #98be65;\">(<\/span>|x| <span style=\"color: #a9a1e1;\">(<\/span>*x<span style=\"color: #a9a1e1;\">)<\/span>.clone<span style=\"color: #a9a1e1;\">()<\/span><span style=\"color: #98be65;\">)<\/span>.\n            .collect<span style=\"color: #98be65;\">()<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span>result,<span style=\"color: #51afef; font-weight: bold;\">vec!<\/span><span style=\"color: #a9a1e1;\">[<\/span><span style=\"color: #da8548; font-weight: bold;\">4<\/span>,<span style=\"color: #da8548; font-weight: bold;\">5<\/span>,<span style=\"color: #da8548; font-weight: bold;\">6<\/span>,<span style=\"color: #da8548; font-weight: bold;\">8<\/span>,<span style=\"color: #da8548; font-weight: bold;\">10<\/span><span style=\"color: #a9a1e1;\">]<\/span><span style=\"color: #98be65;\">)<\/span>;\n    <span style=\"color: #c678dd;\">}<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"outline-container-org0113085\" class=\"outline-2\">\n<h2 id=\"org0113085\">A note about this post and related subjects<\/h2>\n<div class=\"outline-text-2\" id=\"text-org0113085\">\n<p> When I started my Rust exploration the binary tree was my first experiment. <\/p>\n\n<p> I soon realized that the subject involved a deep understanding of Rust borrowing rules and that missing coroutines was going to make a depth first iterator a major task, so a single post idea quickly grow up to multiple posts. <\/p>\n\n<p> While working on this solution I learned a lot and tried to create the simplest possible code. At a certain point in time I tought to create a double linked tree using <code>Rc<\/code> and <code>Weak<\/code> reference and found a <a href=\"https:\/\/rust-unofficial.github.io\/too-many-lists\/\">great book<\/a> on the subject. <\/p>\n\n<p> Luckily I was able to use just <code>Box<\/code> and <code>Vec<\/code> to complete an acceptable iterator so I completely dropped doubly linked trees <\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"In this post I will show how to transform a recursive depth first traversal function into an iterator for a binary tree","protected":false},"author":1,"featured_media":409,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[5],"class_list":["post-410","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-language-learning","tag-rust"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Climbing a (binary) Tree - Noise On The Net<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Climbing a (binary) Tree - Noise On The Net\" \/>\n<meta property=\"og:description\" content=\"In this post I will show how to transform a recursive depth first traversal function into an iterator for a binary tree\" \/>\n<meta property=\"og:url\" content=\"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/\" \/>\n<meta property=\"og:site_name\" content=\"Noise On The Net\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-14T20:19:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-18T16:27:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/annie-spratt-gNI4_88t9Rs-unsplash_reduced.jpg?fit=800%2C1200&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"1200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"marco.p.v.vezzoli\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"marco.p.v.vezzoli\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/climbing-a-binary-tree\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/climbing-a-binary-tree\\\/\"},\"author\":{\"name\":\"marco.p.v.vezzoli\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/#\\\/schema\\\/person\\\/88c3a70f2b23480197bc61d6e1e2e982\"},\"headline\":\"Climbing a (binary) Tree\",\"datePublished\":\"2024-04-14T20:19:00+00:00\",\"dateModified\":\"2024-04-18T16:27:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/climbing-a-binary-tree\\\/\"},\"wordCount\":959,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/#\\\/schema\\\/person\\\/88c3a70f2b23480197bc61d6e1e2e982\"},\"image\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/climbing-a-binary-tree\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/noiseonthenet.space\\\/noise\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/annie-spratt-gNI4_88t9Rs-unsplash_reduced.jpg?fit=800%2C1200&ssl=1\",\"keywords\":[\"Rust\"],\"articleSection\":[\"Language learning\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/climbing-a-binary-tree\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/climbing-a-binary-tree\\\/\",\"url\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/climbing-a-binary-tree\\\/\",\"name\":\"Climbing a (binary) Tree - Noise On The Net\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/climbing-a-binary-tree\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/climbing-a-binary-tree\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/noiseonthenet.space\\\/noise\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/annie-spratt-gNI4_88t9Rs-unsplash_reduced.jpg?fit=800%2C1200&ssl=1\",\"datePublished\":\"2024-04-14T20:19:00+00:00\",\"dateModified\":\"2024-04-18T16:27:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/climbing-a-binary-tree\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/climbing-a-binary-tree\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/climbing-a-binary-tree\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/noiseonthenet.space\\\/noise\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/annie-spratt-gNI4_88t9Rs-unsplash_reduced.jpg?fit=800%2C1200&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/noiseonthenet.space\\\/noise\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/annie-spratt-gNI4_88t9Rs-unsplash_reduced.jpg?fit=800%2C1200&ssl=1\",\"width\":800,\"height\":1200},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/climbing-a-binary-tree\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Climbing a (binary) Tree\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/#website\",\"url\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/\",\"name\":\"Noise On The Net\",\"description\":\"Sharing adventures in code\",\"publisher\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/#\\\/schema\\\/person\\\/88c3a70f2b23480197bc61d6e1e2e982\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/#\\\/schema\\\/person\\\/88c3a70f2b23480197bc61d6e1e2e982\",\"name\":\"marco.p.v.vezzoli\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d9aab1df560bc14d73b0b442198f196ce39e7c7a38df1dc22fec0b97f17da9?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d9aab1df560bc14d73b0b442198f196ce39e7c7a38df1dc22fec0b97f17da9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d9aab1df560bc14d73b0b442198f196ce39e7c7a38df1dc22fec0b97f17da9?s=96&d=mm&r=g\",\"caption\":\"marco.p.v.vezzoli\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d9aab1df560bc14d73b0b442198f196ce39e7c7a38df1dc22fec0b97f17da9?s=96&d=mm&r=g\"},\"description\":\"Self taught assembler programming at 11 on my C64 (1983). Never stopped since then -- always looking up for curious things in the software development, data science and AI. Linux and FOSS user since 1994. MSc in physics in 1996. Working in large semiconductor companies since 1997 (STM, Micron) developing analytics and full stack web infrastructures, microservices, ML solutions\",\"sameAs\":[\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/marco-paolo-valerio-vezzoli-0663835\\\/\"],\"url\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/author\\\/marco-p-v-vezzoli\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Climbing a (binary) Tree - Noise On The Net","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/","og_locale":"en_US","og_type":"article","og_title":"Climbing a (binary) Tree - Noise On The Net","og_description":"In this post I will show how to transform a recursive depth first traversal function into an iterator for a binary tree","og_url":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/","og_site_name":"Noise On The Net","article_published_time":"2024-04-14T20:19:00+00:00","article_modified_time":"2024-04-18T16:27:47+00:00","og_image":[{"width":800,"height":1200,"url":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/annie-spratt-gNI4_88t9Rs-unsplash_reduced.jpg?fit=800%2C1200&ssl=1","type":"image\/jpeg"}],"author":"marco.p.v.vezzoli","twitter_card":"summary_large_image","twitter_misc":{"Written by":"marco.p.v.vezzoli","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/#article","isPartOf":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/"},"author":{"name":"marco.p.v.vezzoli","@id":"https:\/\/noiseonthenet.space\/noise\/#\/schema\/person\/88c3a70f2b23480197bc61d6e1e2e982"},"headline":"Climbing a (binary) Tree","datePublished":"2024-04-14T20:19:00+00:00","dateModified":"2024-04-18T16:27:47+00:00","mainEntityOfPage":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/"},"wordCount":959,"commentCount":4,"publisher":{"@id":"https:\/\/noiseonthenet.space\/noise\/#\/schema\/person\/88c3a70f2b23480197bc61d6e1e2e982"},"image":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/annie-spratt-gNI4_88t9Rs-unsplash_reduced.jpg?fit=800%2C1200&ssl=1","keywords":["Rust"],"articleSection":["Language learning"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/","url":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/","name":"Climbing a (binary) Tree - Noise On The Net","isPartOf":{"@id":"https:\/\/noiseonthenet.space\/noise\/#website"},"primaryImageOfPage":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/#primaryimage"},"image":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/annie-spratt-gNI4_88t9Rs-unsplash_reduced.jpg?fit=800%2C1200&ssl=1","datePublished":"2024-04-14T20:19:00+00:00","dateModified":"2024-04-18T16:27:47+00:00","breadcrumb":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/#primaryimage","url":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/annie-spratt-gNI4_88t9Rs-unsplash_reduced.jpg?fit=800%2C1200&ssl=1","contentUrl":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/annie-spratt-gNI4_88t9Rs-unsplash_reduced.jpg?fit=800%2C1200&ssl=1","width":800,"height":1200},{"@type":"BreadcrumbList","@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/noiseonthenet.space\/noise\/"},{"@type":"ListItem","position":2,"name":"Climbing a (binary) Tree"}]},{"@type":"WebSite","@id":"https:\/\/noiseonthenet.space\/noise\/#website","url":"https:\/\/noiseonthenet.space\/noise\/","name":"Noise On The Net","description":"Sharing adventures in code","publisher":{"@id":"https:\/\/noiseonthenet.space\/noise\/#\/schema\/person\/88c3a70f2b23480197bc61d6e1e2e982"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/noiseonthenet.space\/noise\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/noiseonthenet.space\/noise\/#\/schema\/person\/88c3a70f2b23480197bc61d6e1e2e982","name":"marco.p.v.vezzoli","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d9aab1df560bc14d73b0b442198f196ce39e7c7a38df1dc22fec0b97f17da9?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d9aab1df560bc14d73b0b442198f196ce39e7c7a38df1dc22fec0b97f17da9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d9aab1df560bc14d73b0b442198f196ce39e7c7a38df1dc22fec0b97f17da9?s=96&d=mm&r=g","caption":"marco.p.v.vezzoli"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/b9d9aab1df560bc14d73b0b442198f196ce39e7c7a38df1dc22fec0b97f17da9?s=96&d=mm&r=g"},"description":"Self taught assembler programming at 11 on my C64 (1983). Never stopped since then -- always looking up for curious things in the software development, data science and AI. Linux and FOSS user since 1994. MSc in physics in 1996. Working in large semiconductor companies since 1997 (STM, Micron) developing analytics and full stack web infrastructures, microservices, ML solutions","sameAs":["https:\/\/noiseonthenet.space\/noise\/","https:\/\/www.linkedin.com\/in\/marco-paolo-valerio-vezzoli-0663835\/"],"url":"https:\/\/noiseonthenet.space\/noise\/author\/marco-p-v-vezzoli\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/annie-spratt-gNI4_88t9Rs-unsplash_reduced.jpg?fit=800%2C1200&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pdDUZ5-6C","jetpack-related-posts":[],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/posts\/410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/comments?post=410"}],"version-history":[{"count":5,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/posts\/410\/revisions"}],"predecessor-version":[{"id":421,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/posts\/410\/revisions\/421"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/media\/409"}],"wp:attachment":[{"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/media?parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/categories?post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/tags?post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}