avatarJen-Hsuan Hsieh (Sean)

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

15745

Abstract

id="acbc">In this section, I refer to the source code on <a href="https://github.com/trekhleb">trekhleb</a>’s Github and do some modifications with my personal thoughts.</p><h2 id="730c">Complexity Analysis</h2> <figure id="f7d5"> <div> <div>

            <iframe class="gist-iframe" src="/gist/JenHsuan/ca6c8a7588463608bdd22777d3270310.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="0ffc">Implementations</h2><p id="8dc4"><b>1.Create a stack structure

</b>We can use a linked list to implement a stack. We’ll push new nodes from the tail node and pop the node from the head node.</p><figure id="b38a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*AJPFN7H2EqlBm3laPCNhzQ.png"><figcaption>Copy right@A Layman</figcaption></figure><p id="0027">Implementation for the data structure</p> <figure id="6a42"> <div> <div>

            <iframe class="gist-iframe" src="/gist/JenHsuan/4c8765394759c251bfa47734ddbfe0da.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="db10"><b>2.Push: <a href="https://readmedium.com/learning-data-structure-in-javascript-for-beginners-f5752363a4e1#06d3">append the new node to the linked list from the head node</a></b></p><ul><li>Time complexity: O(1)</li></ul><figure id="b4a8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*4S263aiULoySgpUb0SJdcA.png"><figcaption>Copy right@A Layman</figcaption></figure>
    <figure id="5bd8">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/6825321b0bb6e002a04372a77a0c8af0.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="69d7"><b>3.Pop: <a href="https://readmedium.com/learning-data-structure-in-javascript-for-beginners-f5752363a4e1#06d3">remove the head node of the linked list</a></b></p><ul><li>Time complexity: O(1)</li></ul>
    <figure id="6646">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/cf93f07c47fe5b9a9a59c61b7b5d00d1.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="2dbc">Stack related questions</h2><ul><li><b>Implement the min stack</b></li><li><b>Recursive questions:</b> backtracking</li></ul><h1 id="8804">5.Hash table</h1><p id="605b">A Hash table is a linear data structure based on an array. It’s an array of the linked list of the value.</p><h2 id="03b2">Complexity Analysis</h2>
    <figure id="2831">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/9e0b16e09f90f7069007a9f4cef07f94.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="9370">The<b> advantage </b>of the hash table is that it allows users to access data by index efficiently with constant time.</p><div id="c948"><pre>key =&gt; lookup

<span class="hljs-string">"John"</span> => Person(<span class="hljs-number">20</span>, <span class="hljs-string">"man"</span>) <span class="hljs-string">"merry"</span> => Person(<span class="hljs-number">22</span>, <span class="hljs-string">"woman"</span>) ...</pre></div><p id="e8ef">It uses the hash key to index data and deals with the collision issues with different kinds of methods.</p><div id="40de"><pre><span class="hljs-function"><span class="hljs-title">string</span> -></span> <span class="hljs-function"><span class="hljs-title">hash</span> code -></span> index</pre></div><div id="f7fa"><pre><span class="hljs-function"><span class="hljs-title">ex</span>. merry-></span> <span class="hljs-function"><span class="hljs-title">hash</span> code -></span> <span class="hljs-number">0</span></pre></div><div id="1b6e"><pre><span class="hljs-attribute">hashTable</span>[<span class="hljs-number">0</span>] = Person(<span class="hljs-number">22</span>, <span class="hljs-string">"woman"</span>)</pre></div><p id="25da">In this section, I refer to the source code on <a href="https://github.com/trekhleb">trekhleb</a>’s Github and do some modifications with my personal thoughts.</p><h2 id="2928">Implementations in JavaScript</h2><p id="545d"><b>1.Create a hash table in JavaScript</b></p><ul><li>Use an array to store values in the different linked list</li><li>Store the <b>original key</b> and<b> the value </b>to the linked list node</li></ul><figure id="33c0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*TcULoQcv-VujZWljCoNgvQ.png"><figcaption>Copy right@A Layman</figcaption></figure> <figure id="b628"> <div> <div>

            <iframe class="gist-iframe" src="/gist/JenHsuan/57127e9f79d253ac87c73f7ab527f0e7.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="5c81"><b>2.Hash function</b></p><blockquote id="279b"><p>The hash function is the most important component of a hash table which is used to map the key to a specific bucket.

