{"id":351,"date":"2024-03-16T17:05:00","date_gmt":"2024-03-16T16:05:00","guid":{"rendered":"https:\/\/noiseonthenet.space\/noise\/?p=351"},"modified":"2024-03-17T18:03:43","modified_gmt":"2024-03-17T17:03:43","slug":"growing-a-binary-tree-in-rust","status":"publish","type":"post","link":"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/","title":{"rendered":"Growing A (Binary) Tree in Rust"},"content":{"rendered":"<div id=\"orgab46f17\" class=\"figure\"> <p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/03\/toa-heftiba-W1yjvf5idqA-unsplash_reduced.jpg?ssl=1\" alt=\"toa-heftiba-W1yjvf5idqA-unsplash_reduced.jpg\" \/> <\/p> <\/div>\n\n<p> Photo by <a href=\"https:\/\/unsplash.com\/@heftiba?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\">Toa Heftiba<\/a> on <a href=\"https:\/\/unsplash.com\/photos\/green-leafed-plant-photography-W1yjvf5idqA?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\">Unsplash<\/a> <\/p>\n\n<p> The code for this post is <a href=\"https:\/\/github.com\/noiseOnTheNet\/post011_growing_a_tree\">here<\/a> <\/p>\n<div id=\"outline-container-org3938045\" class=\"outline-2\">\n<h2 id=\"org3938045\">What is a binary tree<\/h2>\n<div class=\"outline-text-2\" id=\"text-org3938045\">\n<p> A tree is a data structure, familiar to most developers; it is composed at least of a &ldquo;root&rdquo; node which is its entry point. Each node can have one or many or no &ldquo;chidren&rdquo; nodes, so this is a kind of graph. Unlike other graphs however, &ldquo;children&rdquo; nodes in trees won&rsquo;t have their &ldquo;parents&rdquo; as chidrens, nor any &ldquo;ancestor&rdquo;. In other words this graph has no circular paths, which creates a &ldquo;hierarchical&rdquo; structure. <\/p>\n\n<div id=\"org519fdfb\" class=\"figure\"> <p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/03\/post011_rust_tree.png?ssl=1\" alt=\"post011_rust_tree.png\" \/> <\/p> <\/div>\n\n<p> If we draw this data structure with the root node in the bottom and chidren nodes above it looks like a plant, hence its name. We are going to grow a specific species of tree which has at maximum two &ldquo;children&rdquo; per each node: it is called &ldquo;binary tree&rdquo;. <\/p>\n\n<p> Those nodes who don&rsquo;t have any childred are called &ldquo;leaves&rdquo; while those who have are called &ldquo;branches&rdquo;. <\/p>\n\n<p> Binary trees are used in machine learning models or to sort data etc. <\/p>\n<\/div>\n<\/div>\n<div id=\"outline-container-orgb0e193d\" class=\"outline-2\">\n<h2 id=\"orgb0e193d\">A naive example from C<\/h2>\n<div class=\"outline-text-2\" id=\"text-orgb0e193d\">\n<p> Those who programmed in languages in the C family know that to define a tree each data structure representing a node should &ldquo;connect&rdquo; to similar structures; this is why we call them &ldquo;recursive&rdquo; data structures. <\/p>\n\n<p> To avoid infinite &ldquo;loops&rdquo; C uses &ldquo;pointers&rdquo; to connect a parent node with its childrens, so a first attempt to create this structures in rust can be using references <\/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;\">BTree0<\/span> <span style=\"color: #51afef;\">{<\/span><span style=\"color: #dcaeea;\">left<\/span> : &amp; <span style=\"color: #ECBE7B;\">BTree0<\/span>, <span style=\"color: #dcaeea;\">right<\/span> : &amp; <span style=\"color: #ECBE7B;\">BTree0<\/span><span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> In C missing childrens are represented by <code>null<\/code> pointers which are quite dangerous, so Rust uses the <code>Option<\/code> enumeration to represent possibly missing data. An <code>Option<\/code> can be either <code>None<\/code> or <code>Some(thing)<\/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;\">struct<\/span> <span style=\"color: #ECBE7B;\">BTree0<\/span> <span style=\"color: #51afef;\">{<\/span><span style=\"color: #dcaeea;\">left<\/span> : <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span>&amp; <span style=\"color: #ECBE7B;\">BTree0<\/span><span style=\"color: #c678dd;\">&gt;<\/span>, <span style=\"color: #dcaeea;\">right<\/span> : <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span>&amp; <span style=\"color: #ECBE7B;\">BTree0<\/span><span style=\"color: #c678dd;\">&gt;<\/span><span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> The compiler need to be sure that each reference to <code>BTree0<\/code> node lives as long as the parent node, so we need to add a lifetime signature <\/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;\">BTree0<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #51afef;\">{<\/span><span style=\"color: #dcaeea;\">left<\/span> : <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span>&amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">BTree0<\/span><span style=\"color: #98be65;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span><span style=\"color: #98be65;\">&gt;<\/span><span style=\"color: #c678dd;\">&gt;<\/span>, <span style=\"color: #dcaeea;\">right<\/span> : <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span>&amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">BTree0<\/span><span style=\"color: #98be65;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span><span style=\"color: #98be65;\">&gt;<\/span><span style=\"color: #c678dd;\">&gt;<\/span><span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> let&rsquo;s derive the <code>Debug<\/code> trait to print this data structure <\/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<\/span><span style=\"color: #c678dd; font-weight: bold;\">)<\/span><span style=\"color: #51afef; font-weight: bold;\">]<\/span>\n<span style=\"color: #51afef;\">struct<\/span> <span style=\"color: #ECBE7B;\">BTree0<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span><span style=\"color: #51afef;\">&gt;<\/span> <span style=\"color: #51afef;\">{<\/span><span style=\"color: #dcaeea;\">left<\/span> : <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span>&amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">BTree0<\/span><span style=\"color: #98be65;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span><span style=\"color: #98be65;\">&gt;<\/span><span style=\"color: #c678dd;\">&gt;<\/span>, <span style=\"color: #dcaeea;\">right<\/span> : <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span>&amp; '<span style=\"color: #dcaeea;\">a<\/span> <span style=\"color: #ECBE7B;\">BTree0<\/span><span style=\"color: #98be65;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span><span style=\"color: #98be65;\">&gt;<\/span><span style=\"color: #c678dd;\">&gt;<\/span><span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> Now we can create some structure and print it: <\/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;\">fn<\/span> <span style=\"color: #c678dd;\">main<\/span><span style=\"color: #51afef;\">()<\/span> <span style=\"color: #51afef;\">{<\/span>\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">create a leaf node in this scope<\/span>\n    <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">test0<\/span> : <span style=\"color: #ECBE7B;\">BTree0<\/span> = <span style=\"color: #ECBE7B;\">BTree0<\/span> <span style=\"color: #c678dd;\">{<\/span> <span style=\"color: #dcaeea;\">left<\/span>: <span style=\"color: #ECBE7B;\">None<\/span>, <span style=\"color: #dcaeea;\">right<\/span>: <span style=\"color: #ECBE7B;\">None<\/span> <span style=\"color: #c678dd;\">}<\/span>;\n\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">create the root node and join the leaf<\/span>\n    <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">test<\/span> : <span style=\"color: #ECBE7B;\">BTree0<\/span> = <span style=\"color: #ECBE7B;\">BTree0<\/span> <span style=\"color: #c678dd;\">{<\/span> <span style=\"color: #dcaeea;\">left<\/span>: <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #98be65;\">(<\/span>&amp; test0<span style=\"color: #98be65;\">)<\/span>, <span style=\"color: #dcaeea;\">right<\/span>: <span style=\"color: #ECBE7B;\">None<\/span> <span style=\"color: #c678dd;\">}<\/span>;\n\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">print it!<\/span>\n    <span style=\"color: #c678dd;\">println!<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #98be65;\">\"Hello, world! <\/span><span style=\"color: #98be65; font-style: italic;\">{:?}<\/span><span style=\"color: #98be65;\">\"<\/span>, test<span style=\"color: #c678dd;\">)<\/span>;\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> Nice huh? Yes but this is not enough. <\/p>\n<\/div>\n<\/div>\n<div id=\"outline-container-org09e38a6\" class=\"outline-2\">\n<h2 id=\"org09e38a6\">Why the example doesn&rsquo;t actually work<\/h2>\n<div class=\"outline-text-2\" id=\"text-org09e38a6\">\n<p> Recursive data structures are usually created by recursive function calls, i.e. functions which are calling themselves. Let&rsquo;s make a toy example. <\/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;\">fn<\/span> <span style=\"color: #c678dd;\">main<\/span><span style=\"color: #51afef;\">()<\/span> <span style=\"color: #51afef;\">{<\/span>\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">create a leaf node in this scope<\/span>\n    <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">test0<\/span> : BTree0=BTree0 <span style=\"color: #c678dd;\">{<\/span> <span style=\"color: #dcaeea;\">left<\/span>: <span style=\"color: #ECBE7B;\">None<\/span>, <span style=\"color: #dcaeea;\">right<\/span>: <span style=\"color: #ECBE7B;\">None<\/span> <span style=\"color: #c678dd;\">}<\/span>;\n\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">create a branch calling a recursive function<\/span>\n    <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">test1<\/span> = add_test<span style=\"color: #c678dd;\">(<\/span><span style=\"color: #51afef;\">false<\/span><span style=\"color: #c678dd;\">)<\/span>;\n\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">create the root node and join the leaf<\/span>\n    <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">test<\/span> : <span style=\"color: #ECBE7B;\">BTree0<\/span> = <span style=\"color: #ECBE7B;\">BTree0<\/span> <span style=\"color: #c678dd;\">{<\/span> <span style=\"color: #dcaeea;\">left<\/span>: <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #98be65;\">(<\/span>&amp; test0<span style=\"color: #98be65;\">)<\/span>, <span style=\"color: #dcaeea;\">right<\/span>: <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #98be65;\">(<\/span>&amp; test1<span style=\"color: #98be65;\">)<\/span> <span style=\"color: #c678dd;\">}<\/span>;\n\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">print it!<\/span>\n    <span style=\"color: #c678dd;\">println!<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #98be65;\">\"Hello, world! <\/span><span style=\"color: #98be65; font-style: italic;\">{:?}<\/span><span style=\"color: #98be65;\">\"<\/span>, test<span style=\"color: #c678dd;\">)<\/span>;\n<span style=\"color: #51afef;\">}<\/span>\n\n<span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">add_test<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span><span style=\"color: #51afef;\">&gt;(<\/span><span style=\"color: #dcaeea;\">stop<\/span> : <span style=\"color: #ECBE7B;\">bool<\/span><span style=\"color: #51afef;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">BTree0<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span><span style=\"color: #51afef;\">&gt;{<\/span>\n    <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">value<\/span> = <span style=\"color: #ECBE7B;\">BTree0<\/span> <span style=\"color: #c678dd;\">{<\/span>\n        <span style=\"color: #dcaeea;\">left<\/span>: <span style=\"color: #98be65;\">(<\/span><span style=\"color: #51afef;\">if<\/span> stop <span style=\"color: #a9a1e1;\">{<\/span>\n                   <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">stops recursion<\/span>\n                   <span style=\"color: #ECBE7B;\">None<\/span>\n               <span style=\"color: #a9a1e1;\">}<\/span> <span style=\"color: #51afef;\">else<\/span> <span style=\"color: #a9a1e1;\">{<\/span>\n                   <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">calls itself but stop after next call<\/span>\n                   <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">branch<\/span> = add_test<span style=\"color: #51afef;\">(<\/span><span style=\"color: #51afef;\">true<\/span><span style=\"color: #51afef;\">)<\/span>;\n                   <span style=\"color: #ECBE7B;\">Some<\/span> <span style=\"color: #51afef;\">(<\/span>&amp; branch<span style=\"color: #51afef;\">)<\/span>\n               <span style=\"color: #a9a1e1;\">}<\/span><span style=\"color: #98be65;\">)<\/span>,\n        <span style=\"color: #dcaeea;\">right<\/span>: <span style=\"color: #ECBE7B;\">None<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>;\n    <span style=\"color: #51afef;\">return<\/span> value;\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> while the code is syntactically correct the <code>branch<\/code> variable does not live as much as the <code>value<\/code> which holds it. <\/p>\n\n<p> Why? because this variable is created in the stack frame of this function call, which will be wiped away as soon as the function returns. <\/p>\n<\/div>\n<\/div>\n<div id=\"outline-container-org48c9a39\" class=\"outline-2\">\n<h2 id=\"org48c9a39\">Grow a tree in a box<\/h2>\n<div class=\"outline-text-2\" id=\"text-org48c9a39\">\n<p> So the whole point is to create a node outside of the process stack, i.e. in the heap. <\/p>\n\n<p> The <code>Box<\/code> object provided by the standard library does exactly 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; font-weight: bold;\">#<\/span><span style=\"color: #51afef; font-weight: bold;\">[<\/span><span style=\"color: #51afef; font-weight: bold;\">derive<\/span><span style=\"color: #c678dd; font-weight: bold;\">(<\/span><span style=\"color: #51afef; font-weight: bold;\">Debug<\/span><span style=\"color: #c678dd; font-weight: bold;\">)<\/span><span style=\"color: #51afef; font-weight: bold;\">]<\/span>\n<span style=\"color: #51afef;\">struct<\/span> <span style=\"color: #ECBE7B;\">BTree1<\/span> <span style=\"color: #51afef;\">{<\/span><span style=\"color: #dcaeea;\">left<\/span> : <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Box<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">BTree1<\/span><span style=\"color: #98be65;\">&gt;<\/span><span style=\"color: #c678dd;\">&gt;<\/span>, <span style=\"color: #dcaeea;\">right<\/span> : <span style=\"color: #ECBE7B;\">Option<\/span><span style=\"color: #c678dd;\">&lt;<\/span><span style=\"color: #ECBE7B;\">Box<\/span><span style=\"color: #98be65;\">&lt;<\/span><span style=\"color: #ECBE7B;\">BTree1<\/span><span style=\"color: #98be65;\">&gt;<\/span><span style=\"color: #c678dd;\">&gt;<\/span><span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> this code now compiles and works: <\/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;\">fn<\/span> <span style=\"color: #c678dd;\">main<\/span><span style=\"color: #51afef;\">()<\/span> <span style=\"color: #51afef;\">{<\/span>\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">create a leaf node in this scope<\/span>\n    <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">test0<\/span> : <span style=\"color: #ECBE7B;\">BTree1<\/span> = <span style=\"color: #ECBE7B;\">BTree1<\/span> <span style=\"color: #c678dd;\">{<\/span> <span style=\"color: #dcaeea;\">left<\/span>: <span style=\"color: #ECBE7B;\">None<\/span>, <span style=\"color: #dcaeea;\">right<\/span>: <span style=\"color: #ECBE7B;\">None<\/span> <span style=\"color: #c678dd;\">}<\/span>;\n\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">create a branch calling a recursive function<\/span>\n    <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">test1<\/span> = add_test<span style=\"color: #c678dd;\">(<\/span><span style=\"color: #51afef;\">false<\/span><span style=\"color: #c678dd;\">)<\/span>;\n\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">create the root node and join the leaf<\/span>\n    <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">test<\/span> : <span style=\"color: #ECBE7B;\">BTree1<\/span> = <span style=\"color: #ECBE7B;\">BTree1<\/span> <span style=\"color: #c678dd;\">{<\/span> <span style=\"color: #dcaeea;\">left<\/span>: <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Box<\/span>::new<span style=\"color: #a9a1e1;\">(<\/span>test0<span style=\"color: #a9a1e1;\">)<\/span><span style=\"color: #98be65;\">)<\/span>, <span style=\"color: #dcaeea;\">right<\/span>: <span style=\"color: #ECBE7B;\">Some<\/span><span style=\"color: #98be65;\">(<\/span><span style=\"color: #ECBE7B;\">Box<\/span>::new<span style=\"color: #a9a1e1;\">(<\/span>test1<span style=\"color: #a9a1e1;\">)<\/span><span style=\"color: #98be65;\">)<\/span> <span style=\"color: #c678dd;\">}<\/span>;\n\n    <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">print it!<\/span>\n    <span style=\"color: #c678dd;\">println!<\/span><span style=\"color: #c678dd;\">(<\/span><span style=\"color: #98be65;\">\"Hello, world! <\/span><span style=\"color: #98be65; font-style: italic;\">{:?}<\/span><span style=\"color: #98be65;\">\"<\/span>, test<span style=\"color: #c678dd;\">)<\/span>;\n<span style=\"color: #51afef;\">}<\/span>\n\n<span style=\"color: #51afef;\">fn<\/span> <span style=\"color: #c678dd;\">add_test<\/span><span style=\"color: #51afef;\">&lt;<\/span>'<span style=\"color: #dcaeea;\">a<\/span><span style=\"color: #51afef;\">&gt;(<\/span><span style=\"color: #dcaeea;\">stop<\/span> : <span style=\"color: #ECBE7B;\">bool<\/span><span style=\"color: #51afef;\">)<\/span> -&gt; <span style=\"color: #ECBE7B;\">BTree1<\/span><span style=\"color: #51afef;\">{<\/span>\n    <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">value<\/span> = <span style=\"color: #ECBE7B;\">BTree1<\/span> <span style=\"color: #c678dd;\">{<\/span>\n        <span style=\"color: #dcaeea;\">left<\/span>: <span style=\"color: #98be65;\">(<\/span>\n            <span style=\"color: #51afef;\">if<\/span> stop <span style=\"color: #a9a1e1;\">{<\/span>\n                <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">stops recursion<\/span>\n                <span style=\"color: #ECBE7B;\">None<\/span>\n            <span style=\"color: #a9a1e1;\">}<\/span> <span style=\"color: #51afef;\">else<\/span> <span style=\"color: #a9a1e1;\">{<\/span>\n                <span style=\"color: #5B6268;\">\/\/ <\/span><span style=\"color: #5B6268;\">calls itself but stop after next call<\/span>\n                <span style=\"color: #51afef;\">let<\/span> <span style=\"color: #dcaeea;\">branch<\/span> = add_test<span style=\"color: #51afef;\">(<\/span><span style=\"color: #51afef;\">true<\/span><span style=\"color: #51afef;\">)<\/span>; <span style=\"color: #ECBE7B;\">Some<\/span> <span style=\"color: #51afef;\">(<\/span><span style=\"color: #ECBE7B;\">Box<\/span>::new<span style=\"color: #c678dd;\">(<\/span>branch<span style=\"color: #c678dd;\">)<\/span><span style=\"color: #51afef;\">)<\/span>\n            <span style=\"color: #a9a1e1;\">}<\/span><span style=\"color: #98be65;\">)<\/span>,\n        <span style=\"color: #dcaeea;\">right<\/span>: <span style=\"color: #ECBE7B;\">None<\/span>\n    <span style=\"color: #c678dd;\">}<\/span>;\n    <span style=\"color: #51afef;\">return<\/span> value;\n<span style=\"color: #51afef;\">}<\/span>\n<\/pre>\n<\/div>\n\n<p> the <code>new<\/code> method of the <code>Box<\/code> trait creates the object in the heap; the compiler deletes it only when it is no more needed. <\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"An introduction to binary trees in rust","protected":false},"author":1,"featured_media":355,"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-351","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.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Growing A (Binary) Tree in Rust - 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\/03\/growing-a-binary-tree-in-rust\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Growing A (Binary) Tree in Rust - Noise On The Net\" \/>\n<meta property=\"og:description\" content=\"An introduction to binary trees in rust\" \/>\n<meta property=\"og:url\" content=\"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/\" \/>\n<meta property=\"og:site_name\" content=\"Noise On The Net\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-16T16:05:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-17T17:03:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/03\/toa-heftiba-W1yjvf5idqA-unsplash_reduced.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"1200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"marco.p.v.vezzoli\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"marco.p.v.vezzoli\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/03\\\/growing-a-binary-tree-in-rust\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/03\\\/growing-a-binary-tree-in-rust\\\/\"},\"author\":{\"name\":\"marco.p.v.vezzoli\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/#\\\/schema\\\/person\\\/88c3a70f2b23480197bc61d6e1e2e982\"},\"headline\":\"Growing A (Binary) Tree in Rust\",\"datePublished\":\"2024-03-16T16:05:00+00:00\",\"dateModified\":\"2024-03-17T17:03:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/03\\\/growing-a-binary-tree-in-rust\\\/\"},\"wordCount\":496,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/#\\\/schema\\\/person\\\/88c3a70f2b23480197bc61d6e1e2e982\"},\"image\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/03\\\/growing-a-binary-tree-in-rust\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/noiseonthenet.space\\\/noise\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/toa-heftiba-W1yjvf5idqA-unsplash_reduced.jpg?fit=800%2C1200&ssl=1\",\"keywords\":[\"Rust\"],\"articleSection\":[\"Language learning\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/03\\\/growing-a-binary-tree-in-rust\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/03\\\/growing-a-binary-tree-in-rust\\\/\",\"url\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/03\\\/growing-a-binary-tree-in-rust\\\/\",\"name\":\"Growing A (Binary) Tree in Rust - Noise On The Net\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/03\\\/growing-a-binary-tree-in-rust\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/03\\\/growing-a-binary-tree-in-rust\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/noiseonthenet.space\\\/noise\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/toa-heftiba-W1yjvf5idqA-unsplash_reduced.jpg?fit=800%2C1200&ssl=1\",\"datePublished\":\"2024-03-16T16:05:00+00:00\",\"dateModified\":\"2024-03-17T17:03:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/03\\\/growing-a-binary-tree-in-rust\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/03\\\/growing-a-binary-tree-in-rust\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/03\\\/growing-a-binary-tree-in-rust\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/noiseonthenet.space\\\/noise\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/toa-heftiba-W1yjvf5idqA-unsplash_reduced.jpg?fit=800%2C1200&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/noiseonthenet.space\\\/noise\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/toa-heftiba-W1yjvf5idqA-unsplash_reduced.jpg?fit=800%2C1200&ssl=1\",\"width\":800,\"height\":1200},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/2024\\\/03\\\/growing-a-binary-tree-in-rust\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/noiseonthenet.space\\\/noise\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Growing A (Binary) Tree in Rust\"}]},{\"@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":"Growing A (Binary) Tree in Rust - 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\/03\/growing-a-binary-tree-in-rust\/","og_locale":"en_US","og_type":"article","og_title":"Growing A (Binary) Tree in Rust - Noise On The Net","og_description":"An introduction to binary trees in rust","og_url":"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/","og_site_name":"Noise On The Net","article_published_time":"2024-03-16T16:05:00+00:00","article_modified_time":"2024-03-17T17:03:43+00:00","og_image":[{"width":800,"height":1200,"url":"https:\/\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/03\/toa-heftiba-W1yjvf5idqA-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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/#article","isPartOf":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/"},"author":{"name":"marco.p.v.vezzoli","@id":"https:\/\/noiseonthenet.space\/noise\/#\/schema\/person\/88c3a70f2b23480197bc61d6e1e2e982"},"headline":"Growing A (Binary) Tree in Rust","datePublished":"2024-03-16T16:05:00+00:00","dateModified":"2024-03-17T17:03:43+00:00","mainEntityOfPage":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/"},"wordCount":496,"commentCount":0,"publisher":{"@id":"https:\/\/noiseonthenet.space\/noise\/#\/schema\/person\/88c3a70f2b23480197bc61d6e1e2e982"},"image":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/03\/toa-heftiba-W1yjvf5idqA-unsplash_reduced.jpg?fit=800%2C1200&ssl=1","keywords":["Rust"],"articleSection":["Language learning"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/","url":"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/","name":"Growing A (Binary) Tree in Rust - Noise On The Net","isPartOf":{"@id":"https:\/\/noiseonthenet.space\/noise\/#website"},"primaryImageOfPage":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/#primaryimage"},"image":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/03\/toa-heftiba-W1yjvf5idqA-unsplash_reduced.jpg?fit=800%2C1200&ssl=1","datePublished":"2024-03-16T16:05:00+00:00","dateModified":"2024-03-17T17:03:43+00:00","breadcrumb":{"@id":"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/#primaryimage","url":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/03\/toa-heftiba-W1yjvf5idqA-unsplash_reduced.jpg?fit=800%2C1200&ssl=1","contentUrl":"https:\/\/i0.wp.com\/noiseonthenet.space\/noise\/wp-content\/uploads\/2024\/03\/toa-heftiba-W1yjvf5idqA-unsplash_reduced.jpg?fit=800%2C1200&ssl=1","width":800,"height":1200},{"@type":"BreadcrumbList","@id":"https:\/\/noiseonthenet.space\/noise\/2024\/03\/growing-a-binary-tree-in-rust\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/noiseonthenet.space\/noise\/"},{"@type":"ListItem","position":2,"name":"Growing A (Binary) Tree in Rust"}]},{"@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\/03\/toa-heftiba-W1yjvf5idqA-unsplash_reduced.jpg?fit=800%2C1200&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pdDUZ5-5F","jetpack-related-posts":[],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/posts\/351","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=351"}],"version-history":[{"count":4,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/posts\/351\/revisions"}],"predecessor-version":[{"id":363,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/posts\/351\/revisions\/363"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/media\/355"}],"wp:attachment":[{"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/media?parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/categories?post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/noiseonthenet.space\/noise\/wp-json\/wp\/v2\/tags?post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}