<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Competitive programming — zero to hero - Medium]]></title>
        <description><![CDATA[This will provide you with basics about data structures and algorithms, their concepts so it will be helpful for you preparing for an interviews, pair programming rounds, problem solving and get better at it. - Medium]]></description>
        <link>https://medium.com/competitive-programming-zero-to-hero?source=rss----138f5db1d32d---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Competitive programming — zero to hero - Medium</title>
            <link>https://medium.com/competitive-programming-zero-to-hero?source=rss----138f5db1d32d---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 09:19:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/competitive-programming-zero-to-hero" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Find out the Height of Binary Tree]]></title>
            <link>https://medium.com/competitive-programming-zero-to-hero/find-out-the-height-of-binary-tree-f012a599d374?source=rss----138f5db1d32d---4</link>
            <guid isPermaLink="false">https://medium.com/p/f012a599d374</guid>
            <category><![CDATA[interview-preparation]]></category>
            <category><![CDATA[trees]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[binary-tree]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Sagar Bankar]]></dc:creator>
            <pubDate>Sun, 17 May 2020 05:37:57 GMT</pubDate>
            <atom:updated>2020-05-17T05:37:57.158Z</atom:updated>
            <content:encoded><![CDATA[<p>This is one of the popular topic asked in problem solving interviews you can see below that so many companies prefer to ask questions on binary trees. so i am explaining an approach to solve this problem with the implementation in java.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*ELzU44a5DcJ7BDAMQ0t9Tg.png" /></figure><p>To getting started with this i assume that you have a prior knowledge of tree data structure if not so please go through Tree data structure first.</p><h3><strong>Problem Statement :-</strong></h3><p>In problem statement a binary tree is given to you and your task if to find out the height of tree.(e.g. how many levels in it including root level)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*q2-tuOy2E3Zsk0fx1T-AoQ.png" /><figcaption>Binary Tree with the Height 5</figcaption></figure><h3>Finding an optimal solution..</h3><p>Below is the sudo code for finding height of an binary tree using recursion.</p><p>Step 1 : Accept the Node</p><p>Step 2 : IF Node is Null THEN Return 0</p><p>Step 3 : ELSE Return <strong>1+Max(height(Node.left -go to step 1),height(Node.right -go to step1))</strong></p><p>Step 4 : END</p><p>Lets understand by solving an problem. refer the above diagram.</p><ol><li><strong>Node = 1 is not null so return 1+Max(height(2),height(3)) = 1+Max(1,4)=5</strong></li><li>Node = 2 is not null so return 1+Max(height(null),height(null)) so height(null) = 0 so height(2) = 1 + (0,0)=1;</li><li>Node = 3 is not null so return 1 + Max(height(4),height(6)) =1+Max(3,1)=4</li><li>Node =4 is not null so return 1 + Max(height(null),height(5)) = 1 + Max(0,2)=3</li><li>Node =6 is not null so return 1 + Max(height(null),height(null)) so height(null) = 0 so height(6) = 1 + (0,0)=1;</li><li>Node =5 is not null so return 1 + Max(height(7),height(null)) so height(5)=1+Max(height(7)=1,0)=2;</li></ol><p>Please check below diagram if above steps seems complicated to you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NhjsaYgN4qULbOicVZhBOg.png" /><figcaption>Recursion of Height Function for the Problem statement.</figcaption></figure><p>Above function has <strong>time complexity of O(n)</strong> as its traversing through each of the nodes in binary tree.</p><h3><strong>Implementation using Java</strong></h3><p>If you break down the problems into smaller chunk&#39;s it makes the problem easier to solve. look at the code to solve height problem its just a 4 line of code which gives you the expected result.</p><pre><strong>int </strong>height(Node node)<br>{<br>    <strong>if</strong>(node==<strong>null</strong>)<br>        <strong>return </strong>0;<br>    <strong>else<br>        return </strong>1+Math.<em>max</em>(height(node.<strong>left</strong>),height(node.<strong>right</strong>));<br>}</pre><p>We can solve this problem using other techniques also like Memoization and dynamic programming which reduce the extra traversing which we did it using recursion.Instead we can use intermediate result storing or DP approach to achieve optimization at more granular level.We can see how we can solve this problem using Memoization and dynamic programming in next article. Stay tunned !</p><p>For full program please use below git url</p><p><a href="https://github.com/savish43/competitive-programming/blob/master/Tree/HeightOfBinaryTree.java">savish43/competitive-programming</a></p><p>Thanks !!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f012a599d374" width="1" height="1" alt=""><hr><p><a href="https://medium.com/competitive-programming-zero-to-hero/find-out-the-height-of-binary-tree-f012a599d374">Find out the Height of Binary Tree</a> was originally published in <a href="https://medium.com/competitive-programming-zero-to-hero">Competitive programming — zero to hero</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Heaps with Java Implementation]]></title>
            <link>https://medium.com/competitive-programming-zero-to-hero/understanding-heaps-with-java-implementation-70cc0ce9dc9d?source=rss----138f5db1d32d---4</link>
            <guid isPermaLink="false">https://medium.com/p/70cc0ce9dc9d</guid>
            <category><![CDATA[heap]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[computer-engin]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[min-heap]]></category>
            <dc:creator><![CDATA[Sagar Bankar]]></dc:creator>
            <pubDate>Sat, 16 May 2020 13:53:38 GMT</pubDate>
            <atom:updated>2020-05-16T13:53:38.539Z</atom:updated>
            <content:encoded><![CDATA[<p>Before getting started with Heap data structure it requires good understanding of tree data structure. If you are not aware with Trees, then you must go through Tree first before getting into this.</p><p>As Heap is one the important topics asked in interview and pair programming so having basics ready is the first step towards. So lets get started.</p><p>Heap is having specific ordering properties divided into two types <strong>Min heap</strong> and <strong>Max Heap</strong>.</p><p><strong>Min Heap</strong> is complete binary with value of each node is greater than or equal to the value of its parent. Root element has the <strong>smallest </strong>value among all other node. This applicable to all the sub tress in it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/260/0*7Jw0YO_RNndhOPLi.png" /></figure><p>Min Heap with lowest element at root level</p><p><strong>Max Heap</strong> is complete binary with with value of each node is Smaller than or equal to the value of its parent. Root element has the <strong>greatest </strong>value among all node. This applicable to all the sub tress in it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/260/0*rBYmTbubwC4Un0vW.png" /></figure><p>Max Heap with Greatest element is at top</p><h3>Why Heaps ?</h3><p>When you want to access Higher priority and lower priority element very frequently then heaps is the best choice. It has O(1) complexity while fetching lowest element (Min Heap) and Highest element (Max heaps).Below are the some of the important points about heap.</p><ul><li>Heaps are not sorted.</li><li>Nodes in heaps does not have specific relation with other nodes.</li><li>Height of heaps (Heap is complete binary tree) O(log N).</li><li>Searching other element in Heap takes O(N) because traversing is required.</li></ul><h3>How it works ?</h3><p>In Below diagram it demonstrates after deletion of root element which is smallest among all other how it works to get other element in order .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*b68bsRWPwF_B2Lh_.png" /></figure><p>Working of Heap : After deleting an element how it works to get other nodes in order.</p><h3>Operations in Heap</h3><h3>Insertion</h3><ol><li>Add the node to the bottom of the tree.</li><li>Look at the parent node. If the parent is greater than the node, swap them.</li><li>Continue comparing and swapping to allow the node to move upwards until it finds a parent node that is smaller than it.</li></ol><h3>Deletion</h3><p>Deletion is simple as we have to delete top most element which root of the tree but further process is cumbersome to get other nodes in order. that why we need to follow below steps. refer to the diagram above for reference.</p><ol><li>Take the bottom level’s right most node and move it to top, replacing the deleted node.</li><li>Compare the new root to its children. If it is larger than either child, swap the item with the smaller of the two children.</li><li>Continue comparing and swapping, move downward the node until it is smaller than both of its children.</li></ol><h3>Implementation of Heap Using Java</h3><p>We will implementing min heap with an Array. There will be main operation will be focusing on while implementing such as insertion,deletion,heapify(to order the min heap),find the parent,left child and right child positions and so on so lets get started.</p><p><strong><em>MaxSize</em></strong><em> will be size of an Array.</em></p><p><strong><em>Size </em></strong><em>is initialize to 0 and will increase when insertion happen.</em></p><p><strong>Inserting an element in an min Heap</strong></p><pre>public void insert(int element)<br>{<br>  <strong>  <em>//check the size of an array is its full then return</em></strong><em>    </em>if (size &gt;= maxSize) {<br>        return;<br>    }</pre><pre>   <strong> <em>//increase the size of an array</em></strong><em>    </em>Heap[++size] = element;<br>    int current = size;</pre><pre>   <strong> <em>//comparison of current element with parent until and unless parent is smaller than root element</em></strong><em>    </em>while (Heap[current] &lt; Heap[parent(current)]) {<br>        swap(current, parent(current));<br>        current = parent(current);<br>    }<br>}</pre><p><strong>Deletion of an element in an min Heap</strong></p><pre>public int remove()<br>{<br>    <strong><em>//to get the root element from an array which is at position 0</em></strong><em><br>    </em>int popped = Heap[<em>FRONT</em>];<br>    <br>    <strong><em>//assign a last life node to root </em></strong><em><br>    </em>Heap[<em>FRONT</em>] = Heap[size--];<br>    <br>    <strong><em>//need to do ordering as last leaf node is promoted at root level so calling heapify</em></strong><em><br>    </em>minHeapify(<em>FRONT</em>);<br>    <br>    return popped;<br>}</pre><p><strong>Ordering (swapping and comparing to get min heap in order)</strong></p><pre><strong>private void </strong>heapify(<strong>int </strong>pos)<br>{</pre><pre>    <em>// If the node is a non-leaf node and greater<br>    // than any of its child<br>    </em><strong>if </strong>(!isLeaf(pos)) {<br>        <strong>if </strong>(<strong>Heap</strong>[pos] &gt; <strong>Heap</strong>[leftChild(pos)]<br>                || <strong>Heap</strong>[pos] &gt; <strong>Heap</strong>[rightChild(pos)]) {</pre><pre>            <em>// Swap with the left child and heapify<br>            // the left child<br>            </em><strong>if </strong>(<strong>Heap</strong>[leftChild(pos)] &lt; <strong>Heap</strong>[rightChild(pos)]) {<br>                swap(pos, leftChild(pos));<br>                heapify(leftChild(pos));<br>            }</pre><pre>            <em>// Swap with the right child and heapify<br>            // the right child<br>            </em><strong>else </strong>{<br>                swap(pos, rightChild(pos));<br>                heapify(rightChild(pos));<br>            }<br>        }<br>    }<br>}</pre><p><strong>Retrieving parent, Left child and right child</strong></p><pre><strong>private int </strong>parent(<strong>int </strong>pos)<br>{<br>    <strong>return </strong>pos / 2;<br>}</pre><pre><em>// Function to return the position of the<br>// left child for the node currently at pos<br></em><strong>private int </strong>leftChild(<strong>int </strong>pos)<br>{<br>    <strong>return </strong>(2 * pos);<br>}</pre><pre><em>// Function to return the position of<br>// the right child for the node currently<br>// at pos<br></em><strong>private int </strong>rightChild(<strong>int </strong>pos)<br>{<br>    <strong>return </strong>(2 * pos) + 1;<br>}</pre><p><strong>Checking if node is leaf Node or not</strong></p><pre><em>// Function that returns true if the passed<br>// node is a leaf node<br></em><strong>private boolean </strong>isLeaf(<strong>int </strong>pos)<br>{<br>    <strong>if </strong>(pos &gt;= (<strong>size </strong>/ 2) &amp;&amp; pos &lt;= <strong>size</strong>) {<br>        <strong>return true</strong>;<br>    }<br>    <strong>return false</strong>;<br>}</pre><p>For full code please go through below git url.</p><p><a href="https://github.com/savish43/competitive-programming/blob/master/Heap/">https://github.com/savish43/competitive-programming/blob/master/Heap/</a></p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=70cc0ce9dc9d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/competitive-programming-zero-to-hero/understanding-heaps-with-java-implementation-70cc0ce9dc9d">Understanding Heaps with Java Implementation</a> was originally published in <a href="https://medium.com/competitive-programming-zero-to-hero">Competitive programming — zero to hero</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>