— from <a href="https://leetcode.com/explore/learn/card/hash-table/182/practical-applications/1110/">Leetcode</a></p></blockquote><ol><li>Turn the key into the hashed key and use linked lists to store the new node with the same original key inorder to handle the collision</li><li>Reduce the hashed number to fit the table size</li></ol> <figure id="4588"> <div> <div>

            <iframe class="gist-iframe" src="/gist/JenHsuan/e3bf4528f3f20d6bc623fd3b6f2b63f5.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="99fd"><b>3.Insert</b></p><ul><li>Time complexity: O(1)</li><li>First, we use the hash key to decide which bucket we will store</li><li>Second, we append to the linked list with original key</li></ul>
    <figure id="03e5">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/d26e0141b8808373ad763d13b4fc4ecc.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="38ba"><b>4.Search</b></p><ul><li>Time complexity: O(1) ~ O(n)</li><li>First, we choose the bucket with the hash key</li><li>Second, we search the value in the linked list with the original key</li></ul>
    <figure id="f8f2">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/eac6e6d742580105e3899c1e96dcf397.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="0d3b"><b>5.Delete</b></p><ul><li>Time complexity: O(1)</li><li>First, we choose the bucket with the hash key</li><li>Second, we delete the value in the linked list with the original key</li></ul>
    <figure id="44ad">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/8a8ebbf3c89918769b71438f678f55aa.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="c9b0">Hash table related questions</h2><ul><li>Implement the hash set without built-in hash table functions</li><li>Implement the hash table without built-in hash table functions</li><li>Duplicates</li></ul><h2 id="9d6b">References</h2><ul><li><a href="https://www.youtube.com/watch?v=shs0KM3wKv8&amp;list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8&amp;index=4">Data Structures: Hash Tables</a></li></ul><h1 id="de8c">6. Binary search tree</h1><p id="4675">In this section, I refer to the source code on <a href="https://github.com/trekhleb">trekhleb</a>’s Github and do some modifications with my personal thoughts.</p><h2 id="f2fb">Definitions</h2><blockquote id="121f"><p>A <code>binary search tree</code> (BST), a special form of a binary tree, satisfies the binary search property:</p></blockquote><blockquote id="2d46"><p>The value in each node must be <code>greater than</code> (or equal to) any values stored in its left subtree.</p></blockquote><blockquote id="10bd"><p>The value in each node must be <code>less than</code> (or equal to) any values stored in its right subtree.

