{"id":418,"date":"2024-04-21T09:06:00","date_gmt":"2024-04-21T08:06:00","guid":{"rendered":"https:\/\/noiseonthenet.space\/noise\/?p=418"},"modified":"2024-04-21T20:55:41","modified_gmt":"2024-04-21T19:55:41","slug":"embedding-a-binary-tree","status":"publish","type":"post","link":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/","title":{"rendered":"Embedding a (binary) Tree"},"content":{"rendered":"<div id=\"orgd1bddc8\" class=\"figure\"> <p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/rutpratheep-nilpechr-P5tWtdtY2AY-unsplash_reduced.jpg?ssl=1\" alt=\"rutpratheep-nilpechr-P5tWtdtY2AY-unsplash_reduced.jpg\" \/> <\/p> <\/div>\n\n<p> Photo by <a href=\"https:\/\/unsplash.com\/@rutpratheep?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\">Rutpratheep Nilpechr<\/a> on <a href=\"https:\/\/unsplash.com\/photos\/a-buddha-head-in-the-middle-of-a-tree-P5tWtdtY2AY?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 create a binary tree in Rust language, which is suitable to be used in embedded devices <\/p>\n\n<p> Here is the journey so far; I will mention some of the concepts already described in these posts. <\/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<li><a href=\"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\/\">Climbing a (binary) Tree<\/a><\/li>\n<\/ol>\n\n<p> The code for this post is <a href=\"https:\/\/github.com\/noiseOnTheNet\/post016_stack_tree\">here<\/a>. <\/p>\n\n<p> So far we created and explored trees which grow into the application <b>heap<\/b> memory, this allowed us to have almost arbitrary size and use the type system to guarantee consistent states <\/p>\n\n<p> What if we cannot use the heap? This happens in some &ldquo;embedded&rdquo; devices, which are an important target of Rust. <\/p>\n\n<p> Our trees will have a maximum height (or depth using mainstream jargon) and this may seem a hard limitation, but has interesting cases in machine learning. <\/p>\n\n<p> To simulate this case we are going to add the following line at the beginning of our file <\/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;\">no_std<\/span><span style=\"color: #51afef; font-weight: bold;\">]<\/span>\n<\/pre>\n<\/div>\n\n<p> This completely disable any access to the heap, as well as other libraries which may not work on bare metal platforms. <\/p>\n\n<p> Let&rsquo;s dive in. <\/p>\n<div id=\"outline-container-orgd0303f2\" class=\"outline-2\">\n<h2 id=\"orgd0303f2\">Choosing compromises<\/h2>\n<div class=\"outline-text-2\" id=\"text-orgd0303f2\">\n<div id=\"orgcea8fc2\" class=\"figure\"> <p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/egor-myznik-DRs9XsNlAZw-unsplash._reduced.jpg?ssl=1\" alt=\"egor-myznik-DRs9XsNlAZw-unsplash._reduced.jpg\" \/> <\/p> <\/div>\n\n<p> Photo by <a href=\"https:\/\/unsplash.com\/@vonshnauzer?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\">Egor Myznik<\/a> on <a href=\"https:\/\/unsplash.com\/photos\/a-young-boy-standing-in-front-of-a-vending-machine-DRs9XsNlAZw?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\">Unsplash<\/a> <\/p>\n\n<p> When developing code we may have constraints of memory size, execution time, storage size, I\/O throughput, computation cores available. <\/p>\n\n<p> Dealing with these constraints may require to give up perfect solutions and use some approximations; choosing carefully these approximations and account for the errors we expect is a critical path of a software architecture. <\/p>\n<\/div>\n<div id=\"outline-container-org6c85c2f\" class=\"outline-3\">\n<h3 id=\"org6c85c2f\">Size compromise<\/h3>\n<div class=\"outline-text-3\" id=\"text-org6c85c2f\">\n<p> By forcing our data structure to work in the stack we <b>need to know its size in advance<\/b> thus: <\/p>\n\n<ul class=\"org-ul\">\n<li>we may allocate more space than needed<\/li>\n<li>we may not grow over the maximum size we define<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div id=\"outline-container-org179b17f\" class=\"outline-3\">\n<h3 id=\"org179b17f\">Type compromise<\/h3>\n<div class=\"outline-text-3\" id=\"text-org179b17f\">\n<p> A more subtle point is <b>we may accept that types can&rsquo;t fully describe our status<\/b>, i.e. our data structure may allow for inconsistent states. <\/p>\n\n<p> To deal with this point we have two tools: <\/p>\n\n<ul class=\"org-ul\">\n<li><b>incapsulate<\/b> the inner elements of our data structure so that they can be accessed only via our interface<\/li>\n<li><b>add unit tests<\/b> to verify if the interface respect the &ldquo;contract&rdquo;<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div id=\"outline-container-org7a5f146\" class=\"outline-3\">\n<h3 id=\"org7a5f146\">Error management compromise<\/h3>\n<div class=\"outline-text-3\" id=\"text-org7a5f146\">\n<p> What if our data structure ends up in an inconsistent state? <\/p>\n\n<p> We may have e.g.: <\/p>\n\n<ul class=\"org-ul\">\n<li>incomplete coverage of our tests<\/li>\n<li>hardware failure<\/li>\n<li>software\/hardare attacks (e.g. rowhammer)<\/li>\n<\/ul>\n\n<p> If one of these or any other failure brings our data structure in an inconsistent state, we have two options: <\/p>\n\n<ul class=\"org-ul\">\n<li><b>panic<\/b> i.e. terminate the program<\/li>\n<li>return a <b>Result<\/b> type<\/li>\n<\/ul>\n\n<p> The first solution is simpler but makes it impossible to understand the reasons of the failure in a post-mortem analysis, moreover if I&rsquo;m developing a library I may prefer to leave the decision about how to handle the inconsistent state to the application. <\/p>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"outline-container-org06a053c\" class=\"outline-2\">\n<h2 id=\"org06a053c\">Addressing a binary tree in an array<\/h2>\n<div class=\"outline-text-2\" id=\"text-org06a053c\">\n<p> We can put a binary tree in a fixed size array if we store the data in a certain order. <\/p>\n\n<p> Let&rsquo;s take our previous example of a sorting tree and suppose to insert the following values <\/p>\n\n<ol class=\"org-ol\">\n<li>4<\/li>\n<li>6<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>5<\/li>\n<li>1<\/li>\n<li>8<\/li>\n<\/ol>\n\n<p> This is how our tree would look like: remember that smaller values get are placed in the right branch and higher are placed in the left branch. <\/p>\n\n<div id=\"org60b1e6d\" class=\"figure\"> <p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/post016_rust_tree.png?ssl=1\" alt=\"post016_rust_tree.png\" \/> <\/p> <\/div>\n\n<p> Now suppose to call the root <code>#1<\/code>: we are going to label all nodes with positive integers with the following rules: <\/p>\n\n<ul class=\"org-ul\">\n<li>the right node label number is twice than the parent node label number<\/li>\n<li>the left node label number is equal twice the parent node label plus one<\/li>\n<\/ul>\n<p> Here is the result <\/p>\n\n<div id=\"orgac7e323\" class=\"figure\"> <p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/post016_rust_tree_label.png?ssl=1\" alt=\"post016_rust_tree_label.png\" \/> <\/p> <\/div>\n\n<p> This rule allows to map a binary tree into an array <\/p>\n\n<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">\n\n\n<colgroup>\n<col  class=\"org-left\" \/>\n\n<col  class=\"org-left\" \/>\n\n<col  class=\"org-left\" \/>\n\n<col  class=\"org-left\" \/>\n\n<col  class=\"org-left\" \/>\n\n<col  class=\"org-left\" \/>\n\n<col  class=\"org-left\" \/>\n<\/colgroup>\n<thead>\n<tr>\n<th scope=\"col\" class=\"org-left\">#1<\/th>\n<th scope=\"col\" class=\"org-left\">#2<\/th>\n<th scope=\"col\" class=\"org-left\">#3<\/th>\n<th scope=\"col\" class=\"org-left\">#4<\/th>\n<th scope=\"col\" class=\"org-left\">#5<\/th>\n<th scope=\"col\" class=\"org-left\">#6<\/th>\n<th scope=\"col\" class=\"org-left\">#7<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"org-left\">(4)<\/td>\n<td class=\"org-left\">(2)<\/td>\n<td class=\"org-left\">(6)<\/td>\n<td class=\"org-left\">(1)<\/td>\n<td class=\"org-left\">(3)<\/td>\n<td class=\"org-left\">(5)<\/td>\n<td class=\"org-left\">(8)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n<p> It is no coincidence that this tree has 3 &ldquo;levels&rdquo; and the number of values it can host is equal to <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=N%283%29%3D2%5E3-1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"N(3)=2^3-1\" class=\"latex\" \/> <\/p>\n\n<p> So using this address rule we can use an array with a fixed lenght of <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=2%5EN&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"2^N\" class=\"latex\" \/> to host up to <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=N&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"N\" class=\"latex\" \/> levels of a binary tree. Of course we expect some cells to be empty; so we will use an array of <code>Option&lt;T&gt;<\/code> objects. <\/p>\n\n<p> For simplicty we have this <code>T<\/code> type to implement <code>Copy<\/code> so we can return it by value. The height of our tree (more commonly referred as <code>depth<\/code>) will be calculated as <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=depth%3D%5Clceil+log_2%28argmax_i%28node%28i%29%21%3DNone%29+%5Crceil&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"depth=&#92;lceil log_2(argmax_i(node(i)!=None) &#92;rceil\" class=\"latex\" \/> <\/p>\n\n<p> In this example we decide to fix the maximum depth to 8 so our tree will be placed into an array of <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=length+%3D+2%5E8+%3D+256&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"length = 2^8 = 256\" class=\"latex\" \/> <\/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;\">struct<\/span> <span style=\"color: #ECBE7B;\">STree8<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n    <span style=\"color: #dcaeea;\">nodes<\/span>: <span style=\"color: #c678dd;\">[<\/span><span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #98be65;\">&gt;<\/span>;<span style=\"color: #da8548; font-weight: bold;\">256<\/span><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;\">T<\/span> : <span style=\"color: #ECBE7B;\">Copy<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">STree8<\/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;\">create an empty tree<\/span>\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">new<\/span><span style=\"color: #c678dd;\">()<\/span> -&gt; <span style=\"color: #ECBE7B;\">STree8<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #c678dd;\">&gt;{<\/span>\n      <span style=\"color: #ECBE7B;\">STree8<\/span><span style=\"color: #98be65;\">{<\/span>\n          <span style=\"color: #dcaeea;\">nodes<\/span>: <span style=\"color: #a9a1e1;\">[<\/span><span style=\"color: #ECBE7B;\">None<\/span>; <span style=\"color: #da8548; font-weight: bold;\">256<\/span><span style=\"color: #a9a1e1;\">]<\/span>\n      <span style=\"color: #98be65;\">}<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">calculate the tree depth<\/span>\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">depth<\/span><span style=\"color: #c678dd;\">(<\/span>&amp; <span style=\"color: #51afef;\">self<\/span><span style=\"color: #c678dd;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">u32<\/span><span style=\"color: #c678dd;\">{<\/span>\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #dcaeea;\">result<\/span> : <span style=\"color: #ECBE7B;\">usize<\/span> = <span style=\"color: #da8548; font-weight: bold;\">0<\/span>;\n        <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">find the highest index of a non empty cell<\/span>\n        <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">there is no check about the array integrity here<\/span>\n        <span style=\"color: #51afef;\">for<\/span> <span style=\"color: #98be65;\">(<\/span>i, value<span style=\"color: #98be65;\">)<\/span> <span style=\"color: #51afef;\">in<\/span> <span style=\"color: #51afef;\">self<\/span>.nodes.into_iter<span style=\"color: #98be65;\">()<\/span>.enumerate<span style=\"color: #98be65;\">(){<\/span>\n            <span style=\"color: #51afef;\">if<\/span> <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #a9a1e1;\">(<\/span>_<span style=\"color: #a9a1e1;\">)<\/span> = value<span style=\"color: #a9a1e1;\">{<\/span>\n                <span style=\"color: #51afef;\">if<\/span> i &gt; result<span style=\"color: #51afef;\">{<\/span>\n                    result = i;\n                <span style=\"color: #51afef;\">}<\/span>\n            <span style=\"color: #a9a1e1;\">}<\/span>\n        <span style=\"color: #98be65;\">}<\/span>\n        <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">the cell 0 is always ignored with our assignment<\/span>\n        <span style=\"color: #51afef;\">if<\/span> result == <span style=\"color: #da8548; font-weight: bold;\">0<\/span> <span style=\"color: #98be65;\">{<\/span>\n            <span style=\"color: #51afef;\">return<\/span> <span style=\"color: #da8548; font-weight: bold;\">0<\/span>;\n        <span style=\"color: #98be65;\">}<\/span>\n        result.ilog2<span style=\"color: #98be65;\">()<\/span> + <span style=\"color: #da8548; font-weight: bold;\">1<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">this function returns the content of a cell<\/span>\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">but checks that the index is below the maximum allowed:<\/span>\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">we can't afford panic in an embedded code<\/span>\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">error types are explained later on<\/span>\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">peek<\/span><span style=\"color: #c678dd;\">(<\/span>&amp; <span style=\"color: #51afef;\">self<\/span>, <span style=\"color: #dcaeea;\">cell<\/span>: <span style=\"color: #ECBE7B;\">usize<\/span><span style=\"color: #c678dd;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">Result<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #98be65;\">&gt;<\/span>,<span style=\"color: #ECBE7B;\">TreeError<\/span><span style=\"color: #c678dd;\">&gt;{<\/span>\n\n        <span style=\"color: #51afef;\">if<\/span> cell &gt;= <span style=\"color: #da8548; font-weight: bold;\">256<\/span><span style=\"color: #98be65;\">{<\/span>\n            <span style=\"color: #51afef;\">return<\/span> <span style=\"color: #ECBE7B;\">Err<\/span><span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #ECBE7B;\">TreeError<\/span>::<span style=\"color: #ECBE7B;\">TreeOverflowCell<\/span><span style=\"color: #a9a1e1;\">)<\/span>\n        <span style=\"color: #98be65;\">}<\/span>\n        <span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #51afef;\">self<\/span>.nodes<span style=\"color: #a9a1e1;\">[<\/span>cell<span style=\"color: #a9a1e1;\">]<\/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> With our labelling rule we can create a sorting tree provided the type <code>T<\/code> implements the <code>Ord<\/code> 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;\">trait<\/span> <span style=\"color: #ECBE7B;\">SortTree<\/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>\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> -&gt; <span style=\"color: #ECBE7B;\">Result<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">usize<\/span>, &amp; '<span style=\"color: #51afef;\">static<\/span> <span style=\"color: #ECBE7B;\">str<\/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: #dcaeea;\">T<\/span> : <span style=\"color: #ECBE7B;\">Ord<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">SortTree<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #51afef;\">for<\/span> <span style=\"color: #ECBE7B;\">STree8<\/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;\">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> -&gt; <span style=\"color: #ECBE7B;\">Result<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">usize<\/span>, &amp; '<span style=\"color: #51afef;\">static<\/span> <span style=\"color: #ECBE7B;\">str<\/span><span style=\"color: #c678dd;\">&gt;{<\/span>\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #dcaeea;\">node<\/span> : <span style=\"color: #ECBE7B;\">usize<\/span> = <span style=\"color: #da8548; font-weight: bold;\">1<\/span>;\n        <span style=\"color: #51afef;\">loop<\/span> <span style=\"color: #98be65;\">{<\/span>\n            <span style=\"color: #51afef;\">if<\/span> node &gt; <span style=\"color: #da8548; font-weight: bold;\">255<\/span><span style=\"color: #a9a1e1;\">{<\/span>\n                <span style=\"color: #51afef;\">return<\/span> <span style=\"color: #ECBE7B;\">Err<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #98be65;\">\"level greater than 8\"<\/span><span style=\"color: #51afef;\">)<\/span>\n            <span style=\"color: #a9a1e1;\">}<\/span>\n            <span style=\"color: #51afef;\">match<\/span> <span style=\"color: #51afef;\">self<\/span>.nodes<span style=\"color: #a9a1e1;\">[<\/span>node<span style=\"color: #a9a1e1;\">]{<\/span>\n                <span style=\"color: #ECBE7B;\">None<\/span> =&gt; <span style=\"color: #51afef;\">{<\/span>\n                    <span style=\"color: #51afef;\">self<\/span>.nodes<span style=\"color: #c678dd;\">[<\/span>node<span style=\"color: #c678dd;\">]<\/span> = <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #c678dd;\">(<\/span>value<span style=\"color: #c678dd;\">)<\/span>;\n                    <span style=\"color: #51afef;\">return<\/span> <span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #c678dd;\">(<\/span>node<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: #dcaeea;\">node_value<\/span><span style=\"color: #51afef;\">)<\/span> =&gt; <span style=\"color: #51afef;\">{<\/span>\n                    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">if we have the value in the tree already<\/span>\n                    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">then stop<\/span>\n                    <span style=\"color: #51afef;\">if<\/span> value == *node_value<span style=\"color: #c678dd;\">{<\/span>\n                        <span style=\"color: #51afef;\">return<\/span> <span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #98be65;\">(<\/span>node<span style=\"color: #98be65;\">)<\/span>;\n                    <span style=\"color: #c678dd;\">}<\/span>\n                    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">the shift 1 operation is equivalent<\/span>\n                    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">to multiply by 2<\/span>\n                    node &lt;&lt;= <span style=\"color: #da8548; font-weight: bold;\">1<\/span>;\n\n                    <span style=\"color: #51afef;\">if<\/span> value &gt; *node_value<span style=\"color: #c678dd;\">{<\/span>\n                        <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">if the value is greater than<\/span>\n                        <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">the one in the current cell<\/span>\n                        <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">go to the \"left\" node<\/span>\n                        node += <span style=\"color: #da8548; font-weight: bold;\">1<\/span>;\n                    <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> We can now test our <code>insert<\/code> and <code>depth<\/code> methods <\/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;\">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;\">can_insert<\/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;\">STree8<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">i64<\/span><span style=\"color: #98be65;\">&gt;<\/span> = <span style=\"color: #ECBE7B;\">STree8<\/span>::new<span style=\"color: #98be65;\">()<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">test_list<\/span> = <span style=\"color: #98be65;\">[<\/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;\">2<\/span>,<span style=\"color: #da8548; font-weight: bold;\">8<\/span>,<span style=\"color: #da8548; font-weight: bold;\">6<\/span>,<span style=\"color: #da8548; font-weight: bold;\">1<\/span><span style=\"color: #98be65;\">]<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #dcaeea;\">count<\/span> = <span style=\"color: #da8548; font-weight: bold;\">0<\/span>;\n        <span style=\"color: #51afef;\">for<\/span> <span style=\"color: #dcaeea;\">value<\/span> <span style=\"color: #51afef;\">in<\/span> test_list<span style=\"color: #98be65;\">{<\/span>\n            <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">result<\/span> = tree.insert<span style=\"color: #a9a1e1;\">(<\/span>value<span style=\"color: #a9a1e1;\">)<\/span>;\n            <span style=\"color: #51afef;\">match<\/span> result <span style=\"color: #a9a1e1;\">{<\/span>\n                <span style=\"color: #ECBE7B;\">Err<\/span><span style=\"color: #51afef;\">(<\/span>message<span style=\"color: #51afef;\">)<\/span> =&gt; <span style=\"color: #51afef;\">{<\/span>\n                    <span style=\"color: #51afef; font-weight: bold;\">panic!<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #98be65;\">\"failed insertion {}\"<\/span>,message<span style=\"color: #c678dd;\">)<\/span>;\n                <span style=\"color: #51afef;\">}<\/span>,\n                <span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #51afef;\">(<\/span>node<span style=\"color: #51afef;\">)<\/span> =&gt; <span style=\"color: #51afef;\">{<\/span>\n                    <span style=\"color: #51afef; font-weight: bold;\">assert!<\/span><span style=\"color: #c678dd;\">(<\/span>node &lt; <span style=\"color: #da8548; font-weight: bold;\">256<\/span><span style=\"color: #c678dd;\">)<\/span>;\n                    count += <span style=\"color: #da8548; font-weight: bold;\">1<\/span>;\n                <span style=\"color: #51afef;\">}<\/span>\n            <span style=\"color: #a9a1e1;\">}<\/span>\n        <span style=\"color: #98be65;\">}<\/span>\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span>count,test_list.len<span style=\"color: #a9a1e1;\">()<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">result<\/span> = tree.peek<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">1<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">4<\/span><span style=\"color: #51afef;\">)<\/span><span style=\"color: #a9a1e1;\">)<\/span>,result<span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">result<\/span> = tree.peek<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">2<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">2<\/span><span style=\"color: #51afef;\">)<\/span><span style=\"color: #a9a1e1;\">)<\/span>,result<span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">result<\/span> = tree.peek<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">3<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">5<\/span><span style=\"color: #51afef;\">)<\/span><span style=\"color: #a9a1e1;\">)<\/span>,result<span style=\"color: #98be65;\">)<\/span>;\n    <span style=\"color: #c678dd;\">}<\/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;\">test_depth<\/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;\">STree8<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">i64<\/span><span style=\"color: #98be65;\">&gt;<\/span> = <span style=\"color: #ECBE7B;\">STree8<\/span>::new<span style=\"color: #98be65;\">()<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span>tree.depth<span style=\"color: #a9a1e1;\">()<\/span>,<span style=\"color: #da8548; font-weight: bold;\">0<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">_<\/span> = tree.insert<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">4<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span>tree.depth<span style=\"color: #a9a1e1;\">()<\/span>,<span style=\"color: #da8548; font-weight: bold;\">1<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">_<\/span> = 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: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span>tree.depth<span style=\"color: #a9a1e1;\">()<\/span>,<span style=\"color: #da8548; font-weight: bold;\">2<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">_<\/span> = tree.insert<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">2<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span>tree.depth<span style=\"color: #a9a1e1;\">()<\/span>,<span style=\"color: #da8548; font-weight: bold;\">2<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">_<\/span> = tree.insert<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">8<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span>tree.depth<span style=\"color: #a9a1e1;\">()<\/span>,<span style=\"color: #da8548; font-weight: bold;\">3<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">_<\/span> = tree.insert<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">6<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span>tree.depth<span style=\"color: #a9a1e1;\">()<\/span>,<span style=\"color: #da8548; font-weight: bold;\">4<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">_<\/span> = tree.insert<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">1<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span>tree.depth<span style=\"color: #a9a1e1;\">()<\/span>,<span style=\"color: #da8548; font-weight: bold;\">4<\/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-orgfd65686\" class=\"outline-2\">\n<h2 id=\"orgfd65686\">Design a Depth First Traversal Iterator<\/h2>\n<div class=\"outline-text-2\" id=\"text-orgfd65686\">\n<p> As in <a href=\"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\">Climbing a (binary) Tree<\/a> post we need a stack structure to store <\/p>\n\n<ul class=\"org-ul\">\n<li>the return address<\/li>\n<li>the node we are currently exploring<\/li>\n<\/ul>\n<\/div>\n<div id=\"outline-container-org782fb78\" class=\"outline-3\">\n<h3 id=\"org782fb78\">Storing the current node<\/h3>\n<div class=\"outline-text-3\" id=\"text-org782fb78\">\n<p> In a previous post ( <a href=\"https:\/\/noiseonthenet.space\/noise\/2024\/03\/stacking-bits\/\">Stacking Bits<\/a> ) I described how to create a stack of boolean using shift operators on a <code>usize<\/code> word. <\/p>\n\n<p> it turns out that is exactly working as our address rule &#x2013; and this is not a coincidence: we already saw how trees and stacks are mutually connected. <\/p>\n\n<p> By masking the topmost bit this the state is representing the exact address od our array cell. The following methods are extracted from the extended implementation. <\/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;\">fn<\/span> <span style=\"color: #c678dd;\">size<\/span><span style=\"color: #51afef;\">(<\/span>&amp; <span style=\"color: #51afef;\">self<\/span><span style=\"color: #51afef;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">u32<\/span> <span style=\"color: #51afef;\">{<\/span>\n        <span style=\"color: #ECBE7B;\">usize<\/span>::<span style=\"color: #ECBE7B;\">BITS<\/span> - <span style=\"color: #ECBE7B;\">usize<\/span>::leading_zeros<span style=\"color: #c678dd;\">(<\/span><span style=\"color: #51afef;\">self<\/span>.stack<span style=\"color: #c678dd;\">)<\/span> - <span style=\"color: #da8548; font-weight: bold;\">1<\/span>\n    <span style=\"color: #51afef;\">}<\/span>\n\n    <span style=\"color: #51afef;\">pub<\/span> <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">get_state<\/span><span style=\"color: #51afef;\">(<\/span>&amp; <span style=\"color: #51afef;\">self<\/span><span style=\"color: #51afef;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">usize<\/span> <span style=\"color: #51afef;\">{<\/span>\n        <span style=\"color: #51afef;\">self<\/span>.stack ^ <span style=\"color: #c678dd;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">1<\/span> &lt;&lt; <span style=\"color: #51afef;\">self<\/span>.size<span style=\"color: #98be65;\">()<\/span><span style=\"color: #c678dd;\">)<\/span>\n    <span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> by placing the binary stack code into a different file <code>btree.rs<\/code> we can access it using module commands in our main library <code>lib.rs<\/code> <\/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;\">mod<\/span> <span style=\"color: #a9a1e1;\">bstack<\/span>;\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"outline-container-org731ea07\" class=\"outline-3\">\n<h3 id=\"org731ea07\">Storing the return address<\/h3>\n<div class=\"outline-text-3\" id=\"text-org731ea07\">\n<p> As we cannot use a flexible data structure like <code>Vec&lt;T&gt;<\/code> to store the return address we may leverage the stack property to create an array to store it in the same index of each traversed cell <\/p>\n\n<p> Thus our iterator structure looks like this: <\/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;\">struct<\/span> <span style=\"color: #ECBE7B;\">STree8Iter<\/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: #dcaeea;\">tree<\/span>: &amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">STree8<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #c678dd;\">&gt;<\/span>,\n    <span style=\"color: #dcaeea;\">stack<\/span>: <span style=\"color: #a9a1e1;\">bstack<\/span>::<span style=\"color: #ECBE7B;\">BStack<\/span>,\n    <span style=\"color: #dcaeea;\">addresses<\/span>: <span style=\"color: #c678dd;\">[<\/span><span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Address<\/span><span style=\"color: #98be65;\">&gt;<\/span>; <span style=\"color: #da8548; font-weight: bold;\">256<\/span><span style=\"color: #c678dd;\">]<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> Before implementing it we make a little dirgression about errors <\/p>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"outline-container-org3d6f56d\" class=\"outline-2\">\n<h2 id=\"org3d6f56d\">Managing errors<\/h2>\n<div class=\"outline-text-2\" id=\"text-org3d6f56d\">\n<div id=\"org80c040e\" class=\"figure\"> <p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/kenny-eliason-Cmz06-0btw-unsplash_reduced.jpg?ssl=1\" alt=\"kenny-eliason--Cmz06-0btw-unsplash_reduced.jpg\" \/> <\/p> <\/div>\n\n<p> Photo by <a href=\"https:\/\/unsplash.com\/@neonbrand?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\">Kenny Eliason<\/a> on <a href=\"https:\/\/unsplash.com\/photos\/red-wrong-way-signage-on-road--Cmz06-0btw?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\">Unsplash<\/a> <\/p>\n\n<p> We cannot use <code>String<\/code> object to represent an error value, due to our heap constraint. <\/p>\n\n<p> As we saw that <code>&amp; str<\/code> objects in the stack do not live enough we may choose to use constant strings which have infinite lifetime <code>&amp; 'static str<\/code> but this has three drawbacks: <\/p>\n\n<ul class=\"org-ul\">\n<li>we cannot add dynamic information about why and how the system failed<\/li>\n<li>this will make it more complex for the users of our library to match and handle errors<\/li>\n<li>this may require more space than using other solutions<\/li>\n<\/ul>\n\n<p> A common approach is to define an <code>enum<\/code> which describes the expected failure modes. As we are using another library (bstack) which has its own errors it is a common practice to create one enumeration case also including the error type from this library <\/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, Clone, Copy, PartialEq<\/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;\">TreeError<\/span><span style=\"color: #51afef;\">{<\/span>\n    <span style=\"color: #ECBE7B;\">MissingReturnAddress<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #ECBE7B;\">usize<\/span><span style=\"color: #c678dd;\">)<\/span>,\n    <span style=\"color: #ECBE7B;\">StackError<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #a9a1e1;\">bstack<\/span>::<span style=\"color: #ECBE7B;\">BStackError<\/span><span style=\"color: #c678dd;\">)<\/span>,\n    <span style=\"color: #ECBE7B;\">IteratorCompleted<\/span>,\n    <span style=\"color: #ECBE7B;\">TreeOverflowCell<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> Rust has a very nice way to manage the error <a href=\"https:\/\/en.wikipedia.org\/wiki\/Monad_(functional_programming)\">monad<\/a> which include some syntax sugar like using a <a href=\"https:\/\/doc.rust-lang.org\/rust-by-example\/std\/result\/question_mark.html\">question mark<\/a> at the end of an expression. <\/p>\n\n<p> The <code>std<\/code> crate defines also an <code>Error<\/code> trait, which I will ignore in this specific case because: <\/p>\n\n<ul class=\"org-ul\">\n<li>in our emebedded environment may not work<\/li>\n<li>I need to keep this post simple<\/li>\n<\/ul>\n\n<p> To use this shortcut when we call a method from <code>bstack<\/code> library (which may return a different kind of error respect to our current signature) we need some kind of automatic translation. This can be done implementing the <code>From<\/code> trait. <\/p>\n\n<p> In our case we will just wrap the <code>bstack<\/code> error in our <code>TreeError<\/code> variant: <\/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: #ECBE7B;\">From<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #a9a1e1;\">bstack<\/span>::<span style=\"color: #ECBE7B;\">BStackError<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #51afef;\">for<\/span> <span style=\"color: #ECBE7B;\">TreeError<\/span><span style=\"color: #51afef;\">{<\/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: #a9a1e1;\">bstack<\/span>::<span style=\"color: #ECBE7B;\">BStackError<\/span><span style=\"color: #c678dd;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">Self<\/span> <span style=\"color: #c678dd;\">{<\/span>\n        <span style=\"color: #ECBE7B;\">TreeError<\/span>::<span style=\"color: #ECBE7B;\">StackError<\/span><span style=\"color: #98be65;\">(<\/span>value<span style=\"color: #98be65;\">)<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> This method is suitable for small applications like this one: more complex libraries are available for larger projects e.g. <a href=\"https:\/\/crates.io\/crates\/thiserror\">thiserror<\/a> <\/p>\n<\/div>\n<\/div>\n<div id=\"outline-container-org1d97e42\" class=\"outline-2\">\n<h2 id=\"org1d97e42\">Implement the Depth First Traversal Iterator<\/h2>\n<div class=\"outline-text-2\" id=\"text-org1d97e42\">\n<p> In a <a href=\"https:\/\/noiseonthenet.space\/noise\/2024\/04\/climbing-a-binary-tree\">previous post<\/a> I explained how to create an iterator for a binary tree: here we are going to implement the same sequence using our different stack structure. <\/p>\n\n<p> Here is the address enumeration described there: <\/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, Clone, Copy, PartialEq<\/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;\">AfterLeft<\/span>,\n    <span style=\"color: #ECBE7B;\">ValueYielded<\/span>,\n    <span style=\"color: #ECBE7B;\">Completed<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> To make paths more explicit I decided to use an enumeration to represent the possible connections from 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; 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, Clone, Copy, PartialEq<\/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;\">Branch<\/span><span style=\"color: #51afef;\">{<\/span>\n    <span style=\"color: #ECBE7B;\">Left<\/span>,\n    <span style=\"color: #ECBE7B;\">Right<\/span>\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> The first step is to incapsulate the <code>push<\/code> and <code>pop<\/code> calls to avoid misalignments: in this case there are two push methods <\/p>\n\n<ul class=\"org-ul\">\n<li><code>push_branch<\/code> to describe when accessing a chidren node with a relative path (i.e. left or right) from the current<\/li>\n<li><code>push_cell<\/code> is used to push a node with an absolute path, usually when a parent node is pushed back into the call stack with a changed return address<\/li>\n<\/ul>\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: #dcaeea;\">T<\/span> : <span style=\"color: #ECBE7B;\">Copy<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">STree8Iter<\/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;\">pub<\/span> <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;\">STree8<\/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;\">STree8Iter<\/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;\">let<\/span> <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #dcaeea;\">iterator<\/span> = <span style=\"color: #ECBE7B;\">STree8Iter<\/span>::<span style=\"color: #98be65;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span>, <span style=\"color: #ECBE7B;\">T<\/span><span style=\"color: #98be65;\">&gt;{<\/span>\n            tree,\n            <span style=\"color: #dcaeea;\">stack<\/span>: <span style=\"color: #a9a1e1;\">bstack<\/span>::<span style=\"color: #ECBE7B;\">BStack<\/span>::new<span style=\"color: #a9a1e1;\">()<\/span>,\n            <span style=\"color: #dcaeea;\">addresses<\/span>: <span style=\"color: #a9a1e1;\">[<\/span><span style=\"color: #ECBE7B;\">None<\/span>; <span style=\"color: #da8548; font-weight: bold;\">256<\/span><span style=\"color: #a9a1e1;\">]<\/span>\n        <span style=\"color: #98be65;\">}<\/span>;\n        <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">prepare the stack if the tree has a root node<\/span>\n        <span style=\"color: #51afef;\">if<\/span> <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #a9a1e1;\">(<\/span>_<span style=\"color: #a9a1e1;\">)<\/span><span style=\"color: #98be65;\">)<\/span> = iterator.tree.peek<span style=\"color: #98be65;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">1<\/span><span style=\"color: #98be65;\">)<\/span> <span style=\"color: #98be65;\">{<\/span>\n            <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">ignore errors as iterator is just created<\/span>\n            <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">_<\/span> = iterator.push_branch<span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #ECBE7B;\">Branch<\/span>::<span style=\"color: #ECBE7B;\">Right<\/span>, <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Enter<\/span><span style=\"color: #a9a1e1;\">)<\/span>;\n        <span style=\"color: #98be65;\">}<\/span>\n        iterator\n    <span style=\"color: #c678dd;\">}<\/span>\n\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">relative access from the current node<\/span>\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">push_branch<\/span><span style=\"color: #c678dd;\">(<\/span>&amp; <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #51afef;\">self<\/span>, <span style=\"color: #dcaeea;\">branch<\/span>: <span style=\"color: #ECBE7B;\">Branch<\/span>, <span style=\"color: #dcaeea;\">address<\/span>: <span style=\"color: #ECBE7B;\">Address<\/span><span style=\"color: #c678dd;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">Result<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">usize<\/span>, <span style=\"color: #ECBE7B;\">TreeError<\/span><span style=\"color: #c678dd;\">&gt;{<\/span>\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">_<\/span> = <span style=\"color: #51afef;\">self<\/span>.stack.push<span style=\"color: #98be65;\">(<\/span>branch == <span style=\"color: #ECBE7B;\">Branch<\/span>::<span style=\"color: #ECBE7B;\">Right<\/span><span style=\"color: #98be65;\">)<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">cell<\/span> = <span style=\"color: #51afef;\">self<\/span>.stack.get_state<span style=\"color: #98be65;\">()<\/span>;\n        <span style=\"color: #51afef;\">self<\/span>.addresses<span style=\"color: #98be65;\">[<\/span><span style=\"color: #51afef;\">self<\/span>.stack.get_state<span style=\"color: #a9a1e1;\">()<\/span><span style=\"color: #98be65;\">]<\/span> = <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #98be65;\">(<\/span>address<span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #98be65;\">(<\/span>cell<span style=\"color: #98be65;\">)<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">used to push back parent nodes in the call stack<\/span>\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">when we need to change their return address<\/span>\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">push_cell<\/span><span style=\"color: #c678dd;\">(<\/span>&amp; <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #51afef;\">self<\/span>, <span style=\"color: #dcaeea;\">cell<\/span>: <span style=\"color: #ECBE7B;\">usize<\/span>, <span style=\"color: #dcaeea;\">address<\/span>: <span style=\"color: #ECBE7B;\">Address<\/span><span style=\"color: #c678dd;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">Result<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">usize<\/span>,<span style=\"color: #ECBE7B;\">TreeError<\/span><span style=\"color: #c678dd;\">&gt;{<\/span>\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">_<\/span> = <span style=\"color: #51afef;\">self<\/span>.stack.push<span style=\"color: #98be65;\">(<\/span>cell &amp; <span style=\"color: #da8548; font-weight: bold;\">1<\/span> == <span style=\"color: #da8548; font-weight: bold;\">1<\/span><span style=\"color: #98be65;\">)<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">cell<\/span> = <span style=\"color: #51afef;\">self<\/span>.stack.get_state<span style=\"color: #98be65;\">()<\/span>;\n        <span style=\"color: #51afef;\">self<\/span>.addresses<span style=\"color: #98be65;\">[<\/span><span style=\"color: #51afef;\">self<\/span>.stack.get_state<span style=\"color: #a9a1e1;\">()<\/span><span style=\"color: #98be65;\">]<\/span> = <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #98be65;\">(<\/span>address<span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #98be65;\">(<\/span>cell<span style=\"color: #98be65;\">)<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n\n    <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">pop<\/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;\">Result<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">usize<\/span>, <span style=\"color: #ECBE7B;\">Address<\/span><span style=\"color: #98be65;\">)<\/span>, <span style=\"color: #ECBE7B;\">TreeError<\/span><span style=\"color: #c678dd;\">&gt;<\/span> <span style=\"color: #c678dd;\">{<\/span>\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">cell<\/span> = <span style=\"color: #51afef;\">self<\/span>.stack.get_state<span style=\"color: #98be65;\">()<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">_branch<\/span> = <span style=\"color: #51afef;\">self<\/span>.stack.pop<span style=\"color: #98be65;\">()<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">address<\/span> = <span style=\"color: #51afef;\">self<\/span>.addresses<span style=\"color: #98be65;\">[<\/span>cell<span style=\"color: #98be65;\">]<\/span>.ok_or<span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">TreeError<\/span>::<span style=\"color: #ECBE7B;\">MissingReturnAddress<\/span><span style=\"color: #a9a1e1;\">(<\/span>cell<span style=\"color: #a9a1e1;\">)<\/span><span style=\"color: #98be65;\">)<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span>;\n        <span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #a9a1e1;\">(<\/span>cell, address<span style=\"color: #a9a1e1;\">)<\/span><span style=\"color: #98be65;\">)<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>\n\n    <span style=\"color: #51afef;\">pub<\/span> <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;\">Result<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span>, <span style=\"color: #ECBE7B;\">TreeError<\/span><span style=\"color: #c678dd;\">&gt;{<\/span>\n        <span style=\"color: #51afef; font-weight: bold;\">todo!<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #98be65;\">\"to be developed\"<\/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> Finally where we have the actual implementation of <code>next_item<\/code>, which works in the same way we implemented it in the heap based 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;\">pub<\/span> <span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">next_item<\/span><span style=\"color: #51afef;\">(<\/span>&amp; <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #51afef;\">self<\/span><span style=\"color: #51afef;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">Result<\/span><span style=\"color: #51afef;\">&lt;<\/span><span style=\"color: #ECBE7B;\">T<\/span>, <span style=\"color: #ECBE7B;\">TreeError<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n        <span style=\"color: #51afef;\">while<\/span> <span style=\"color: #51afef;\">self<\/span>.stack.size<span style=\"color: #c678dd;\">()<\/span> &gt; <span style=\"color: #da8548; font-weight: bold;\">0<\/span><span style=\"color: #c678dd;\">{<\/span>\n            <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #98be65;\">(<\/span>cell, address<span style=\"color: #98be65;\">)<\/span> = <span style=\"color: #51afef;\">self<\/span>.pop<span style=\"color: #98be65;\">()<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span>;\n            <span style=\"color: #51afef;\">match<\/span> address<span style=\"color: #98be65;\">{<\/span>\n                <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Enter<\/span> =&gt; <span style=\"color: #a9a1e1;\">{<\/span>\n                    <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">left_address<\/span> = cell &lt;&lt; <span style=\"color: #da8548; font-weight: bold;\">1<\/span>;\n                    <span style=\"color: #51afef;\">match<\/span> <span style=\"color: #51afef;\">self<\/span>.tree.peek<span style=\"color: #51afef;\">(<\/span>left_address<span style=\"color: #51afef;\">)<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span><span style=\"color: #51afef;\">{<\/span>\n                        <span style=\"color: #ECBE7B;\">None<\/span> =&gt; <span style=\"color: #c678dd;\">{<\/span>\n                            <span style=\"color: #51afef;\">self<\/span>.push_cell<span style=\"color: #98be65;\">(<\/span>cell, <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">AfterLeft<\/span><span style=\"color: #98be65;\">)<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span>;\n                        <span style=\"color: #c678dd;\">}<\/span>\n                        <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #c678dd;\">(<\/span>_<span style=\"color: #c678dd;\">)<\/span> =&gt;<span style=\"color: #c678dd;\">{<\/span>\n                            <span style=\"color: #51afef;\">self<\/span>.push_cell<span style=\"color: #98be65;\">(<\/span>cell, <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">AfterLeft<\/span><span style=\"color: #98be65;\">)<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span>;\n                            <span style=\"color: #51afef;\">self<\/span>.push_branch<span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Branch<\/span>::<span style=\"color: #ECBE7B;\">Left<\/span>, <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Enter<\/span><span style=\"color: #98be65;\">)<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span>;\n                        <span style=\"color: #c678dd;\">}<\/span>\n                    <span style=\"color: #51afef;\">}<\/span>\n                <span style=\"color: #a9a1e1;\">}<\/span>,\n                <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">AfterLeft<\/span> =&gt; <span style=\"color: #a9a1e1;\">{<\/span>\n                    <span style=\"color: #51afef;\">self<\/span>.push_cell<span style=\"color: #51afef;\">(<\/span>cell, <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">ValueYielded<\/span><span style=\"color: #51afef;\">)<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span>;\n                    <span style=\"color: #51afef;\">if<\/span> <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #51afef;\">ref<\/span> <span style=\"color: #dcaeea;\">result<\/span><span style=\"color: #51afef;\">)<\/span> = <span style=\"color: #51afef;\">self<\/span>.tree.peek<span style=\"color: #51afef;\">(<\/span>cell<span style=\"color: #51afef;\">)<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span><span style=\"color: #51afef;\">{<\/span>\n                        <span style=\"color: #51afef;\">return<\/span> <span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #c678dd;\">(<\/span>*result<span style=\"color: #c678dd;\">)<\/span>;\n                    <span style=\"color: #51afef;\">}<\/span><span style=\"color: #51afef;\">else<\/span><span style=\"color: #51afef;\">{<\/span>\n                        <span style=\"color: #51afef;\">return<\/span> <span style=\"color: #ECBE7B;\">Err<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #ECBE7B;\">TreeError<\/span>::<span style=\"color: #ECBE7B;\">IteratorCompleted<\/span><span style=\"color: #c678dd;\">)<\/span>\n                    <span style=\"color: #51afef;\">}<\/span>\n\n                <span style=\"color: #a9a1e1;\">}<\/span>,\n                <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">ValueYielded<\/span> =&gt; <span style=\"color: #a9a1e1;\">{<\/span>\n                    <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">right_address<\/span> = <span style=\"color: #51afef;\">(<\/span>cell &lt;&lt; <span style=\"color: #da8548; font-weight: bold;\">1<\/span><span style=\"color: #51afef;\">)<\/span> | <span style=\"color: #da8548; font-weight: bold;\">1<\/span>;\n                    <span style=\"color: #51afef;\">match<\/span> <span style=\"color: #51afef;\">self<\/span>.tree.peek<span style=\"color: #51afef;\">(<\/span>right_address<span style=\"color: #51afef;\">)<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span><span style=\"color: #51afef;\">{<\/span>\n                        <span style=\"color: #ECBE7B;\">None<\/span> =&gt; <span style=\"color: #c678dd;\">{<\/span>\n                            <span style=\"color: #51afef;\">self<\/span>.push_cell<span style=\"color: #98be65;\">(<\/span>cell, <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Completed<\/span><span style=\"color: #98be65;\">)<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span>;\n                        <span style=\"color: #c678dd;\">}<\/span>,\n                        <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #c678dd;\">(<\/span>_<span style=\"color: #c678dd;\">)<\/span> =&gt; <span style=\"color: #c678dd;\">{<\/span>\n                            <span style=\"color: #51afef;\">self<\/span>.push_cell<span style=\"color: #98be65;\">(<\/span>cell, <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Completed<\/span><span style=\"color: #98be65;\">)<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span>;\n                            <span style=\"color: #51afef;\">self<\/span>.push_branch<span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Branch<\/span>::<span style=\"color: #ECBE7B;\">Right<\/span>, <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Enter<\/span><span style=\"color: #98be65;\">)<\/span><span style=\"color: #c678dd; font-weight: bold;\">?<\/span>;\n                        <span style=\"color: #c678dd;\">}<\/span>\n                    <span style=\"color: #51afef;\">}<\/span>\n                <span style=\"color: #a9a1e1;\">}<\/span>,\n                <span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Completed<\/span> =&gt;<span style=\"color: #a9a1e1;\">{<\/span>\n\n                <span style=\"color: #a9a1e1;\">}<\/span>\n            <span style=\"color: #98be65;\">}<\/span>\n        <span style=\"color: #c678dd;\">}<\/span>\n        <span style=\"color: #ECBE7B;\">Err<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #ECBE7B;\">TreeError<\/span>::<span style=\"color: #ECBE7B;\">IteratorCompleted<\/span><span style=\"color: #c678dd;\">)<\/span>\n    <span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"outline-container-orgdbfa325\" class=\"outline-2\">\n<h2 id=\"orgdbfa325\">Debugging<\/h2>\n<div class=\"outline-text-2\" id=\"text-orgdbfa325\">\n<p> We may not have a debugger easily running in a bare metal platform; moreover we have no <code>print!<\/code> macro available and also writing results on the serial connection with the host may alter the platform behavior. <\/p>\n\n<p> You certainly noticed that the <code>next_item<\/code> implementation does not conform the iterator trait this time. Of course we can create one anyway. <\/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: #dcaeea;\">T<\/span> : <span style=\"color: #ECBE7B;\">Copy<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #ECBE7B;\">Iterator<\/span> <span style=\"color: #51afef;\">for<\/span> <span style=\"color: #ECBE7B;\">STree8Iter<\/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> = <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;\">WARNING<\/span>\n        <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">this implicitly discard any error<\/span>\n        <span style=\"color: #51afef;\">self<\/span>.next_item<span style=\"color: #98be65;\">()<\/span>.ok<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> While <code>next_node<\/code> provides a rich return type explaining failures (mostly useful for debugging), this implementation removes all failure information to gain the rich <code>Iterator<\/code> echosystem: the library user is free to chose wathever is more appropriate. <\/p>\n\n<p> A test suite is not solving all bare metal issues but may help when possible, to solve issues in a frendlier environment <\/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;\">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;\">can_create_iterator<\/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;\">STree8<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">i64<\/span><span style=\"color: #98be65;\">&gt;<\/span> = <span style=\"color: #ECBE7B;\">STree8<\/span>::new<span style=\"color: #98be65;\">()<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">test_list<\/span> = <span style=\"color: #98be65;\">[<\/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;\">2<\/span>,<span style=\"color: #da8548; font-weight: bold;\">8<\/span>,<span style=\"color: #da8548; font-weight: bold;\">6<\/span>,<span style=\"color: #da8548; font-weight: bold;\">1<\/span><span style=\"color: #98be65;\">]<\/span>;\n        <span style=\"color: #51afef;\">for<\/span> <span style=\"color: #dcaeea;\">value<\/span> <span style=\"color: #51afef;\">in<\/span> test_list<span style=\"color: #98be65;\">{<\/span>\n            <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">_result<\/span> = tree.insert<span style=\"color: #a9a1e1;\">(<\/span>value<span style=\"color: #a9a1e1;\">)<\/span>;\n        <span style=\"color: #98be65;\">}<\/span>\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #dcaeea;\">iterator<\/span> = <span style=\"color: #ECBE7B;\">STree8Iter<\/span>::new<span style=\"color: #98be65;\">(<\/span>&amp; tree<span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span>iterator.stack.size<span style=\"color: #a9a1e1;\">()<\/span>,<span style=\"color: #da8548; font-weight: bold;\">1<\/span><span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span>iterator.pop<span style=\"color: #a9a1e1;\">()<\/span>,<span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #51afef;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">1<\/span>,<span style=\"color: #ECBE7B;\">Address<\/span>::<span style=\"color: #ECBE7B;\">Enter<\/span><span style=\"color: #51afef;\">)<\/span><span style=\"color: #a9a1e1;\">)<\/span><span style=\"color: #98be65;\">)<\/span>;\n    <span style=\"color: #c678dd;\">}<\/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;\">can_extract_with_next_item<\/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;\">STree8<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">i64<\/span><span style=\"color: #98be65;\">&gt;<\/span> = <span style=\"color: #ECBE7B;\">STree8<\/span>::new<span style=\"color: #98be65;\">()<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">test_list<\/span> = <span style=\"color: #98be65;\">[<\/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;\">2<\/span>,<span style=\"color: #da8548; font-weight: bold;\">8<\/span>,<span style=\"color: #da8548; font-weight: bold;\">6<\/span>,<span style=\"color: #da8548; font-weight: bold;\">1<\/span><span style=\"color: #98be65;\">]<\/span>;\n        <span style=\"color: #51afef;\">for<\/span> <span style=\"color: #dcaeea;\">value<\/span> <span style=\"color: #51afef;\">in<\/span> test_list<span style=\"color: #98be65;\">{<\/span>\n            <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">_result<\/span> = tree.insert<span style=\"color: #a9a1e1;\">(<\/span>value<span style=\"color: #a9a1e1;\">)<\/span>;\n        <span style=\"color: #98be65;\">}<\/span>\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #dcaeea;\">iterator<\/span> = <span style=\"color: #ECBE7B;\">STree8Iter<\/span>::new<span style=\"color: #98be65;\">(<\/span>&amp; tree<span style=\"color: #98be65;\">)<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #dcaeea;\">result<\/span> = iterator.next_item<span style=\"color: #98be65;\">()<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">1<\/span><span style=\"color: #a9a1e1;\">)<\/span>,result<span style=\"color: #98be65;\">)<\/span>;\n        result = iterator.next_item<span style=\"color: #98be65;\">()<\/span>;\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Ok<\/span><span style=\"color: #a9a1e1;\">(<\/span><span style=\"color: #da8548; font-weight: bold;\">2<\/span><span style=\"color: #a9a1e1;\">)<\/span>,result<span style=\"color: #98be65;\">)<\/span>;\n    <span style=\"color: #c678dd;\">}<\/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;\">sort_works<\/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;\">STree8<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">i64<\/span><span style=\"color: #98be65;\">&gt;<\/span> = <span style=\"color: #ECBE7B;\">STree8<\/span>::new<span style=\"color: #98be65;\">()<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">test_list<\/span> = <span style=\"color: #98be65;\">[<\/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;\">2<\/span>,<span style=\"color: #da8548; font-weight: bold;\">8<\/span>,<span style=\"color: #da8548; font-weight: bold;\">6<\/span>,<span style=\"color: #da8548; font-weight: bold;\">1<\/span><span style=\"color: #98be65;\">]<\/span>;\n        <span style=\"color: #51afef;\">for<\/span> <span style=\"color: #dcaeea;\">value<\/span> <span style=\"color: #51afef;\">in<\/span> test_list<span style=\"color: #98be65;\">{<\/span>\n            <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">_result<\/span> = tree.insert<span style=\"color: #a9a1e1;\">(<\/span>value<span style=\"color: #a9a1e1;\">)<\/span>;\n        <span style=\"color: #98be65;\">}<\/span>\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">expected<\/span> = <span style=\"color: #98be65;\">[<\/span><span style=\"color: #da8548; font-weight: bold;\">1<\/span>,<span style=\"color: #da8548; font-weight: bold;\">2<\/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: #98be65;\">]<\/span>;\n        <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #51afef;\">mut<\/span> <span style=\"color: #dcaeea;\">count<\/span> = <span style=\"color: #da8548; font-weight: bold;\">0<\/span>;\n        <span style=\"color: #51afef;\">for<\/span> <span style=\"color: #98be65;\">(<\/span>i,v<span style=\"color: #98be65;\">)<\/span> <span style=\"color: #51afef;\">in<\/span> tree.into_iter<span style=\"color: #98be65;\">()<\/span>.enumerate<span style=\"color: #98be65;\">(){<\/span>\n            <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #a9a1e1;\">(<\/span>v,expected<span style=\"color: #51afef;\">[<\/span>i<span style=\"color: #51afef;\">]<\/span><span style=\"color: #a9a1e1;\">)<\/span>;\n            count += <span style=\"color: #da8548; font-weight: bold;\">1<\/span>;\n        <span style=\"color: #98be65;\">}<\/span>\n        <span style=\"color: #51afef; font-weight: bold;\">assert_eq!<\/span><span style=\"color: #98be65;\">(<\/span>count,test_list.len<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-orgd26faae\" class=\"outline-2\">\n<h2 id=\"orgd26faae\">Conclusions<\/h2>\n<div class=\"outline-text-2\" id=\"text-orgd26faae\">\n<p> Rust allows pretty complex abstractions to run on bare metal with very little or no runtime cost (iterators are a well known example). <\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"creating a tree which works in embedded devices","protected":false},"author":1,"featured_media":446,"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-418","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>Embedding 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\/embedding-a-binary-tree\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Embedding a (binary) Tree - Noise On The Net\" \/>\n<meta property=\"og:description\" content=\"creating a tree which works in embedded devices\" \/>\n<meta property=\"og:url\" content=\"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/\" \/>\n<meta property=\"og:site_name\" content=\"Noise On The Net\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-21T08:06:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-21T19:55:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/rutpratheep-nilpechr-P5tWtdtY2AY-unsplash_reduced.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"800\" \/>\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=\"8 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\\\/embedding-a-binary-tree\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/embedding-a-binary-tree\\\/\"},\"author\":{\"name\":\"marco.p.v.vezzoli\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/#\\\/schema\\\/person\\\/88c3a70f2b23480197bc61d6e1e2e982\"},\"headline\":\"Embedding a (binary) Tree\",\"datePublished\":\"2024-04-21T08:06:00+00:00\",\"dateModified\":\"2024-04-21T19:55:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/embedding-a-binary-tree\\\/\"},\"wordCount\":1491,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/#\\\/schema\\\/person\\\/88c3a70f2b23480197bc61d6e1e2e982\"},\"image\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/embedding-a-binary-tree\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/noiseonthenet.space\\\/noise\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/rutpratheep-nilpechr-P5tWtdtY2AY-unsplash_reduced.jpg?fit=1200%2C800&ssl=1\",\"keywords\":[\"Rust\"],\"articleSection\":[\"Language learning\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/embedding-a-binary-tree\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/embedding-a-binary-tree\\\/\",\"url\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/embedding-a-binary-tree\\\/\",\"name\":\"Embedding a (binary) Tree - Noise On The Net\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/embedding-a-binary-tree\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/embedding-a-binary-tree\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/noiseonthenet.space\\\/noise\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/rutpratheep-nilpechr-P5tWtdtY2AY-unsplash_reduced.jpg?fit=1200%2C800&ssl=1\",\"datePublished\":\"2024-04-21T08:06:00+00:00\",\"dateModified\":\"2024-04-21T19:55:41+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/embedding-a-binary-tree\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/embedding-a-binary-tree\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/embedding-a-binary-tree\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/noiseonthenet.space\\\/noise\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/rutpratheep-nilpechr-P5tWtdtY2AY-unsplash_reduced.jpg?fit=1200%2C800&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/noiseonthenet.space\\\/noise\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/rutpratheep-nilpechr-P5tWtdtY2AY-unsplash_reduced.jpg?fit=1200%2C800&ssl=1\",\"width\":1200,\"height\":800},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/04\\\/embedding-a-binary-tree\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Embedding 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":"Embedding 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\/embedding-a-binary-tree\/","og_locale":"en_US","og_type":"article","og_title":"Embedding a (binary) Tree - Noise On The Net","og_description":"creating a tree which works in embedded devices","og_url":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/","og_site_name":"Noise On The Net","article_published_time":"2024-04-21T08:06:00+00:00","article_modified_time":"2024-04-21T19:55:41+00:00","og_image":[{"width":1200,"height":800,"url":"https:\/\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/rutpratheep-nilpechr-P5tWtdtY2AY-unsplash_reduced.jpg","type":"image\/jpeg"}],"author":"marco.p.v.vezzoli","twitter_card":"summary_large_image","twitter_misc":{"Written by":"marco.p.v.vezzoli","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/#article","isPartOf":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/"},"author":{"name":"marco.p.v.vezzoli","@id":"https:\/\/noiseonthenet.space\/noise\/#\/schema\/person\/88c3a70f2b23480197bc61d6e1e2e982"},"headline":"Embedding a (binary) Tree","datePublished":"2024-04-21T08:06:00+00:00","dateModified":"2024-04-21T19:55:41+00:00","mainEntityOfPage":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/"},"wordCount":1491,"commentCount":0,"publisher":{"@id":"https:\/\/noiseonthenet.space\/noise\/#\/schema\/person\/88c3a70f2b23480197bc61d6e1e2e982"},"image":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/rutpratheep-nilpechr-P5tWtdtY2AY-unsplash_reduced.jpg?fit=1200%2C800&ssl=1","keywords":["Rust"],"articleSection":["Language learning"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/","url":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/","name":"Embedding a (binary) Tree - Noise On The Net","isPartOf":{"@id":"https:\/\/noiseonthenet.space\/noise\/#website"},"primaryImageOfPage":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/#primaryimage"},"image":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/rutpratheep-nilpechr-P5tWtdtY2AY-unsplash_reduced.jpg?fit=1200%2C800&ssl=1","datePublished":"2024-04-21T08:06:00+00:00","dateModified":"2024-04-21T19:55:41+00:00","breadcrumb":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/#primaryimage","url":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/rutpratheep-nilpechr-P5tWtdtY2AY-unsplash_reduced.jpg?fit=1200%2C800&ssl=1","contentUrl":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/04\/rutpratheep-nilpechr-P5tWtdtY2AY-unsplash_reduced.jpg?fit=1200%2C800&ssl=1","width":1200,"height":800},{"@type":"BreadcrumbList","@id":"https:\/\/noiseonthenet.space\/noise\/2024\/04\/embedding-a-binary-tree\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/noiseonthenet.space\/noise\/"},{"@type":"ListItem","position":2,"name":"Embedding 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\/rutpratheep-nilpechr-P5tWtdtY2AY-unsplash_reduced.jpg?fit=1200%2C800&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pdDUZ5-6K","jetpack-related-posts":[],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/posts\/418","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=418"}],"version-history":[{"count":6,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/posts\/418\/revisions"}],"predecessor-version":[{"id":453,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/posts\/418\/revisions\/453"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/media\/446"}],"wp:attachment":[{"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/media?parent=418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/categories?post=418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/tags?post=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}