Free AI web copilot to create summaries, insights and extended knowledge, download it at here
2296
Abstract
figure><h2 id="bc2d">Insert an Element & Insert a Tree node</h2><p id="3283">The method <code>InsertElement</code> takes a key and value then constructs a new tree node, then calls the <code>InsertNode</code> function to insert the node into the Tree.</p><p id="760c">The function <code>InsertNode</code> is not a Tree method because its’ implementation is not specific to the Tree.</p>
<figure id="a616">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="1e2b">In-order Traverse</h2><p id="e389">The method<code>InOrderTraverseTree</code> takes a function as an argument and calls the <code>inOrderTraverseTree</code> function with the function as an argument.</p>
<figure id="13f8">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="277e">The traverse result of the example binary search tree is as follows.</p><p id="9964"><code>Value 1 -> Value 3 > Value 6 -> Value 8 -> Value 10</code></p><h2 id="d2f4">Preorder Traverse</h2>
<figure id="4861">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="cbed">The traverse result of the example binary search tree is as follows.</p><p id="8c64"><code>Value 8 -> Value 3 > Value 1-> Value 6-> Value 10</code></p><h2 id="9973">Postorder Traverse</h2>
<figure id="da72">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" hei
Options
ght="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="f3de">The traverse result of the example binary search tree is as follows.</p><p id="6789"><code>Value 1 -> Value 6 > Value 3 -> Value 10-> Value 8</code></p><h2 id="de61">Min & Max</h2><p id="ed18">The method <code>MinNode/MaxNode</code> is used to find the node whose key (<b>not value</b>) is the minimum/maximum.</p><p id="4912">Remember that there is no need to traverse the entire Tree to find the minimum/maximum.</p><p id="71b8">Because the property of a BST assures the minimum/maximum is always the leftmost/rightmost node.</p>
<figure id="3b96">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="a404">Binary Search</h2><p id="993f">The method <code>SearchNode</code> is used to search for a node in the Tree. If it is found, it returns the node. If not, it returns nil.</p>
<figure id="c5ea">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="4fae">Remove Node</h2><p id="180b">The method <code>RemoveNode</code>is used to remove a node from the Tree based on the key.</p>
<figure id="1cae">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="2380">I hope you enjoyed reading this. If you’d like to support me as a writer, consider becoming<a href="https://jerryan.medium.com/membership"> a Medium member</a>. You’ll also get unlimited access to every story on Medium.</p></article></body>