-<a href="https://leetcode.com/explore/learn/card/introduction-to-data-structure-binary-search-tree/140/introduction-to-a-bst/1017/">Leetcode</a></p></blockquote><h2 id="84ac">Complexity Analysis</h2><ul><li>Time complexity: depends on branches and the depth</li></ul><div id="885d"><pre><span class="hljs-keyword">Recursive</span> runtime </pre></div><div id="d1ec"><pre>= O(branches ^ depth), depth = <span class="hljs-built_in">log</span> <span class="hljs-built_in">N</span> (<span class="hljs-built_in">N</span> = total nodes)</pre></div><div id="c5c7"><pre><span class="hljs-built_in">Time</span> complexity of BST = O(<span class="hljs-number">2</span> ^ <span class="hljs-built_in">log</span> <span class="hljs-built_in">N</span>) (<span class="hljs-built_in">N</span> = total nodes) = O(<span class="hljs-built_in">N</span>)</pre></div><div id="1bf3"><pre><span class="hljs-built_in">Time</span> complexity of balanced BST = O(<span class="hljs-built_in">log</span> <span class="hljs-built_in">N</span>) (<span class="hljs-built_in">N</span> = total nodes) -> don'<span class="hljs-built_in">t</span> have to go through every nodes</pre></div><ul><li>Space complexity: depends on the number of nodes</li></ul><div id="431d"><pre><span class="hljs-built_in">O</span><span class="hljs-punctuation">(</span><span class="hljs-built_in">N</span><span class="hljs-punctuation">)</span> <span class="hljs-punctuation">(</span><span class="hljs-built_in">N</span> <span class="hljs-operator">=</span> <span class="hljs-variable">total</span> <span class="hljs-variable">nodes</span><span class="hljs-punctuation">)</span></pre></div><ul><li>The following table shows the time complexity of the binary tree (n total nodes)</li></ul> <figure id="956c"> <div> <div>

            <iframe class="gist-iframe" src="/gist/JenHsuan/a85cc14bd56f53d08a0752a29e873586.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="7ec2">Implementations</h2><ol><li><b>Create a binary tree structure</b></li></ol><ul><li>A single node has left child, right child, parent, and value</li></ul><figure id="fbc9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*gnvGSsGYwcw-ntd-cvwSfw.png"><figcaption>Copy right@A Layman</figcaption></figure>
    <figure id="82e3">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/127126bd8b71b45bae89844ff72f9350.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="d88b"><b>2. Insert</b></p><ul><li>Time complexity: O(log n) ~ O(n)</li></ul>
    <figure id="0694">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/54b26b12bdd11cca00cb6ed31d601d18.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="4f46"><b>3. Search</b></p><ul><li>Time complexity: O(log n) ~ O(n)</li><li>Check the node’s value. Search to the left node if the value is less than the target value, vise versa.</li></ul>
    <figure id="c873">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/858810efd4adb1bef4ab45a2edc0fc76.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="cf25"><b>4. Search the minimum node</b></p><ul><li>Time complexity: O(n)</li></ul>
    <figure id="0a5c">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/4d519e93daa0dc0b1abcf83a8ead5d81.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="2b6d"><b>5. Search and remove</b></p><ul><li>Time complexity: O(log n) ~ O(n)</li></ul>
    <figure id="a503">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/b7a07cb4ed8809f846c17a575086715e.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="693e">Properties</h2><p id="0da6"><b>1. Height (depth)</b></p><ul><li>The height of a node means the max depth of the right sub-tree and left sub-tree.</li></ul>
    <figure id="b859">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/4a208736329597f9ae546608d63118fa.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="c7a5"><b>2. Balance</b></p><ul><li>For the balanced BST, the height difference between the left sub-tree should be less than or equal to one.</li></ul>
    <figure id="2878">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/fdf665e7d445c69ba74effa7dca08ffb.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="ee48">Common problems</h2><ul><li>symmetric tree -&gt;deep copy, is same, swap (<a href="https://leetcode.com/problems/symmetric-tree/">101. Symmetric Tree</a>)</li><li>validate BST (<a href="https://leetcode.com/problems/validate-binary-search-tree/">98. Validate Binary Search Tree</a>)</li><li>lowest common ancestor</li></ul><h2 id="a3fe">References</h2><ul><li><a href="https://stackoverflow.com/questions/31999259/what-is-the-get-keyword-before-a-function-in-a-class">What is the “get” keyword before a function in a class?</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get">getter</a></li><li><a href="https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Classes/static">static</a></li></ul><h1 id="f25c">7. Binary heap</h1><p id="25af">Heap is just a tree with a <b>special property</b> or <b>constraints</b> on the values of its nodes which is called the <b>heap property the constraint.</b></p><p id="96dd">In this section, I learn to implement the data structure from<a href="https://www.udemy.com/co

Options

