mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-13 10:49:46 +08:00
Documentation for 5245b3e4a9
This commit is contained in:
@@ -163,21 +163,21 @@ Functions</h2></td></tr>
|
||||
</table>
|
||||
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
||||
<div class="textblock"><p >An implementation for finding the <a href="https://www.youtube.com/watch?v=5cPbNCrdotA" target="_blank">Inorder successor of a binary search tree</a> Inorder successor of a node is the next node in Inorder traversal of the Binary Tree. Inorder Successor is NULL for the last node in Inorder traversal. </p>
|
||||
<h3><a class="anchor" id="autotoc_md78"></a>
|
||||
<h3><a class="anchor" id="autotoc_md79"></a>
|
||||
Case 1: The given node has the right node/subtree</h3>
|
||||
<pre class="fragment"> * In this case, the left-most deepest node in the right subtree will
|
||||
</pre><p> come just after the given node as we go to left deep in inorder.</p><ul>
|
||||
<li>Go deep to left most node in right subtree. OR, we can also say in case if BST, find the minimum of the subtree for a given node.</li>
|
||||
</ul>
|
||||
<h3><a class="anchor" id="autotoc_md79"></a>
|
||||
<h3><a class="anchor" id="autotoc_md80"></a>
|
||||
Case 2: The given node does not have a right node/subtree</h3>
|
||||
<h4><a class="anchor" id="autotoc_md80"></a>
|
||||
<h4><a class="anchor" id="autotoc_md81"></a>
|
||||
Method 1: Use parent pointer (store the address of parent nodes)</h4>
|
||||
<ul>
|
||||
<li>If a node does not have the right subtree, and we already visited the node itself, then the next node will be its parent node according to inorder traversal, and if we are going to parent from left, then the parent would be unvisited.</li>
|
||||
<li>In other words, go to the nearest ancestor for which given node would be in left subtree.</li>
|
||||
</ul>
|
||||
<h4><a class="anchor" id="autotoc_md81"></a>
|
||||
<h4><a class="anchor" id="autotoc_md82"></a>
|
||||
Method 2: Search from the root node</h4>
|
||||
<ul>
|
||||
<li>In case if there is no link from a child node to the parent node, we need to walk down the tree starting from the root node to the given node, by doing so, we are visiting every ancestor of the given node.</li>
|
||||
|
||||
@@ -125,7 +125,7 @@ Functions</h2></td></tr>
|
||||
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
||||
<div class="textblock"><p >Implementation to check whether a number is a power of 2 or not. </p>
|
||||
<p >This algorithm uses bit manipulation to check if a number is a power of 2 or not.</p>
|
||||
<h3><a class="anchor" id="autotoc_md76"></a>
|
||||
<h3><a class="anchor" id="autotoc_md77"></a>
|
||||
Algorithm</h3>
|
||||
<p >Let the input number be n, then the bitwise and between n and n-1 will let us know whether the number is power of 2 or not</p>
|
||||
<p >For Example, If N= 32 then N-1 is 31, if we perform bitwise and of these two numbers then the result will be zero, which indicates that it is the power of 2 If N=23 then N-1 is 22, if we perform bitwise and of these two numbers then the result will not be zero , which indicates that it is not the power of 2 </p><dl class="section note"><dt>Note</dt><dd>This implementation is better than naive recursive or iterative approach.</dd></dl>
|
||||
|
||||
@@ -139,7 +139,7 @@ Functions</h2></td></tr>
|
||||
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
||||
<div class="textblock"><p >Implementation of the <a href="https://en.wikipedia.org/wiki/Selection_sort" target="_blank">Selection sort</a> implementation using recursion. </p>
|
||||
<p >The selection sort algorithm divides the input list into two parts: a sorted sublist of items which is built up from left to right at the front (left) of the list, and a sublist of the remaining unsorted items that occupy the rest of the list. Initially, the sorted sublist is empty, and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on the sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.</p>
|
||||
<h3><a class="anchor" id="autotoc_md98"></a>
|
||||
<h3><a class="anchor" id="autotoc_md99"></a>
|
||||
Implementation</h3>
|
||||
<p >FindMinIndex This function finds the minimum element of the array(list) recursively by simply comparing the minimum element of array reduced size by 1 and compares it to the last element of the array to find the minimum of the whole array.</p>
|
||||
<p >SelectionSortRecursive Just like selection sort, it divides the list into two parts (i.e.: sorted and unsorted) and finds the minimum of the unsorted array. By calling the <code>FindMinIndex</code> function, it swaps the minimum element with the first element of the list, and then solves recursively for the remaining unsorted list. </p><dl class="section author"><dt>Author</dt><dd><a href="https://github.com/Tushar-K24" target="_blank">Tushar Khanduri</a> </dd></dl>
|
||||
|
||||
Reference in New Issue
Block a user