urse/break-away-coding-interviews-1/">Break Away: Programming And Coding Interviews</a> with slight modifications.</p><h2 id="cd8e">Complexity Analysis</h2> <figure id="a583"> <div> <div>

            <iframe class="gist-iframe" src="/gist/JenHsuan/6622dd65af9dbcfc9397c39af148de44.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="b6aa">Shape property</h2><p id="c74f">1.Every level of the binary tree in a heap is filled except perhaps the last. The heap should form a <b>complete binary tree</b></p><p id="f0dd">2.Implement by an <b>array. </b>If there is a<b> node</b> at index <b>i.</b></p><ul><li>It has a <b>left</b> child at index:<b> 2i + 1</b></li><li>It has a <b>right </b>child at index: <b>2i + 2</b></li><li>It has a <b>parent</b> at index: <b>(i — 1)/2</b></li></ul>
    <figure id="9a75">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JenHsuan/8b7487c3196f5557ec9607962d6410d2.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="627d">Heap property</h2><p id="5368">1.Minimum heap:</p><ul><li>Every node’s value should be <i>&lt;=</i> value of its children</li><li>The node with the smallest value should be the root of the tree</li></ul><p id="6b1c">2.Maximum heap:</p><ul><li>Every node value should be &gt;= value of its children</li><li>The node with the largest value should be the root of the tree</li></ul><h2 id="5d74">Implementations in JavaScript</h2><p id="4462">1.Heapify down (complexity: O(log n) for each call)</p><ul><li>Minimum heap</li></ul><div id="e604"><pre>heapifyDow<span class="hljs-meta">n</span>(<span class="hljs-keyword">index</span>) {
var leftIndex = getLeftChild<span class="hljs-meta">Index</span>(<span class="hljs-keyword">index</span>),
    rightIndex = getRightChild<span class="hljs-meta">Index</span>(<span class="hljs-keyword">index</span>),
    smallerIndex = -1;</pre></div><div id="41c1"><pre><span class="hljs-attribute">if</span> (leftIndex !== -<span class="hljs-number">1</span> &amp;&amp; rightIndex !== -<span class="hljs-number">1</span>) {
    <span class="hljs-attribute">smallerIndex</span> = getNode(leftIndex) &lt; getNode(rightIndex) ? leftIndex : rightIndex;
} <span class="hljs-attribute">else</span> if (leftIndex !== -<span class="hljs-number">1</span>) {
    <span class="hljs-attribute">smallerIndex</span> = leftIndex;
} <span class="hljs-section">else</span> {
    <span class="hljs-attribute">smallerIndex</span> = rightIndex;
}</pre></div><div id="8526"><pre>if (smallerIndex <span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span> -<span class="hljs-number">1</span>) {
     return
}</pre></div><div id="1531"><pre>if (getNode(smallerIndex) &lt; <span class="hljs-built_in">getNode</span>(index)) {
    <span class="hljs-built_in">swap</span>(smallerIndex, index);
    <span class="hljs-built_in">heapifyDown</span>(smallerIndex);
}

}</pre></div><p id="5d8d">2.Heapify up (complexity: O(log n) for each call)</p><ul><li>Minimum heap</li></ul><div id="9789"><pre>heapifyUp(<span class="hljs-keyword">index</span>) { var parentIndex = getParent<span class="hljs-meta">Index</span>(<span class="hljs-keyword">index</span>); <span class="hljs-keyword">if</span> (parentIndex !== -1 <span class="hljs-variable">&&</span> getNode(<span class="hljs-keyword">index</span>) < getNode(parentIndex)) { swap(parentIndex, <span class="hljs-keyword">index</span>); heapifyUp(parentIndex); } }</pre></div><p id="b7e1">3.Insert (complexity: O(log n))</p><div id="f730"><pre>insert(value) { <span class="hljs-keyword">this</span>.heapContainer.push(value);
<span class="hljs-keyword">this</span>.heapifyUp(<span class="hljs-keyword">this</span>.heapContainer.length - <span class="hljs-number">1</span>);
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; }</pre></div><p id="f5b8">4.Search (complexity: O(n))</p><div id="9447"><pre>find(value)
const foundItemIndices <span class="hljs-operator">=</span> []<span class="hljs-comment">;</span> for (let itemIndex <span class="hljs-operator">=</span> <span class="hljs-number">0</span><span class="hljs-comment">; itemIndex < this.heapContainer.length; itemIndex++) {</span> if (value <span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span> this.heapContainer[itemIndex]) { foundItemIndices.push(itemIndex)<span class="hljs-comment">;</span> } }

 return foundItemIndices<span class="hljs-comment">;</span>

}</pre></div><p id="b10f">5.Remove (complexity: O(log n) for each call)</p><ul><li>Replace the target node with the last node</li><li>Heapify down or heapify up the target node</li></ul><div id="70aa"><pre>remove(value) { <span class="hljs-keyword">const</span> numberOfItemsToRemove = <span class="hljs-keyword">this</span>.find(value).length; <span class="hljs-keyword">for</span> (let iteration = <span class="hljs-number">0</span>; iteration < numberOfItemsToRemove; iteration ++) { <span class="hljs-keyword">const</span> indexToRemove = <span class="hljs-keyword">this</span>.find(value).pop(); <span class="hljs-keyword">if</span> (indexToRemove === (<span class="hljs-keyword">this</span>.heapContainer.length) - <span class="hljs-number">1</span>) { <span class="hljs-keyword">this</span>.heapContainer.pop(); } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">this</span>.heapContainer[indexToRemove] = <span class="hljs-keyword">this</span>.heapContainer.pop(); }</pre></div><div id="6dd9"><pre><span class="hljs-keyword">const</span> parentNode = getNode(getParentIndex(iteration)); <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.getLeftChildIndex(indexToRemove) !== -<span class="hljs-number">1</span> && parentNode !== <span class="hljs-string">'undefined'</span>) { <span class="hljs-keyword">this</span>.heapifyDown(indexToRemove); } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">this</span>.heapifyUp(indexToRemove); } }</pre></div><div id="9fc4"><pre><span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; }</pre></div><h1 id="3c81">Summary</h1><p id="7109"><i>Thanks for your patient. I am <a href="https://www.linkedin.com/in/jen-hsuan-hsieh-6a13347a/">Sean</a>. I work as a software engineer.</i></p><p id="95c5"><i>This article is my note. Please feel free to give me advice if any mistakes. I am looking forward to your feedback.</i></p><p id="fe7b"><i>Please feel free to clap if this article can help you. Thank you.</i></p><p id="4788"><i>You can also subscribe my page on Facebook.</i></p><div id="67b1" class="link-block"> <a href="https://www.facebook.com/imalayman/?view_public_for=1143424542505127"> <div> <div> <h2>A Layman</h2> <div><h3>A Layman. 4 likes. The page sharing the information about software, photography, and coffee</h3></div> <div><p>www.facebook.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*4ZqII8Lofa1ui3gq)"></div> </div> </div> </a> </div><h1 id="e3cf">Related topics</h1><p id="0421">How to use the two-way binding in Knout.js and ReactJS?</p><div id="bb06" class="link-block"> <a href="https://readmedium.com/how-to-use-the-two-way-binding-in-knout-js-and-reactjs-afea126d0236"> <div> <div> <h2>How to use the two-way binding in Knout.js and ReactJS?</h2> <div><h3>Components built by different frameworks must have the same action in one website. What we have to realize is how to…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*SK9-uglh85Hl4wAh3OHQ_g.png)"></div> </div> </div> </a> </div><p id="7d15">Learn how to use SignalR to build a chatroom application</p><div id="ebac" class="link-block"> <a href="https://readmedium.com/learn-how-to-use-signalr-to-build-a-chatroom-application-63e8bb8049ff"> <div> <div> <h2>Learn how to use SignalR to build a chatroom application</h2> <div><h3>The Introduction of SignalR</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*j6m4jnkuRKkQ6rrkqLFeGA.png)"></div> </div> </div> </a> </div><p id="6252">My reflection of <effective sql="">:</effective></p><div id="a401" class="link-block"> <a href="https://readmedium.com/how-to-design-the-data-model-my-reflection-of-effective-sql-adba6376588a"> <div> <div> <h2>How To Design The Data Model? — My reflection of <effective sql=""></effective></h2> <div><h3><effective 61="" sql:="" specific="" ways="" to="" write="" better="" sql=""> gave me many tips for using the database.</effective></h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*trYud-lAkP2-bXo82U9bQA.png)"></div> </div> </div> </a> </div><div id="249d" class="link-block"> <a href="https://readmedium.com/how-to-design-the-index-my-reflection-of-effective-sql-b23d24212974"> <div> <div> <h2>How To Design The Index? — My reflection of <effective sql=""></effective></h2> <div><h3><effective 61="" sql:="" specific="" ways="" to="" write="" better="" sql=""> gave me many tips for using the database.</effective></h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*l4nVJqhrI1_sFMy2zQ8N0w.png)"></div> </div> </div> </a> </div><div id="6af9" class="link-block"> <a href="https://readmedium.com/whats-if-we-can-t-change-the-design-my-reflection-of-effective-sql-3704b87c49b5"> <div> <div> <h2>What if we can’t change the design? — My reflection of <effective sql=""> Part 3</effective></h2> <div><h3><effective 61="" sql:="" specific="" ways="" to="" write="" better="" sql=""> gave me many tips for using the database.</effective></h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*jqUMxB_afM-552DjqBo3gA.png)"></div> </div> </div> </a> </div><p id="75de">IT & Network:</p><div id="79e8" class="link-block"> <a href="https://readmedium.com/back-to-the-basic-introduction-to-dns-f63aebe7bee7"> <div> <div> <h2>Back to the basic: Introduction to DNS</h2> <div><h3>I decided to back to the basic and enhance these bits of knowledge. I also expect that this article can help someone…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*m4nxtUjDkrX9lClzHnnboA.png)"></div> </div> </div> </a> </div><p id="e02d">Database:</p><div id="74a6" class="link-block"> <a href="https://readmedium.com/learning-sql-server-part-1-locking-in-sql-server-807f79cbe9dd"> <div> <div> <h2>Learning SQL server part.1 — lock and concurrency in SQL server</h2> <div><h3>Even though I used the SQL server for a long time, I didn’t know it very well. However, I received an alert with the…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*f4tAhp00tEK51-KU73J5Yw.png)"></div> </div> </div> </a> </div><p id="a3ee">Software testing:</p><div id="c11a" class="link-block"> <a href="https://readmedium.com/software-testing-automatic-test-with-newman-485c49d7dc1d"> <div> <div> <h2>Software testing-Automatic test with Newman</h2> <div><h3>undefined</h3></div> <div><p>undefined</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*cY7YmhMCMMiXr02fvrqH9A.png)"></div> </div> </div> </a> </div><p id="d27c">Debugging:</p><div id="0b33" class="link-block"> <a href="https://readmedium.com/service-fabric-trouble-shooting-part-1-how-to-install-and-create-service-fabric-cluster-64aeaaf69657"> <div> <div> <h2>Service Fabric trouble shooting part.1 — How to install and create Service Fabric cluster?</h2> <div><h3>Introduction</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*4P7GXp1lFjMzQUeC1sCweg.png)"></div> </div> </div> </a> </div><div id="4ce1" class="link-block"> <a href="https://readmedium.com/how-to-attach-visual-studio-code-to-the-asp-net-core-process-in-the-service-fabric-cluster-831d83b7c0b0"> <div> <div> <h2>Service Fabric trouble shooting part.2 — How to attach Visual studio code?</h2> <div><h3>ASP NET core, Service Fabric</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*jBgxd0uWizZ8I4pH57RfhQ.png)"></div> </div> </div> </a> </div><p id="c65c">DevOps:</p><div id="0acf" class="link-block"> <a href="https://readmedium.com/devops-introduction-to-opserver-10427f8c5139"> <div> <div> <h2>[DevOps] Introduction to Opserver</h2> <div><h3>Introduction</h3></div> <div><p> to Opserver Introductionmedium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*yUfKnntVMJ7p_caQ4G2j3Q.png)"></div> </div> </div> </a> </div></article></body>

Learning data structure in JavaScript for beginners

Introduction

It’s the note for learning data structure in JavaScript. I studied the source code form trekhleb’s Github repository. It’s a nice material for the JavaScript developers and beginners to learn data structure.

Besides trekhleb’s Github, I also refer to Break Away: Programming And Coding Interviews from Udemy and the videos from Hackerrank. This course uses Java to implement and explain data structures.

This article summarizes and includes the following data structure.

The table shows the time complexity of the common operations. The implementations of stack and queue are based on the linked list. They allow users to insert or delete nodes efficiently.

To have better performance for traversing the entire container, we may consider using a hash table or binary search tree. It depends on whether you want to order data or not.

  1. Linked list
  2. Doubly linked list
  3. Queue
  4. Stack
  5. Hash table
  6. Binary search tree
  7. Binary heap

1. Linked list

The single linked list is a unidirectional list.

The advantage of a linked list is that it allows users to insert or delete nodes efficiently with constant time.

However, the linked list is not good at access data. We are not allowed to access data with index in the linked list. We can only find the target data by traversing the container.

Complexity Analysis

  • O(1): append, prepend, deleteHead
  • O(n): insertAt, deleteAt, delete, search

Implementations

In this section, I refer to the source code trekhleb’s Github and do some modifications with my personal thoughts.

1.Create a linked list

A single linked list has the head and the tail node. Each node has a value field and a pointer to the next node

Copy right@A Layman

Implementation for the data structure

2.Append

Copy right@A Layman
  • The time complexity is O(1) or O(n) and it depends on how we implement the linked list

3. Prepend

  • Time complexity: O(1)

4. Insert at a specific index

  • Time complexity: O(n)

5.Delete the head node

  • Time complexity: O(1)

6. Delete the node for the specific position

  • Time complexity: O(n)

7. Search and Delete

The deleting operation has to follow these orders because the linked list doesn’t have the pointer to point the previous node.

  • Delete the head node if the value equals the target value
  • Delete the immediate node if the value equals the target value
  • Delete the tail node if the value equals the target value

8.Search

Go through the entire list and return the target node with the target value or the callback function returns true

References

Related LeetCode questions

2. Doubly linked list

A doubly linked list is a bidirectional list. It has an extra pointer called the previous pointer to link the previous node. The main difference between the linked list and the doubly list is the search and delete operation.

In this section, I refer to the source code on trekhleb’s Github and do some modifications with my personal thoughts.

Complexity Analysis

  • O(1): append, prepend, deleteHead
  • O(n): insertAt, deleteAt, delete, search

Implementations

1.Create a linked list structure

A double linked list has the head and the tail node. A node has a value, a pointer to the next node, and a previous pointer to the previous node which is different from linked list.

Copy right@A Layman

2.Append

  • It’s the same as the linked list. The difference is to set the previous pointer of the new node to the tail node.

3. Prepend

  • Insert new node from the head node. It’s the same as the linked list. The difference is to set the previous pointer of the head node to the new node.

4.Delete the head node

  • Time complexity: O(1)
  • The difference is to set the previous pointer of the head node to null.

5. Search and Delete

  • Time complexity: O(n)
  • Unlike the linked list, the deleting operation doesn’t have to follow orders because the doubly linked list has the previous pointer.

3. Queue

The queue is a linear data structure that is based on the linked list. The character of the queue is that it’s a FIFO (first in first out) data structure.

In this section, I refer to the source code on trekhleb’s Github and do some modifications with my personal thoughts.

Complexity Analysis

Implementations

1.Create a queue structure

We can use a linked list to implement a queue. We’ll push new nodes from the tail node and pop the node from the head node.

Copy right@A Layman

Implementation for the data structure:

2. Enqueue: append a node at the end of the linked list (back)

  • Time complexity: O(1)
Copy right@A Layman

3.Dequeue: remove the head node of the linked list (front)

  • Time complexity: O(1)

Queue related questions

  • Implement a circular queue (enqueue, dequeue, front, rear, isEmpty)
  • Implement a queue by 2 stacks (enqueue, dequeue, front, rear, isEmpty)
  • BFS questions

References

4. Stack

Like queue, the stack is a data structure that is based on the linked list. The character of the stack is that it’s a LIFO (last in first out) data structure.

In this section, I refer to the source code on trekhleb’s Github and do some modifications with my personal thoughts.

Complexity Analysis

Implementations

1.Create a stack structure We can use a linked list to implement a stack. We’ll push new nodes from the tail node and pop the node from the head node.

Copy right@A Layman

Implementation for the data structure

2.Push: append the new node to the linked list from the head node

  • Time complexity: O(1)
Copy right@A Layman

3.Pop: remove the head node of the linked list

  • Time complexity: O(1)

Stack related questions

  • Implement the min stack
  • Recursive questions: backtracking

5.Hash table

A Hash table is a linear data structure based on an array. It’s an array of the linked list of the value.

Complexity Analysis

The advantage of the hash table is that it allows users to access data by index efficiently with constant time.

key => lookup
"John" => Person(20, "man")
"merry" => Person(22, "woman")
...

It uses the hash key to index data and deals with the collision issues with different kinds of methods.

string -> hash code -> index
ex. merry-> hash code -> 0
hashTable[0] = Person(22, "woman")

In this section, I refer to the source code on trekhleb’s Github and do some modifications with my personal thoughts.

Implementations in JavaScript

1.Create a hash table in JavaScript

  • Use an array to store values in the different linked list
  • Store the original key and the value to the linked list node
Copy right@A Layman

2.Hash function

The hash function is the most important component of a hash table which is used to map the key to a specific bucket. — from Leetcode

  1. Turn the key into the hashed key and use linked lists to store the new node with the same original key inorder to handle the collision
  2. Reduce the hashed number to fit the table size

3.Insert

  • Time complexity: O(1)
  • First, we use the hash key to decide which bucket we will store
  • Second, we append to the linked list with original key

4.Search

  • Time complexity: O(1) ~ O(n)
  • First, we choose the bucket with the hash key
  • Second, we search the value in the linked list with the original key

5.Delete

  • Time complexity: O(1)
  • First, we choose the bucket with the hash key
  • Second, we delete the value in the linked list with the original key

Hash table related questions

  • Implement the hash set without built-in hash table functions
  • Implement the hash table without built-in hash table functions
  • Duplicates

References

6. Binary search tree

In this section, I refer to the source code on trekhleb’s Github and do some modifications with my personal thoughts.

Definitions

A binary search tree (BST), a special form of a binary tree, satisfies the binary search property:

The value in each node must be greater than (or equal to) any values stored in its left subtree.

The value in each node must be less than (or equal to) any values stored in its right subtree. -Leetcode

Complexity Analysis

  • Time complexity: depends on branches and the depth
Recursive runtime 
= O(branches ^ depth), depth = log N (N = total nodes)
Time complexity of BST
= O(2 ^ log N) (N = total nodes)
= O(N)
Time complexity of balanced BST 
= O(log N) (N = total nodes) -> don't have to go through every nodes
  • Space complexity: depends on the number of nodes
O(N) (N = total nodes)
  • The following table shows the time complexity of the binary tree (n total nodes)

Implementations

  1. Create a binary tree structure
  • A single node has left child, right child, parent, and value
Copy right@A Layman

2. Insert

  • Time complexity: O(log n) ~ O(n)

3. Search

  • Time complexity: O(log n) ~ O(n)
  • Check the node’s value. Search to the left node if the value is less than the target value, vise versa.

4. Search the minimum node

  • Time complexity: O(n)

5. Search and remove

  • Time complexity: O(log n) ~ O(n)

Properties

1. Height (depth)

  • The height of a node means the max depth of the right sub-tree and left sub-tree.

2. Balance

  • For the balanced BST, the height difference between the left sub-tree should be less than or equal to one.

Common problems

References

7. Binary heap

Heap is just a tree with a special property or constraints on the values of its nodes which is called the heap property the constraint.

In this section, I learn to implement the data structure fromBreak Away: Programming And Coding Interviews with slight modifications.

Complexity Analysis

Shape property

1.Every level of the binary tree in a heap is filled except perhaps the last. The heap should form a complete binary tree

2.Implement by an array. If there is a node at index i.

  • It has a left child at index: 2i + 1
  • It has a right child at index: 2i + 2
  • It has a parent at index: (i — 1)/2

Heap property

1.Minimum heap:

  • Every node’s value should be <= value of its children
  • The node with the smallest value should be the root of the tree

2.Maximum heap:

  • Every node value should be >= value of its children
  • The node with the largest value should be the root of the tree

Implementations in JavaScript

1.Heapify down (complexity: O(log n) for each call)

  • Minimum heap
heapifyDown(index) {
    var leftIndex = getLeftChildIndex(index),
        rightIndex = getRightChildIndex(index),
        smallerIndex = -1;
if (leftIndex !== -1 && rightIndex !== -1) {
        smallerIndex = getNode(leftIndex) < getNode(rightIndex) ? leftIndex : rightIndex;
    } else if (leftIndex !== -1) {
        smallerIndex = leftIndex;
    } else {
        smallerIndex = rightIndex;
    }
if (smallerIndex === -1) {
         return
    }
if (getNode(smallerIndex) < getNode(index)) {
        swap(smallerIndex, index);
        heapifyDown(smallerIndex);
    }
}

2.Heapify up (complexity: O(log n) for each call)

  • Minimum heap
heapifyUp(index) {
    var parentIndex = getParentIndex(index);
    if (parentIndex !== -1 &&
        getNode(index) < getNode(parentIndex)) {
        swap(parentIndex, index);
        heapifyUp(parentIndex);
    }
}

3.Insert (complexity: O(log n))

insert(value) {
    this.heapContainer.push(value);    
    this.heapifyUp(this.heapContainer.length - 1);    
    return this;
}

4.Search (complexity: O(n))

find(value)    
    const foundItemIndices = [];
    for (let itemIndex = 0; itemIndex < this.heapContainer.length; itemIndex++) {
        if (value === this.heapContainer[itemIndex]) {
            foundItemIndices.push(itemIndex);
        }
     }
     
     return foundItemIndices;
}

5.Remove (complexity: O(log n) for each call)

  • Replace the target node with the last node
  • Heapify down or heapify up the target node
remove(value) {
    const numberOfItemsToRemove = this.find(value).length;
    for (let iteration = 0; iteration < numberOfItemsToRemove; iteration ++) {
        const indexToRemove = this.find(value).pop();
        if (indexToRemove === (this.heapContainer.length) - 1) {
            this.heapContainer.pop();
        } else {
            this.heapContainer[indexToRemove] = this.heapContainer.pop();
        }
const parentNode = getNode(getParentIndex(iteration));
        if (this.getLeftChildIndex(indexToRemove) !== -1 && parentNode !== 'undefined') {
            this.heapifyDown(indexToRemove);
        } else {
            this.heapifyUp(indexToRemove);
        }
    }
return this;
}

Summary

Thanks for your patient. I am Sean. I work as a software engineer.

This article is my note. Please feel free to give me advice if any mistakes. I am looking forward to your feedback.

Please feel free to clap if this article can help you. Thank you.

You can also subscribe my page on Facebook.

Related topics

How to use the two-way binding in Knout.js and ReactJS?

Learn how to use SignalR to build a chatroom application

My reflection of :

IT & Network:

Database:

Software testing:

Debugging:

DevOps:

Software Development
Algorithms
Data Structures
Interview
JavaScript
Recommended from ReadMedium