<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Omar</title>
    <description>The latest articles on DEV Community by Omar (@omarkhatib).</description>
    <link>https://dev.to/omarkhatib</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F403345%2F6aae351d-fcb9-455f-b189-a64ad951c437.jpg</url>
      <title>DEV Community: Omar</title>
      <link>https://dev.to/omarkhatib</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/omarkhatib"/>
    <language>en</language>
    <item>
      <title>Message Queue</title>
      <dc:creator>Omar</dc:creator>
      <pubDate>Sat, 29 Apr 2023 11:52:08 +0000</pubDate>
      <link>https://dev.to/omarkhatib/message-queue-2887</link>
      <guid>https://dev.to/omarkhatib/message-queue-2887</guid>
      <description>&lt;p&gt;Hello, this article is an attempt to explain message queue without particular tool, but rather a big picture of what they offer without entering details of each message queue technologies like kafka, rabbitMQ, nats&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Queue
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--goPDO1Wu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media2.giphy.com/media/6kyrz1j5uhJdK/giphy.gif%3Fcid%3Decf05e47x5pzdotbfx3u9jowdjm9fhcbnlf4fljq03o6cuvw%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--goPDO1Wu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media2.giphy.com/media/6kyrz1j5uhJdK/giphy.gif%3Fcid%3Decf05e47x5pzdotbfx3u9jowdjm9fhcbnlf4fljq03o6cuvw%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="queue" width="320" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A queue also known as FIFO ( First In First Out ) , it's a data structure that allows to operate the data in the order they have been inserted.&lt;br&gt;
Example: we have 3 persons A, B and C that are waiting the store to open. First A will enter the queue so now we have person &lt;code&gt;A&lt;/code&gt; in the queue&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;------------
| A
------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then after couple of minutes person &lt;code&gt;B&lt;/code&gt; have arrived, so now we have in the Queue two persons &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;------------
| B A
------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;each one of them waiting for their turn, now the store opens,&lt;br&gt;
the store will start taking orders of person &lt;code&gt;A&lt;/code&gt; first because they entered first. So now we only have &lt;code&gt;B&lt;/code&gt; in queue&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;------------
| B
------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The person &lt;code&gt;C&lt;/code&gt; have arrived and entered the queue, so the person &lt;code&gt;C&lt;/code&gt; will wait person &lt;code&gt;B&lt;/code&gt; to finish.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;------------
| C B
------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and so on.&lt;br&gt;
With same concept in computation we can imagine the persons are tasks, or giving data we need to process in order.&lt;br&gt;
This is the queue in it's simplest form, we have many queues types, like a priority queue, where each person have a priority and they are served based on their priorities.&lt;br&gt;
And we have Concurrent Queues that works with multiple threads, and so on.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is message queue
&lt;/h2&gt;

&lt;p&gt;A message queue is also a concurrent data structure that hold messages, those messages can be any kind of data in addition to metadata that can help the messages system to manage the messages, like it's source and it's destination, how long the message will live and so on...&lt;/p&gt;

&lt;h2&gt;
  
  
  Why we need a message queue
&lt;/h2&gt;

&lt;p&gt;Let's take a study case, where we have a service &lt;code&gt;A&lt;/code&gt; that collect some articles from internet and it send them to service &lt;code&gt;B&lt;/code&gt;, and let's assume that time of processing the article is slower than collecting it. If &lt;code&gt;A&lt;/code&gt; keeps sending many articles to service &lt;code&gt;B&lt;/code&gt; while service &lt;code&gt;B&lt;/code&gt; is already processing an article, the article will be lost, so we need a kind of data persistence were we can get the article &lt;br&gt;
that process this data.&lt;br&gt;
A message channel acts as a temporary buffer for the receiver. Unlike the direct request-response communication style, messaging is inherently asynchronous, as sending a message doesn’t require the receiving service to be online.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pattern of message queues
&lt;/h2&gt;

&lt;p&gt;1- Point to point&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DqOtPKxT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1ctckgodaw7v3vdelxu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DqOtPKxT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1ctckgodaw7v3vdelxu.png" alt="p2p" width="617" height="104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Point to point is simplest form of message queuing where there is a single producer ( the service that emit the message ) and single consumer ( the service that receive the message )&lt;/p&gt;

&lt;p&gt;2- Competing Consumers&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--25JsCgzC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/al8iydgcycnkm46a0jl3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--25JsCgzC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/al8iydgcycnkm46a0jl3.png" alt="cp" width="493" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Where there is one producer and multiple consumers, how ever only one consumer will receive the message.&lt;/p&gt;

&lt;p&gt;3- Publish-Subscribe&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_HvmJItS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvo00gklz5kr6yx70a88.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_HvmJItS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvo00gklz5kr6yx70a88.png" alt="pub-sub" width="504" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the Publish-Subscribe Channel delivers a copy of the message to each consumer.&lt;/p&gt;

&lt;p&gt;Those are not the only patterns, but it gives a brief idea on how we can use multiple queues and link them together.&lt;br&gt;
Like we can use the request-reply pattern, where producer sends the message in a channel &lt;code&gt;A&lt;/code&gt; and the consumer reply to the consumer with another channel &lt;code&gt;B&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f9ac5Tmt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hu1z2ikdjracurrxt9p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f9ac5Tmt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hu1z2ikdjracurrxt9p.png" alt="request-reply" width="372" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Guaranty
&lt;/h2&gt;

&lt;p&gt;Message brokers are designed to be scale horizontally, that means they are meant to be distributed.&lt;br&gt;
And like any distributed system it comes with it's own challenges, and each broker take different implementation with it's tradeoffs.&lt;/p&gt;

&lt;p&gt;For message delivery guaranty many message brokers support:&lt;/p&gt;

&lt;p&gt;1- At-most-once delivery. This means that a message will never be delivered more than once but messages might be lost.&lt;/p&gt;

&lt;p&gt;2- At-least-once delivery. This means that we'll never lose a message but a message might end up being delivered to a consumer more than once.&lt;/p&gt;

&lt;p&gt;3- Exactly-once delivery. The holy grail of messaging. All messages will be delivered exactly one time.&lt;/p&gt;

&lt;p&gt;it's impossible to achieve exactly once, because the consumer instance has to delete a message from the channel once it’s done processing it so that another instance won’t read it. If the consumer instance deletes the message before processing it, there is a risk it could crash after deleting the message but before processing it, causing the message to be lost for good. On the other hand, if the consumer instance deletes the message only after processing it, there is a risk that it crashes after processing the message but before deleting it, causing the same message to be read again later on.&lt;/p&gt;

&lt;p&gt;Because of that, there is no such thing as exactly-once message delivery. So the best a consumer can do is to simulate exactly-once message processing by requiring messages to be idempotent and deleting them from the channel only after they have been processed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failures
&lt;/h2&gt;

&lt;p&gt;When a consumer instance fails to process a message, the visibility timeout triggers, and the message is eventually delivered to another instance. What happens if processing a specific message consistently fails with an error, though? To guard against the message being picked up again, we need to limit the maximum number of times the same message can be read from the channel.&lt;/p&gt;

&lt;p&gt;To enforce a maximum number of retries, the broker can stamp messages with a counter that keeps track of the number of times the message has been delivered to a consumer. If the broker doesn’t support this functionality, the consumer can implement it.&lt;/p&gt;

&lt;p&gt;Once we have a way to count the number of times a message has been retried, we still have to decide what to do when the maximum is reached. A consumer shouldn’t delete a message without processing it, as that would cause data loss. But what it can do is remove the message from the channel after writing it to a dead letter channel — a channel that buffers messages that have been retried too many times.&lt;/p&gt;

&lt;p&gt;This way, messages that consistently fail are not lost forever but merely put on the side so that they don’t pollute the main channel, wasting the consumer’s resources. A human can then inspect these messages to debug the failure, and once the root cause has been identified and fixed, move them back to the main channel to be reprocessed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Backlogs
&lt;/h2&gt;

&lt;p&gt;One of the main advantages of using a message broker is that it makes the system more robust to outages. This is because the producer can continue writing messages to a channel even if the consumer is temporarily unavailable. As long as the arrival rate of messages is lower than or equal to their deletion rate, everything is great. However, when that is no longer true and the consumer can’t keep up with the producer, a backlog builds up.&lt;/p&gt;

&lt;p&gt;A messaging channel introduces a bimodal behavior in the system. In one mode, there is no backlog, and everything works as expected. In the other, a backlog builds up, and the system enters a degraded state. The issue with a backlog is that the longer it builds up, the more resources and/or time it will take.&lt;/p&gt;

&lt;p&gt;There are several reasons for backlogs, for example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;more producer instances come online, and/or their throughput increases, and the consumer can’t keep up with the arrival rate&lt;/li&gt;
&lt;li&gt;the consumers performance has degraded and messages take longer to be processed, decreasing the deletion rate&lt;/li&gt;
&lt;li&gt;the consumer fails to process a fraction of the messages, which are picked up again until they eventually end up in the dead letter channel. This wastes the consumer’s resources and delays the processing of healthy messages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To detect and monitor backlogs, we can measure the average time a message waits in the channel to be read for the first time. Typically, brokers attach a timestamp of when the message was first written to it. The consumer can use that timestamp to compute how long the message has been waiting in the channel by comparing it to the timestamp taken when the message was read. Although the two timestamps have been generated by two physical clocks that aren’t perfectly synchronized, this measure generally provides a good warning sign of backlogs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fault isolation
&lt;/h2&gt;

&lt;p&gt;A single producer instance that emits poisonous messages ( message that repeatedly fail to be processed ) can degrade the consumer and potentially create a backlog because these messages are processed multiple times before they end up in the dead-letter channel. Therefore, it’s important to find ways to deal with poisonous messages before that happens.&lt;/p&gt;

&lt;p&gt;If messages are decorated with an identifier of the source that generated them, the consumer can treat them differently. For example, suppose messages from a specific user fail consistently. In that case, the consumer could decide to write these messages to an alternate low-priority channel and remove them from the main channel without processing them. The consumer reads from the slow channel but does so less frequently than the main channel, isolating the damage a single bad user can inflict to the others.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Go and copies</title>
      <dc:creator>Omar</dc:creator>
      <pubDate>Fri, 16 Jul 2021 17:50:40 +0000</pubDate>
      <link>https://dev.to/omarkhatib/go-and-copies-50dg</link>
      <guid>https://dev.to/omarkhatib/go-and-copies-50dg</guid>
      <description>&lt;p&gt;Copying concept was always weird for me , when I learned GO it was like why? Why! Why GO favorite option is to work on a copy?!&lt;br&gt;
Today I make discovery that I will like to share.&lt;/p&gt;

&lt;p&gt;I was reading a book "composing software" and I see an example about Shared State.&lt;br&gt;
"Shared-state concurrency means that concurrent computations communicate through reading and updating a shared location in memory."-&lt;a href="https://slikts.github.io/concurrency-glossary/?id=shared-state-vs-message-passing-distributed-state"&gt;source&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I know the title said GO but the examples that let me understand the importance of copying in GO was an example in the book which is JavaScript , so the examples are in JavaScript.&lt;/p&gt;

&lt;p&gt;Let's take a look at this example taken from the book.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Shared state&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Mutates shared state&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;x1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Mutates shared state&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;x2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;x1&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;x2&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 6&lt;/span&gt;

&lt;span class="c1"&gt;// This example is exactly equivalent to the above, except...&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;y1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;y2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// ...the order of the function calls is reversed...&lt;/span&gt;
&lt;span class="nf"&gt;y2&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;y1&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// ... which changes the resulting value:&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this code seems logical , but let's say we have 2 threads (or routines) accessing the same object x in same time.&lt;br&gt;
both of them are modifying the same object , so the output depends on race condition.&lt;/p&gt;

&lt;p&gt;so to avoid this we use (yes you guess it) a copy!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({...&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({...&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;inc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cm"&gt;/*
Because the functions don't mutate, you can call
these functions as many times as you want, in any order,
without changing the result of other function calls.
*/&lt;/span&gt;

&lt;span class="c1"&gt;// These calls do nothing:&lt;/span&gt;
&lt;span class="nf"&gt;inc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;inc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the three dots ... are spread operator basically it will copy the propriety of the object and the function will return a new copy with modified value. Once I read this code I directly remembered GO , the idea in GO that everything is a copy was weird for me. And as we know go main feature is concurrency so everything as a copy seemed a very good and safe choice by go designers.&lt;br&gt;
so the advantage of working on a copy is safety , isolation , multi threading  , but it also have disadvantage like deficiency.&lt;/p&gt;

&lt;p&gt;That's why we like in GO to avoid as much as we can using pass as pointer.&lt;/p&gt;

&lt;p&gt;that was my discovery today that I liked to share with community , please if you have more information that can help us , share it with us!&lt;/p&gt;

</description>
      <category>go</category>
      <category>functional</category>
      <category>concurrency</category>
    </item>
    <item>
      <title>#011 DS&amp;A - Sorting</title>
      <dc:creator>Omar</dc:creator>
      <pubDate>Thu, 08 Oct 2020 19:38:26 +0000</pubDate>
      <link>https://dev.to/omarkhatib/011-ds-a-sorting-2kd6</link>
      <guid>https://dev.to/omarkhatib/011-ds-a-sorting-2kd6</guid>
      <description>&lt;h2&gt;
  
  
  Sorting Techniques
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Insertion sort algorithm and analysis
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Insertion-sort(A)
{
    for j = 2 to A.length
        key = A[j]
        // insert A[j] into sorted sequence A[1...j-1]
        i = j-1
        while(i&amp;gt;0 and A[i] &amp;gt; key)
            A[i+1] = A[i]
            i = i-1
        A[i+1] = key
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;every time you visit an element you check the one before it if it's greater swap them , if their is a swap you compare the new element with old elements in array.&lt;/p&gt;

&lt;p&gt;Worst case is &lt;code&gt;Teta(n^2)&lt;/code&gt; is if the sorted list is backward sorted from biggest to lowest  , and best case is &lt;code&gt;Omega(n)&lt;/code&gt; if array is already sorted , and space complexity is &lt;code&gt;O(1)&lt;/code&gt; called &lt;code&gt;inplace&lt;/code&gt; which mean we don't use extra spaces.&lt;/p&gt;

&lt;p&gt;We can't optimize it if we apply binary searching the comparison between new element and old elements is &lt;code&gt;O(log n)&lt;/code&gt; and movement required is &lt;code&gt;O(n)&lt;/code&gt; , in doubly linked list it require &lt;code&gt;O(n)&lt;/code&gt; for comparison and &lt;code&gt;O(1)&lt;/code&gt; for movement. In both cases we have bottleneck to &lt;code&gt;O(n)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Merge sort algorithm and analysis
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MERGE(A, p , q , r)
{
    n1 = q-p+1
    n2 = r-q
    let L[1...n+1] and R[1 to n2+1] be the new arrays
    for(i=1 to n1)
        l[i] = A[p+i-1]
    for(j=1 to n2)
        R[j] = A[q+j]
    L[n1+1] = inf
    R[n2+1] = inf
    i = 1 , j = 1
    for(k = p to r)
        if(L[i] &amp;lt;= R[j])
            A[k] = L[i]
            i=i+1
        else
            A[k] = R[j]
            j=j+1
}
// A is array , p is start index , q is end of sorted arr1 index , r is last index
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The merge here work having a main array contain 2 sorted arrays inside of it&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;3&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;5&lt;/th&gt;
&lt;th&gt;6&lt;/th&gt;
&lt;th&gt;7&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;this will be divided into 2 arrays &lt;code&gt;L&lt;/code&gt; and &lt;code&gt;R&lt;/code&gt; &lt;br&gt;
&lt;code&gt;L&lt;/code&gt; from &lt;code&gt;p&lt;/code&gt; to &lt;code&gt;q&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;3&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Inf&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;R&lt;/code&gt; from &lt;code&gt;q+1&lt;/code&gt; to &lt;code&gt;r&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;3&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;Inf&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;after dividing the main array to 2 it will start to compare &lt;code&gt;R[j]&lt;/code&gt; with &lt;code&gt;L[i]&lt;/code&gt; if will increment the index of the lower like if &lt;code&gt;L[i] &amp;lt; R[j]&lt;/code&gt; &lt;code&gt;i&lt;/code&gt; will be incremented by &lt;code&gt;1&lt;/code&gt; , and inserting them into array , and note that &lt;code&gt;Inf&lt;/code&gt; here is large number with same type of data.&lt;/p&gt;

&lt;p&gt;then array will become&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;3&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;5&lt;/th&gt;
&lt;th&gt;6&lt;/th&gt;
&lt;th&gt;7&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;this algorithm will take &lt;code&gt;O(n)&lt;/code&gt; for dividing arrays and &lt;code&gt;n+n&lt;/code&gt; to copy elements back to array is &lt;code&gt;O(n)&lt;/code&gt; so time taken is &lt;code&gt;O(n)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;space complexity is &lt;code&gt;O(n)&lt;/code&gt; also.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;merge-sort(A, p , r) - T(n)
{
    if p&amp;lt;r
        q=L[(p+r)/2]
        merge-sort(A,p,q) - T(n/2)
        merge-sort(A,q+1,r) - T(n/2)
        merge(A,p,q,r) - O(n)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqgmb2ct216daib20qdfi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqgmb2ct216daib20qdfi.gif" alt="Merge-sort-example-300px.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the size of stack required for merge-sort is &lt;code&gt;log(n)+1&lt;/code&gt; so space complexity is &lt;code&gt;o(log n)&lt;/code&gt; and merge will require is &lt;code&gt;O(n)&lt;/code&gt; so total is &lt;code&gt;O(n) + O(log n)&lt;/code&gt; which &lt;code&gt;n&lt;/code&gt; is dominated so &lt;code&gt;O(n)&lt;/code&gt; is space complexity.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;T(n) = 2*T(n/2)+O(n) = O(n*log(n)&lt;/code&gt; we use here the master theorem to calculate time complexity&lt;/p&gt;

&lt;p&gt;so what ever is the size of problem it will always take &lt;code&gt;O(n*log(n))&lt;/code&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Quick sort algorithm and analysis
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PARTITION(A,p,r)
{
    x = A[r]
    i = p-1
    for(j=p to r-1)
    {
        if(A[j] &amp;lt;= x)
        {
            i=i+1
            exchange A[i] with A[j]
        }
    }
    exhange A[i+1] with A[r]
    return i+1
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Partition algorithm is about putting the last element of array in it's right place where left of it are values which less (no sorted) of it's value and right is greater (not sorted) . Since the &lt;code&gt;for&lt;/code&gt; will loop  &lt;code&gt;n&lt;/code&gt; times so time complexity will be &lt;code&gt;O(n)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;QUICKSORT(A,p,r) - T(n)
{
    if(p&amp;lt;r)
    {
        q = PARTITION(A,p,r) - O(n)
        QUICKSORT(A,p,q-1) - T(n/2)
        QUICKSORT(A,q+1,r) - T(n/2)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F6%2F6a%2FSorting_quicksort_anim.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F6%2F6a%2FSorting_quicksort_anim.gif" alt="Sorting quicksort anim.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fa%2Faf%2FQuicksort-diagram.svg%2F200px-Quicksort-diagram.svg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fa%2Faf%2FQuicksort-diagram.svg%2F200px-Quicksort-diagram.svg.png" alt="img"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;it will keep sub-Quick-sorting and return every time it will face  &lt;code&gt;p==r&lt;/code&gt; or  &lt;code&gt;p&amp;gt;r&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;worst space complexity is &lt;code&gt;O(n)&lt;/code&gt; and average is &lt;code&gt;O(log n)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;time complexity is &lt;code&gt;T(n)=2*T(n/2)+O(n) = Teta(n log(n) )&lt;/code&gt;  using the master theorem&lt;/p&gt;

&lt;p&gt;the worst case is where the number the last element is in the right order.&lt;/p&gt;

&lt;p&gt;time complexity will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = T(n-1) + O(n)
     = T(n-1)+cn
     = T(n-2)+c*(n-1)+c*n
     = T(n-3)+c*(n-2)+c(n-1)+c*n
     ...
     = c+2c+3c+...+c*n
     = O(n^2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Introduction to heaps
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F3%2F38%2FMax-Heap.svg%2F1280px-Max-Heap.svg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F3%2F38%2FMax-Heap.svg%2F1280px-Max-Heap.svg.png" alt="img"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;heap trees are is a binary tree or 3-ary tree or n-ary tree.&lt;/p&gt;

&lt;p&gt;let's talk about binary tree heap only , heap have their root is max heap and all nodes sub-tree should be smaller than the parent node.&lt;/p&gt;

&lt;p&gt;A complete Binary tree of height &lt;strong&gt;h&lt;/strong&gt; has 2h-1 nodes. Out of these 2h-1 are leaf nodes and rest (2h-1-1 are non-leaf)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2Fcomplete-binary-tree.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2Fcomplete-binary-tree.png" alt="complete binary tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2Fcomplete-binary-tree_1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2Fcomplete-binary-tree_1.png" alt="complete binary tree_1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Canonical labeling of nodes
&lt;/h3&gt;

&lt;p&gt;Label the Nodes in the level-wise fashion from left to right, as shown below:&lt;br&gt;
 &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2Fcanonically-labeled.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2Fcanonically-labeled.png" alt="canonically labeled"&gt;&lt;/a&gt;&lt;br&gt;
 The advantage of canonical labeling is that such a tree can be stored in an array where value of a node is stored at the index equal to its  label in the tree.&lt;br&gt;
 &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2FBinary-heap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2FBinary-heap.png" alt="Binary heap"&gt;&lt;/a&gt;&lt;br&gt;
 This way we will save a lot of memory because we do not need to store  all the pointers. If you have noticed, there is a relationship  between the labels of a node and its children above:&lt;br&gt;
 &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2FBinary-heap_1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2FBinary-heap_1.png" alt="Binary heap_1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2*a&lt;/code&gt; is nothing but left shifting , so it's easy to implement in computer. &lt;code&gt;10&lt;/code&gt; is a binary number represent &lt;code&gt;2&lt;/code&gt; in decimal if i left shift it  it will become &lt;code&gt;100&lt;/code&gt; which is &lt;code&gt;4&lt;/code&gt; in decimal.&lt;/p&gt;

&lt;p&gt;This way, it becomes easy to move in either upward or downward direction of a tree, even when it is stored in an array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2Falmost-complete-binary-tree.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2Falmost-complete-binary-tree.png" alt="almost complete binary tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below trees are &lt;strong&gt;NOT&lt;/strong&gt; almost complete binary trees (the labeling has no meaning, because there are gaps).&lt;br&gt;
 &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2FNOT-almost-complete-binary-tree.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.ritambhara.in%2Fwp-content%2Fuploads%2F2016%2F06%2FNOT-almost-complete-binary-tree.png" alt="NOT almost complete binary tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-- &lt;a href="https://www.ritambhara.in/almost-complete-binary-tree/" rel="noopener noreferrer"&gt;source&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The input of an Algorithm affect the time complexity a lot let's take those cases&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;insert&lt;/th&gt;
&lt;th&gt;search&lt;/th&gt;
&lt;th&gt;find min&lt;/th&gt;
&lt;th&gt;delete&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;unsorted array&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sorted array&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unsorted linked list&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n+1)=O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;min heap&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Max heapify algorithm and complete binary tree
&lt;/h3&gt;

&lt;p&gt;to build A heap in first place we should re-arrange it in a way that the parent always bigger than children.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;max-HEAPIFY(A,i)
{
    l = 2*i;
    r = 2*i+1;
    largest = i;

    if(l &amp;lt;= A.heap-size and A[i] &amp;gt; A[i]) largest = l; // check if it's have a left child 
    if(r &amp;lt;= A.heap-size and A[r] &amp;gt; A[largest]) largest = r; // check if it's have a right child
    if(largest != i)
    {
        exchane A[i] with A[largest]
        max-HEAPIFY(A,largest)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the heart of this Algorithm is if we have sub-trees are max-heap but we need to make the root max-heap also.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fusers.cecs.anu.edu.au%2F~Alistair.Rendell%2FTeaching%2Fapac_comp3600%2Fmodule2%2Fimages%2FHeaps_Heapify.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fusers.cecs.anu.edu.au%2F~Alistair.Rendell%2FTeaching%2Fapac_comp3600%2Fmodule2%2Fimages%2FHeaps_Heapify.png" alt="https://users.cecs.anu.edu.au/~Alistair.Rendell/Teaching/apac_comp3600/module2/images/Heaps_Heapify.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;time complexity is &lt;code&gt;O(log n)&lt;/code&gt; which is the height of the tree , space complexity is the number of recursive calls &lt;code&gt;O(log n)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Build max heap algorithm and analysis
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BUILD-max-heap(A)
{
    A.heap-size = A.length
    for( i = floor(A.length/2) down to 1) 
        max-HEAPIFY(A,i)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;take this array &lt;code&gt;A&lt;/code&gt; as an example &lt;code&gt;9,6,5,0,8,2,1,3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F29%2FhAHJLKq7Y8skxMp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F29%2FhAHJLKq7Y8skxMp.png" alt="image-20200929063938553"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;length of &lt;code&gt;A&lt;/code&gt; is 8 &lt;code&gt;floor(8/2)+1 = 4+1 = 5&lt;/code&gt; (we add 1 because we start from 1 not 0)&lt;/p&gt;

&lt;p&gt;so from index 5 to 1 we need to apply &lt;code&gt;max-HEAPIFY(A,i)&lt;/code&gt; , index 5 is already a leaf so nothing to do go to index 4 &lt;code&gt;0&amp;lt;8&lt;/code&gt; so swap them&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F29%2FD5gVrRZvGCUFwxL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F29%2FD5gVrRZvGCUFwxL.png" alt="image-20200929064304354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;then in index&lt;code&gt;3&lt;/code&gt; we have &lt;code&gt;5 &amp;gt; 2 &amp;gt; 1&lt;/code&gt; so nothing to do , go to index &lt;code&gt;2&lt;/code&gt; we have &lt;code&gt;6&amp;lt;8&lt;/code&gt; so swap&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F29%2FU4macJExoQGN5fK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F29%2FU4macJExoQGN5fK.png" alt="image-20200929064415220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and index 1 is already the biggest so nothing to do.&lt;/p&gt;

&lt;p&gt;what is the total time taking to build this max-HEAP ?&lt;/p&gt;

&lt;p&gt;height of tree is &lt;code&gt;log n&lt;/code&gt; and we are applying it n times so you will think that will take &lt;code&gt;O(n log n)&lt;/code&gt; but it's not the time complexity is not the way to do it.&lt;/p&gt;

&lt;p&gt;the number of nodes at height &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwikimedia.org%2Fapi%2Frest_v1%2Fmedia%2Fmath%2Frender%2Fsvg%2Fb26be3e694314bc90c3215047e4a2010c6ee184a" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwikimedia.org%2Fapi%2Frest_v1%2Fmedia%2Fmath%2Frender%2Fsvg%2Fb26be3e694314bc90c3215047e4a2010c6ee184a" alt="h"&gt;&lt;/a&gt;  is &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwikimedia.org%2Fapi%2Frest_v1%2Fmedia%2Fmath%2Frender%2Fsvg%2F5051ad1ea862f8357779fc9ad08e03be88ec7fbf" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwikimedia.org%2Fapi%2Frest_v1%2Fmedia%2Fmath%2Frender%2Fsvg%2F5051ad1ea862f8357779fc9ad08e03be88ec7fbf" alt="{\displaystyle \leq {\frac {2^{\lfloor \log n\rfloor }}{2^{h}}}\leq {\frac {n}{2^{h}}}}"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Therefore, the cost of heapifying all subtrees is:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwikimedia.org%2Fapi%2Frest_v1%2Fmedia%2Fmath%2Frender%2Fsvg%2F2b1f5dac83f79e16b8794611e4b4a91594c422d8" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwikimedia.org%2Fapi%2Frest_v1%2Fmedia%2Fmath%2Frender%2Fsvg%2F2b1f5dac83f79e16b8794611e4b4a91594c422d8" alt="{\displaystyle {\begin{aligned}\sum _{h=0}^{\lfloor \log n\rfloor }{\frac {n}{2^{h}}}O(h)&amp;amp;=O\left(n\sum _{h=0}^{\lfloor \log n\rfloor }{\frac {h}{2^{h}}}\right)\\&amp;amp;=O\left(n\sum _{h=0}^{\infty }{\frac {h}{2^{h}}}\right)\\&amp;amp;=O(n)\end{aligned}}}"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;where &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F29%2FaEbUMAHt6x2VdNz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F29%2FaEbUMAHt6x2VdNz.png" alt="image-20200929065158664"&gt;&lt;/a&gt; is nothing than 2 (there is a proof for this)&lt;/p&gt;

&lt;p&gt;and for space complexity required will be &lt;code&gt;O(log n)&lt;/code&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Extract max from heap
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Heap-extract-max()
{
    if A.heap-size &amp;lt; 1
        error "heap underflow"
    max = A[1]
    A[1] = A[A.heap-size]
    A.heap-size = A.heap-size - 1
    max-HEAPIFY(A,1)
    return max
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have the Heapify procedure, the Extract-Max procedure isn't hard to define at all. Suppose we have the following heap.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.cs.rit.edu%2F~rpj%2Fcourses%2Fbic2%2Fstudios%2Fstudio1%2Fextract-1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.cs.rit.edu%2F~rpj%2Fcourses%2Fbic2%2Fstudios%2Fstudio1%2Fextract-1.gif" alt="img"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Extracting the node with maximum value is easy enough - by the definition of a heap, it is at the top. So we return it. Fine. Now the question is how to turn what remains,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.cs.rit.edu%2F~rpj%2Fcourses%2Fbic2%2Fstudios%2Fstudio1%2Fextract-2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.cs.rit.edu%2F~rpj%2Fcourses%2Fbic2%2Fstudios%2Fstudio1%2Fextract-2.gif" alt="img"&gt;&lt;/a&gt; ,&lt;/p&gt;

&lt;p&gt;back into a heap. We can almost run the heapify procedure on the whole thing, except that we have &lt;em&gt;two&lt;/em&gt; complete binary trees, not one.&lt;/p&gt;

&lt;p&gt;So we move the node at the bottom right to the top.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.cs.rit.edu%2F~rpj%2Fcourses%2Fbic2%2Fstudios%2Fstudio1%2Fextract-4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.cs.rit.edu%2F~rpj%2Fcourses%2Fbic2%2Fstudios%2Fstudio1%2Fextract-4.gif" alt="img"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can perform the Heapify procedure on the whole tree.&lt;/p&gt;

&lt;p&gt;time taking is &lt;code&gt;O(log n)&lt;/code&gt; and same for space.&lt;/p&gt;

&lt;h3&gt;
  
  
  Heap - increase key
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Heap-Increase-Key(A, i, key)
// Input: A: an array representing a heap, i: an array index, key: a new key greater than A[i]
// Output: A still representing a heap where the key of A[i] was increased to key
// Running Time: O(log n) where n =heap-size[A]
1 if key &amp;lt; A[i]
2   error(“New key must be larger than current key”)
3 A[i] ← key
4 while i &amp;gt; 1 and A[Parent(i)] &amp;lt; A[i]
5   exchange A[i] and A[Parent(i)]
6   i ← Parent(i)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because it's increase a key we don't need from our key to be less than element so we return error, and we loop until we reach the root , and for every node we see it's parent if it's less or bigger than the key we exchange it.&lt;/p&gt;

&lt;p&gt;if we need to decrease a node we need to max-heapify for the tree in that node , which in worst case will take &lt;code&gt;O(log n)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Heap Sort
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HEAP-sort(A)
{
    BUILD-max-HEAP(A)
    for(i = A.length down to 2)
    {
        exchange A[1] with A[i]
        A.heap-size = A.heap-size-1
        max-HEAPIFY(A,1)
    }   
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we are running max-HEAPIFY &lt;code&gt;n&lt;/code&gt; times so time complexity is &lt;code&gt;O(n * log(n))&lt;/code&gt; where &lt;code&gt;log n&lt;/code&gt; is time complexity of max-HEAPIFY&lt;/p&gt;

&lt;h3&gt;
  
  
  Bubble sort
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;BubleSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;swapped&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;swapped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]){&lt;/span&gt;
                &lt;span class="n"&gt;swap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
                &lt;span class="n"&gt;swapped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;swapped&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.studytonight.com%2Fdata-structures%2Fimages%2Fbasic-bubble-sort.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.studytonight.com%2Fdata-structures%2Fimages%2Fbasic-bubble-sort.png" alt="Bubble sort algorithm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the worst case is reversed sorted array which is &lt;code&gt;O(n^2)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and average case is &lt;code&gt;O(k*n)&lt;/code&gt; and best case is &lt;code&gt;O(n)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Bucket sort
&lt;/h3&gt;

&lt;p&gt;bucket sort generally use to solve kind of problem where numbers are float and range from &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;b&lt;/code&gt; with &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are float.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0.35 , 0.12 , 0.43 , 0.15 , 0.04 , 0.50 , 0.132&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F10%2F08%2FagUnzTSv7K2DyfR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F10%2F08%2FagUnzTSv7K2DyfR.png" alt="image-20201008134820976"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;it will take first number &lt;code&gt;.&lt;/code&gt; and put it in it's place if is already an element there it will sort them then printing them in order.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0.04 , 0.12 , 0.132 , 0.15 , 0.35 , 0.43 , 0.50&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;in best case where the numbers will be inserted without need to sort to place new item in cell so every insertion will take &lt;code&gt;O(1)&lt;/code&gt; , so all the insertion will take &lt;code&gt;O(n)&lt;/code&gt; and space complexity will be &lt;code&gt;O(n+k)&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;the worst case &lt;code&gt;O( (n/k) * n ) = O(n^2)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Counting sort
&lt;/h3&gt;

&lt;p&gt;Counting sort have restrictions to apply , the restriction is that the input is in a range.&lt;/p&gt;

&lt;p&gt;let's say our range is from &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;so we will take an array of 5 elements &lt;code&gt;2 2 3 4 1 5 1 5&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;INDEX&lt;/th&gt;
&lt;th&gt;appearance&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;then we print them in order &lt;code&gt;1 1 2 2 3 4 5 5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;time complexity is&lt;code&gt;O(n+k)&lt;/code&gt; and space complexity is &lt;code&gt;O(k)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the disadvantage we can not go outside range and if the range is big , and the elements are few&lt;/p&gt;

&lt;p&gt;&lt;code&gt;1 1000 2 3&lt;/code&gt; so the amount of space we need is &lt;code&gt;1000&lt;/code&gt; for only &lt;code&gt;4&lt;/code&gt; elements.&lt;/p&gt;

&lt;p&gt;### Radix sort&lt;/p&gt;

&lt;p&gt;Do following for each digit i where i varies from least significant digit to the most significant digit. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sort input array using counting sort (or any stable sort) according to the i’th digit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Original, unsorted list:&lt;br&gt;
&lt;code&gt;170, 45, 75, 90, 802, 24, 2, 66&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Sorting by least significant digit (1s place) gives: &lt;br&gt;
[*Notice that we keep 802 before 2, because 802 occurred &lt;br&gt;
before 2 in the original list, and similarly for pairs &lt;br&gt;
170 &amp;amp; 90 and 45 &amp;amp; 75.]&lt;/p&gt;

&lt;p&gt;Sorting by next digit (10s place) gives: &lt;br&gt;
[*Notice that 802 again comes before 2 as 802 comes before &lt;br&gt;
2 in the previous list.]&lt;/p&gt;

&lt;p&gt;&lt;code&gt;802, 2, 24, 45, 66, 170, 75, 90&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Sorting by the most significant digit (100s place) gives:&lt;br&gt;
&lt;code&gt;2, 24, 45, 66, 75, 90, 170, 802&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fcdn-uploads%2Fscene00577.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fcdn-uploads%2Fscene00577.jpg" alt="scene00577"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fcdn-uploads%2Fscene00649.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fcdn-uploads%2Fscene00649.jpg" alt="scene00649"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fcdn-uploads%2Fscene00793.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fcdn-uploads%2Fscene00793.jpg" alt="scene00793"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fcdn-uploads%2Fscene01009.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fcdn-uploads%2Fscene01009.jpg" alt="scene01009"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fcdn-uploads%2Fscene01225.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fcdn-uploads%2Fscene01225.jpg" alt="scene01225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let there be d digits in input integers. Radix Sort takes &lt;code&gt;O(d*(n+b))&lt;/code&gt;  time where &lt;code&gt;b&lt;/code&gt; is the base for representing numbers, for example, for the  decimal system, &lt;code&gt;b&lt;/code&gt; is 10. What is the value of &lt;code&gt;d&lt;/code&gt;? If &lt;code&gt;k&lt;/code&gt; is the maximum  possible value, then &lt;code&gt;d&lt;/code&gt; would be &lt;code&gt;O(logb(k))&lt;/code&gt;. So overall time complexity is &lt;code&gt;O((n+b) * logb(k))&lt;/code&gt;. Which looks more than the time complexity of comparison-based sorting  algorithms for a large &lt;code&gt;k&lt;/code&gt;. Let us first limit &lt;code&gt;k&lt;/code&gt;. Let &lt;code&gt;k &amp;lt;= nc&lt;/code&gt; where &lt;code&gt;c&lt;/code&gt; is a constant. In that case, the complexity becomes &lt;code&gt;O(nLogb(n))&lt;/code&gt;. But it still doesn’t beat comparison-based sorting algorithms. &lt;br&gt;
What if we make the value of &lt;code&gt;b&lt;/code&gt; larger?. What should be the value of b to  make the time complexity linear? If we set &lt;code&gt;b&lt;/code&gt; as &lt;code&gt;n&lt;/code&gt;, we get the time  complexity as &lt;code&gt;O(n)&lt;/code&gt;. In other words, we can sort an array of integers  with a range from &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;nc&lt;/code&gt; if the numbers are represented in base &lt;code&gt;n&lt;/code&gt; (or every digit takes &lt;code&gt;log2(n)&lt;/code&gt; bits). &lt;/p&gt;

&lt;p&gt;-- &lt;a href="https://www.geeksforgeeks.org/radix-sort/" rel="noopener noreferrer"&gt;source&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>datastructures</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>#003 - Clean Code - Functions</title>
      <dc:creator>Omar</dc:creator>
      <pubDate>Sat, 26 Sep 2020 00:35:54 +0000</pubDate>
      <link>https://dev.to/omarkhatib/003-clean-code-functions-2j57</link>
      <guid>https://dev.to/omarkhatib/003-clean-code-functions-2j57</guid>
      <description>&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;p&gt;functions are what we do virtualing all the code we do , functions contain all the code that we do , so it's worth to know how to do them well.&lt;/p&gt;

&lt;p&gt;how big should a fuction be? big ?? smal?? &lt;/p&gt;

&lt;p&gt;A function should do it once , do it well and do it only. But what does one thing mean?&lt;/p&gt;

&lt;h3&gt;
  
  
  The first rule of functions
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v2tzH6Ro--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Fi.ytimg.com%252Fvi%252FFJBtujXM5cQ%252Fmaxresdefault.jpg%26f%3D1%26nofb%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v2tzH6Ro--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Fi.ytimg.com%252Fvi%252FFJBtujXM5cQ%252Fmaxresdefault.jpg%26f%3D1%26nofb%3D1" alt="Elephant Vs. Hippo: Extremely Rare Images Show Attack ..." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The functions should be small , in old ages the size should be small as the screen size , those days technologie is cheaper than what is was before , now we have multiple screens we big sizes and lot of pixels , so this math relation is no longer correct , maybe the range of a clean function should be between 4-7 lines. &lt;/p&gt;

&lt;p&gt;to minimize the function size we should extract all checks if elses to an boolean function .&lt;/p&gt;

&lt;p&gt;Remember the scope rule we talk about , if the variable is far from the scope the name should be long and if it is close for be small , In the class we have more than 1 function so the functions should have long descriptive names.&lt;/p&gt;

&lt;p&gt;if we have a function that it's big and operate on set of variables , well this is the behavoir of a class. When we convert our function into a class we take the set of variables that the entier class work on into a constructor.&lt;/p&gt;

&lt;h3&gt;
  
  
  are you out of your mind?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qzJajZzU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttp%253A%252F%252Fimg.gawkerassets.com%252Fimg%252F18ltqopypurybgif%252Fku-xlarge.gif%26f%3D1%26nofb%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qzJajZzU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttp%253A%252F%252Fimg.gawkerassets.com%252Fimg%252F18ltqopypurybgif%252Fku-xlarge.gif%26f%3D1%26nofb%3D1" alt="23 Amazing Labyrinths To Get Lost In | Gizmodo Australia" width="640" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;you will say wait my code is now full of small functions I feel lost inside a forest , well that's where a good name came into play a well named classes and functions will never make you lost if you lost it mean your names are bad.&lt;/p&gt;

&lt;h3&gt;
  
  
  the goegraphy metaphor
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ir2VX8KU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Ftse3.mm.bing.net%252Fth%253Fid%253DOIP.Nk3v2175ugYXfnK0EbqhcwHaFj%2526pid%253DApi%26f%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ir2VX8KU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Ftse3.mm.bing.net%252Fth%253Fid%253DOIP.Nk3v2175ugYXfnK0EbqhcwHaFj%2526pid%253DApi%26f%3D1" alt="img" width="474" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why do we feel so uncomfortable about writing small functions? well it's about our human nature , yes really! We evolve from explorers who love landscapes we like landscapes. so Big functions look more familiar to us.&lt;/p&gt;

&lt;p&gt;We put hidden marks to recognize the path like special shapes or signs , in coding those marks are the comments the try and catches .  But if someone new join the project he will feel lost. But if we put navigations he will not feel lost. Just as every day we get in the car the road have signs to where we are and where were going. So good logical seperation and naming will help newbies to explore our land (code) .&lt;/p&gt;

&lt;h3&gt;
  
  
  The bedroom metaphor
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8ekiWdXN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Fdeliat.files.wordpress.com%252F2009%252F12%252Fmessy_room_one___busy_dad_by_anakingusan.jpg%26f%3D1%26nofb%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8ekiWdXN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Fdeliat.files.wordpress.com%252F2009%252F12%252Fmessy_room_one___busy_dad_by_anakingusan.jpg%26f%3D1%26nofb%3D1" alt="Messy Room | Delia Tee" width="600" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We all have our messy room that we don't like any one to touch it , because we know where our stuff are even if it's messy I know if I need my pants that I wear yesterday it's on the floor. If someone organize it for us , we will go back and rearrange it the way we like.&lt;/p&gt;

&lt;p&gt;But not all mens love to be single , so some of them will make the biggest mistake of their life , they will link themself to  an &lt;strong&gt;annoying&lt;/strong&gt; wife. Now we need to organize all the rooms , all the jars with names on them.&lt;/p&gt;

&lt;p&gt;So to go back to our real life , the work . We are going to work with a team so the best way is to put the code in nicely named place , where it can be found later and understoud.&lt;/p&gt;

&lt;h3&gt;
  
  
  One thing
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LrNOsnAg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Ftse4.mm.bing.net%252Fth%253Fid%253DOIP.TSfC2SrmZcn6Sb5TOTzbfQAAAA%2526pid%253DApi%26f%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LrNOsnAg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Ftse4.mm.bing.net%252Fth%253Fid%253DOIP.TSfC2SrmZcn6Sb5TOTzbfQAAAA%2526pid%253DApi%26f%3D1" alt="img" width="250" height="398"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hear word say one thing , didn't you?  one thing could mean anything. And one thing here should be not for us who write the code it's for those who will read the code after us. &lt;/p&gt;

&lt;p&gt;Let's say we need from our function to scrable an website , but this website need to login and our crawler need an driver browser and so on. It's doing onething which is scrable an website but this one thing contain many sub things with it. So we need a thing to tell us if our function is doing only one thing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extract till you drop
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rRDW5Wd0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Ftse1.mm.bing.net%252Fth%253Fid%253DOIP.Q2wu61UJmS63aXdwrUjwfgHaHa%2526pid%253DApi%26f%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rRDW5Wd0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Ftse1.mm.bing.net%252Fth%253Fid%253DOIP.Q2wu61UJmS63aXdwrUjwfgHaHa%2526pid%253DApi%26f%3D1" alt="img" width="474" height="474"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;how to make sure that our function will do one thing?&lt;/p&gt;

&lt;p&gt;We extract things from function until we have nothing to exctract any more , and sure we need to exctract things that do one thing Again and again.&lt;br&gt;
Well let's be realistic forget everything we talk about before and look at this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z1mYOpg0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-preview.redd.it/hJGBRzWE_h-GGr4xaiYeY1_x0Kf3s5UgKFuLx1BPAF0.png%3Fwidth%3D640%26height%3D508%26crop%3Dsmart%26auto%3Dwebp%26s%3D1e0bd9ccff9924c307e7f8d17c4848888789168e" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z1mYOpg0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-preview.redd.it/hJGBRzWE_h-GGr4xaiYeY1_x0Kf3s5UgKFuLx1BPAF0.png%3Fwidth%3D640%26height%3D508%26crop%3Dsmart%26auto%3Dwebp%26s%3D1e0bd9ccff9924c307e7f8d17c4848888789168e" alt="Post image" width="640" height="508"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>codequality</category>
      <category>productivity</category>
    </item>
    <item>
      <title>#010 DS&amp;A - Time and Space Analysis</title>
      <dc:creator>Omar</dc:creator>
      <pubDate>Sat, 19 Sep 2020 18:29:10 +0000</pubDate>
      <link>https://dev.to/omarkhatib/010-ds-a-time-and-space-analysis-3gpa</link>
      <guid>https://dev.to/omarkhatib/010-ds-a-time-and-space-analysis-3gpa</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Nothing to say more than hello 👋,&lt;br&gt;
In Case your wondering who is this man in the banner it's the Khwarizmi the inventor of algorithms concept.&lt;br&gt;
Make sure to follow because I will start more advanced series in future.&lt;/p&gt;
&lt;h1&gt;
  
  
  Algorithms
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Time and Space Analysis
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Introduction to asymptotic
&lt;/h3&gt;

&lt;p&gt;In order to solve any problem in computer science we usually write a program , before writing program it's better to write it in formal description which is called Algorithm .&lt;/p&gt;

&lt;p&gt;let's say we have a problem &lt;code&gt;P&lt;/code&gt; we need to write a program , let's say we write it in c , to write program we need first to write an Algorithm , let's say &lt;code&gt;P&lt;/code&gt; have many solutions &lt;code&gt;s1,s2,s3,s4....&lt;/code&gt; the thing that will differ solution from another is &lt;code&gt;time&lt;/code&gt; and &lt;code&gt;memory&lt;/code&gt; , the science of it is called &lt;code&gt;Design  and analysis of algorithm&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;some of the notations used in Algorithms are these:&lt;/p&gt;
&lt;h3&gt;
  
  
  Asymptotic notation
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Big(oh) represented by O&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qJb_piVB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/15/93msi27PfBTuZAh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qJb_piVB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/15/93msi27PfBTuZAh.png" alt="image-20200914233224043" width="343" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;if time Increasing as input increasing so the the worst  case or upper bound is &lt;code&gt;C*g(n)&lt;/code&gt; and satisfy those conditions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(n) &amp;lt;= C*g(n) ; n &amp;gt;= n0 ; c&amp;gt;0 , n0 &amp;gt;= 1
f(n) = O(g(n))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's take this example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(n) = 3n+2 and g(n) = n
f(n) = O(g(n))
f(n) &amp;lt;= C * g(n) , c &amp;gt; 0 , n0 &amp;gt;= 1
3n+2 &amp;lt;= c*n
take c = 4
3n+2 &amp;lt;= 4n =&amp;gt; n &amp;gt;= 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can pick &lt;code&gt;g(n) = n^2 or n^3 ... n^n&lt;/code&gt;because &lt;code&gt;f(n)&lt;/code&gt; can be written with respect to &lt;code&gt;g(n)&lt;/code&gt; but it is preferred to take the smallest one which is &lt;code&gt;n&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Big omega represented by Ω&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T1SzJOAB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/15/qNC62udonMtTJk4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T1SzJOAB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/15/qNC62udonMtTJk4.png" alt="image-20200915171717083" width="462" height="309"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(n) &amp;gt;= C*g(n) ; n &amp;gt;= n0 ; c&amp;gt;0 , n0 &amp;gt;= 1
f(n) = O(g(n))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(n) = 3n+2 and g(n) = n
f(n) = O(g(n))
f(n) &amp;gt;= C * g(n)
3n+2 &amp;gt;= c*n
take c = 1 and n0 &amp;gt;= 1
3n+2 = Ω(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;example 2&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;g(n) = 3n+2 g(n)=n^2
3n+2 ?= Ω(n^2) 
3n+2 &amp;gt;= c*n^2 , n0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can not find a &lt;code&gt;C&lt;/code&gt; that satisfy this solution , so we need to pick things lower than &lt;code&gt;n&lt;/code&gt; like &lt;code&gt;log(n)&lt;/code&gt; or &lt;code&gt;log(log(n))&lt;/code&gt;  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Big theta represented by Θ&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q39QYlEX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/15/FzKDeXuHIAx89q7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q39QYlEX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/15/FzKDeXuHIAx89q7.png" alt="image-20200915172753442" width="487" height="296"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(n) = Θ(g(n))
c1*g(n) &amp;lt;= f(n) &amp;lt;= c2*gn(n) ; c1,c2 &amp;gt; 0 , n &amp;gt;= n0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(n) = 3n+2 , g(n) = n
f(n) &amp;lt;= C2*g(n)
take c2 = 4
3n+2 &amp;lt;= 4n

f(n) &amp;gt;= c1*g(n)
take c1 = 1
3n+2 &amp;gt;= n , n0 &amp;gt;= 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;O&lt;/code&gt; this mean their is no bigger time than this , and it's called worst case.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Ω&lt;/code&gt; this mean their is no better time than this , and it's called best case.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Θ&lt;/code&gt; this mean it's the average case , and it's called the average case.&lt;/p&gt;

&lt;p&gt;we usually don't care about best case , we care about worst case. If the worst and best cases are the same we generally go for average case.&lt;/p&gt;

&lt;p&gt;example: take this array with &lt;code&gt;n&lt;/code&gt; elements&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;3&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;5&lt;/th&gt;
&lt;th&gt;6&lt;/th&gt;
&lt;th&gt;7&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;let's say we need to find element &lt;code&gt;x&lt;/code&gt;  , best case is &lt;code&gt;Ω(1)&lt;/code&gt; , worst case is &lt;code&gt;O(n)&lt;/code&gt;, average case is &lt;code&gt;Θ(n/2)= Θ(n)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Time Complexity Analysis
&lt;/h3&gt;

&lt;p&gt;the &lt;code&gt;f(n)&lt;/code&gt; is not the real time is just approximation  time that program take to finish.&lt;/p&gt;

&lt;p&gt;there is 2 types of Algorithms :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;iterative:
A()
{
    for i=1 to n
        max(a,b)
}

recursive:

A(n)
{
    if() A(n/2)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;any iterative can be written as recursive and vise versa.&lt;/p&gt;

&lt;p&gt;If the Algorithm doesn't have loops or recursion time complexity is constant &lt;code&gt;O(1)&lt;/code&gt; , if the time complexity is &lt;code&gt;O(n^2+n)&lt;/code&gt; we take the biggest degree so we take it  &lt;code&gt;O(n^2)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;examples&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A()
{
    int i;
    for(i = 1 to n) pf("text");
}

time complexity : O(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A()
{
    int i;
    for(i = 1 to n) 
        for(j = 1 to n)
            pf("omar");
}

time complexity : O(n^2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A()
{
    int i;
    while (s &amp;lt;= n)
    {
        i++;
        s = s+i;
        pf("omar");
    }
}

time complexity : O(sqrt(n))
/*
Analysing :
s : 1 3 6 10 15 21 ...
i : 1 2 3  4  5  6 ...
s will stop on n
let's say k is the final i
and s is nothing but new i + all old i (6 = 3+2+1) so it will stop when it reach
k(k+1)/2 &amp;gt; n
(k^2+k)/2 &amp;gt; n
k = O(sqrt(n))
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A()
{
    int i;
    for(i=1;i^2&amp;lt;=n;i++) pf("omar");
}

time complexity : O(sqrt(n))
// Here all the cases are the same
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A()
{
    int i,j,k,n;
    for(i=1 ; i&amp;lt;n ; i++)
    {
        for(j=1;j&amp;lt;=i;j++)
        {
            for(k=1 ; k &amp;lt;= 100 ; k++)
            {
                pf("omar");
            }
        }
    }
}

time complexity : O(n^2)

/*
Analysing :
i = 1 , j = 1 time  , k = 100   times
i = 2 , j = 2 times , k = 200   times
i = 3 , j = 3 times , k = 300   times
...
i = n , j = n times , k = j*100 times

1*100+2*100+3*100+...+n*100 
 = 100 (1+2+3+...+n)
 = 100 (n(n+1)/2) = O(n^2)
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int i,j,k,n;
for(i = 1 ; i &amp;lt;= n ;i++)
{
    for(j=1 ; j &amp;lt;= i^2 ; j++)
    {
        for(k=1 ; k &amp;lt;= n/2 ; k++)
        {
            pf("omar");
        }
    }
}

time complexity : O(n^4)

/*
Analysing :
i = 1 , j = 1 time  , k = n/2 * 1 
i = 2 , j = 4 times , k = n/2 * 4   
i = 3 , j = 9 times , k = n/2 * 9   
...
i = n , j = n^2 times , k = n/2 * n^2 times

n/2 * 1 + n/2 *4 + n/2 *  9 + ... + n/2 * n^2
 = n/2 * (n(n+1)(2n+1))/6
 = O(n^4)
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A()
{
    for(i = 1 ; i &amp;lt; n ; i = i*2)
        pf("omar");
}

time complexity : O(log(n))

/*
Analysing :
i :  1  , 2   , 4   ... n
    2^0 , 2^1 , 2^2 ... 2^k
2^k = n =&amp;gt; k = log(n) = O(log(n))
since i is multiplied by 2 every step so log here is base 2
if i is multiplied by k we say log of base k
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A()
{
    int i,j,k;
    for(i=n/2 ; i &amp;lt;= n ; i++)
        for(j=1 ; j &amp;lt;= n2 ; j++)
            for(k=1 ; k &amp;lt;= n ; k=k*2)
                pf("omar");
}

time complexity : O(n^2 * log(n))

/*
Analysing :
n/2 * n/2 * log(n) = O(n^2 * log(n))
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A()
{
    int i,j,k;
    for(i=n/2 ; i &amp;lt;= n ; i++) // n/2
        for(j=1 ; j &amp;lt;= n ; i = 2*k) // log n
            for(k=1 ; k &amp;lt;= n ; k = k*2) // log n
                pf("omar");
}

time complexity : O(n*(log(n))^2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A()
{
    // assume n &amp;gt;= 2
    while(n&amp;gt;1)
    {
        n = n/2;
    }
}

time complexity : O(log(n))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A()
{
    for(i = 1 ; i &amp;lt;= n ; i++) // n
        for(j = 1 ; j &amp;lt;= n ; i = j+i) // 
            pf("omar")
}

time complexity : O(n*log(n))

/*
Analysing :

i = 1 , j = 1 to n ; n   times
i = 2 , j = 1 to n ; n/2 times
i = 3 , j = 1 to n ; n/3 times
...
i = n , j = 1 to n ; n/n times

n(1+ 1/2 + 1/3 + ... + 1/n )
 = n (log n) = O(n * log(n))
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A()
{
    int n = (2^2)^k;
    for(i=1;i&amp;lt;=n;i++) // n
    {
        j = 2
        while(j &amp;lt;= n)
        {
            j = j^2;
            pf("omar");
        }
    }
}

time complexity : O(log(log(n)))

/*
Analysing :

k = 1 ; n = 4       ; j = 2,4               ; n * 2 times
k = 2 ; n = 16      ; j = 2,4,16            ; n * 3 times
k = 3 ; n = (2^2)^3 ; j = 2^1,2^2,2^4,2^8   ; n * 4 times

n = (2^2)^k =&amp;gt; log n = 2^k =&amp;gt; log(log(n))=k
n*(k+1) = n(log(log(n)) + 1) = O(log(log(n)))
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Time analysis of recursive
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A(n)
{
    if(...)
    return A(n/2)+A(n/2);
}

T(n) = c+2*T(n/2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A(n)
{
    if(n&amp;gt;1) return A(n-1);
}

T(n) = 1 + T(n-1) 
= 1 + 1 + T(n-2) 
= 2+1+T(n-3) 
= k+T(n-k) // k = n-1
= (n-1)+T(1) = n-1+1 = n
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;back substitution&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n-1) = 1+T(n-2) -&amp;gt; 2
T(n-2) = 1+T(n-3) -&amp;gt; 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = n + T(n)
T(n-1) = (n-1)+T(n-2)
T(n-2) = (n-2)+T(n-3)
-----------------------
T(n) = n + T(n-1)
     = n + (n-1) + T(n-2)
     = n + (n-1) + (n-2) + T(n-3)
     = n + (n-1) + (n-2)+...+(n-k)+T(n-(k+1))
with n-(k+1)=1 =&amp;gt; n-k-1=1 =&amp;gt; k=n-2
= n+(n-1)+(n-2)+...+(n-(n-2))+T(n-(n-2+1))
= n+(n-1)+(n-2)+...+1
= n(n-1)/2
= O(n^2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;recursive tree method&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 2*T(n/2) + C ; n&amp;gt;1
     = C ; n = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--com0h1EX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/17/Uu1q7cTxwRBDoVM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--com0h1EX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/17/Uu1q7cTxwRBDoVM.png" alt="image-20200917021118588" width="595" height="494"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(1) = T(n/n)
c+2c+4c+...+nc
c(1+2+4+...+n)
c(1+2+4+...+2^k)
c(1 (2^(k+1) - 1) / (2-1) )
c(2^k+1 - 1)
c(2n-1)
O(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 2*T(n/2)+n ; n &amp;gt; 1
     = 1 ; n = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gILA2NDP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/17/KJNy5dLvGMYsO8t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gILA2NDP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/17/KJNy5dLvGMYsO8t.png" alt="image-20200917025850624" width="579" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  comparing various functions
&lt;/h3&gt;

&lt;p&gt;let's say we have two functions &lt;code&gt;n^2&lt;/code&gt; and &lt;code&gt;n^3&lt;/code&gt;  they have &lt;code&gt;n^2&lt;/code&gt; as common so I rewrite it as &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;f(n)&lt;/code&gt; is given and &lt;code&gt;g(n)&lt;/code&gt; is given we take biggest degree and compare them. If they are constants like &lt;code&gt;2&lt;/code&gt; and &lt;code&gt;4&lt;/code&gt; we consider them the same.&lt;/p&gt;

&lt;p&gt;examples&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2^n                         n^2
n log(2)                    2 log(n)
n                           2*log(n)
consider n = 2^100
2^100                       2*log(2^100)
2^100                       200
2^100 &amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;   200
so 2^n growing very large
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3^n                         2^n
n*log(3)                    n*log(2)
cancel n and compare it
log(3)                      log(2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;n^2                         n*log(n)
concel common terms
n*n                         n*log(n)
n           &amp;gt;               log(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;n                           log(n)^100
log(n)                      100*log(log(n))
take n = 2^128
128                         100*log(128)
128                         700
let's take n = 1024
1024                        100*log(1024)
1024                        1000
so n growing larger
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;n^log(n)                    n*log(n)
log(n)*log(n)               log(n)+log(log(n))
for n = 10^1024
1024*1024                   1034
for n = (2^2)^20        
2^20*2^20                   2^20+20
so n^log(n) is larger
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sqrt(log(n))                log(log(n))
1/2 * log(log(n))           log(log(log(n)))
take n = (2^2)^10
5                           3.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;n^(sqrt(n))                 n^log(n)
sqrt(n)*log(n)              log(n)*log(n)
sqrt(n)                     log(n)
1/2 * log(n)                log(log(n))
n = 2^128
64                          7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(n) =  {
            n^3         0&amp;lt;n&amp;lt;10000
            n^2         n&amp;gt;=10000
        }
g(n) =  {
            n           0 &amp;lt; n &amp;lt; 100
            n^3         n &amp;gt; 100
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;0-99&lt;/th&gt;
&lt;th&gt;100-9999&lt;/th&gt;
&lt;th&gt;10,000 ....&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;f(n)&lt;/td&gt;
&lt;td&gt;n^3&lt;/td&gt;
&lt;td&gt;n^3&lt;/td&gt;
&lt;td&gt;n^2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;g(n)&lt;/td&gt;
&lt;td&gt;n&lt;/td&gt;
&lt;td&gt;n^3&lt;/td&gt;
&lt;td&gt;n^3&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;we take care about the function in infinity so &lt;code&gt;g(n)&lt;/code&gt; is bigger in infinity&lt;/p&gt;

&lt;h3&gt;
  
  
  Masters theorem
&lt;/h3&gt;

&lt;p&gt;first there is a different between &lt;code&gt;log^2(n)&lt;/code&gt; and &lt;code&gt;(log(n))^2&lt;/code&gt; , because &lt;code&gt;(log(n))^2 = log(n) * log(n)&lt;/code&gt; and &lt;code&gt;log^2(n) = log(log(n))&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;masters theorem used to solve reclusive problems&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O2QkOcls--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/17/vbxPT2sZVXzU3nM.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O2QkOcls--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/17/vbxPT2sZVXzU3nM.jpg" alt="Lightbox" width="600" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;examples&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 3T(n/2) + n^2
a = 3 , b = 2 , k = 2 p=0
a &amp;lt; b^k
3 &amp;lt; 4
so it's the case 3)a so T(n) = O(n^2 * log^0(n))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 4*T(n/2) + n^2
a=4 , b=2 , k=2 , p=0
4=2^2
so it's case 2)a T(n) = O(n^log(4) * log(n)) = O(n^2*log(n))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = T(n/2)+n^2
a=1 , b=2 , k=2 , p=0
1 &amp;lt; 2^2
it's case 3)a T(n) = O(n^2 * log^0(n)) = O(n^2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 2^n*T(n/2)+n^n master theoreme is not applied
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 16*T(n/4)+n
a = 16 b=4 k=1 p=0
16&amp;gt;4
so it's 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 2*T(n/2)+n*log(n)
a=2 b=2 k=1 p=1
2=2 , p&amp;gt;-1 so it's 2)a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if it doesn't directly look like theorem we need to refactoring it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 2*T(n/2)+n/log(n)
     = 2T(n/2)+n*log^-1(n)
     a=2 , b =2 , k=1 p=-1
     s = 2^1 so it's case 2)b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 2*T(n/4)+n^0.51
a=2 b=4 k=051 p=0
2 &amp;lt; 4^0.51
case 3)a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 05*T(n/2)+1/n
a=0.5 not valid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 6*T(n/3)+n^2*log(n)
a=6 b=3 k=2 p=1
6 &amp;lt; 3^2
so it's 3)a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 64 T(n/8) - n^2 * log(n)
can not apply master theorem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 7*T(n/3)+n^2
a=7 b=3 k=2 p=0
7 &amp;lt; 3^2
case 3)a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 4*T(n/2)+log(n)
a=4 b=2 k=0 p=1
4 &amp;gt; 2^0
case 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = sqrt(2)*T(n/2)+log(n)
a=sqrt(2) b=2 k=0 p=1
sqrt(2) &amp;gt; 2^0
case 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 2*T(n/2)+sqrt(n)
a=2 b=2 k=1/2 p=0
2&amp;gt;2^1/2
case 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 3*T(n/2)+n
a=3 b=2 k=1 p=0
3 &amp;gt; 2^1
case 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 3*T(n/3)+sqrt(n)
a=3 b=3 k=1/2 p=0
3&amp;gt;3^1/2
case 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 4*T(n/2)+C*n
a=4 b=2 k=1 p=0
4 &amp;gt; 2^1
case 3)b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n)=3*T(n/4)+(n*log(n))
a=3 b=4 k=1 p=1
3 &amp;lt; 4
case 3)a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Analysis Space Complexity
&lt;/h3&gt;

&lt;p&gt;same as time complexity we have space complexity for Iterative programs and recursive programs. Some times we sacrifice time for space.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Algo(A,1,n)
{
    int i;
    for(i=1 to n) A[i] = 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this space complexity is constant &lt;code&gt;O(1)&lt;/code&gt; because we don't take the initial input into count. So we calculate extra spaces such as &lt;code&gt;i&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Algo(A,1,n)
{
    int i;
    create B[n];
    for(i=1 to n) B[i] = A[i];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the amount of space required is &lt;code&gt;O(n)&lt;/code&gt; because we declare &lt;code&gt;B[n]&lt;/code&gt; that contain &lt;code&gt;n&lt;/code&gt; element. Same as Time complexity we didn't take in count the constants in other word we take higher degree.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Algo(A,1,n)
{
    create B[n,n];
    int i,j;
    for(i=1 to n)
        for(j=1 to n) B[i,j]=A[i]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;space complexity is &lt;code&gt;O(n^2)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A(n)
{
    if(n&amp;gt;=1)
    {
        A(n-1);
        pf(n);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because the program is small we are going to use the tree method , take &lt;code&gt;n=3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ggdq9k9m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/20/ZSGkWKiABa1whLb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ggdq9k9m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/20/ZSGkWKiABa1whLb.png" alt="image-20200919192742044" width="441" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---szfS7yJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/20/pLAGNsbQkIvH2Y1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---szfS7yJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/20/pLAGNsbQkIvH2Y1.png" alt="image-20200919192808057" width="118" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the output is &lt;code&gt;1 2 3&lt;/code&gt; because every time I end call I print it&lt;/p&gt;

&lt;p&gt;the space complexity is the number of stacks which is &lt;code&gt;O(kn)&lt;/code&gt; where &lt;code&gt;k&lt;/code&gt; is constant so we write it as &lt;code&gt;O(n)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;time complexity is &lt;code&gt;T(n) = T(n-1)+1&lt;/code&gt; it's not form where we can apply master theorem so we gonna use back substitution&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n)  =T(n-1)+1
T(n-1)=T(n-2)+1
T(n-2)=T(n-3)+1
T(n) = T(T(n-2)+1)+1
     = T(n-2) +2
     = (T(n-3)+1) +2
     = T(n-3)+3
     = T(n-k)+k
     = T(n-n)+n
     = T(0)+n
     = 1+n
     = O(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so time and space complexity is &lt;code&gt;O(n)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A(n)
{
    if(n&amp;gt;=1)
    {
        A(n-1);
        pf(n);
        A(n-1);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;number of recursive calls are&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A(3) = 15 = 2^(3+1) - 1
A(2) =  7 = 2^(2+1) - 1
A(1) =  3 = 2^(1+1) - 1
A(n) = (2^n) - 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this is not the space complexity because the stack will only need 4 cells &lt;code&gt;A(0),A(1),A(2),A(3)&lt;/code&gt;  in the stack in order to compute it where the stack will start to empty it self every time it reach &lt;code&gt;A(0)&lt;/code&gt;  , so it's &lt;code&gt;(n+1)*k&lt;/code&gt; where &lt;code&gt;k&lt;/code&gt; is the size occupied by one cell in stack so space complexity is nothing more than &lt;code&gt;O(nk) = O(n)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To optimize it We can use Dynamic programming which is to store the already computed values for not compute them again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A(n) -&amp;gt; T(n)
{
    if(n&amp;gt;=1)
    {
        A(n-1); -&amp;gt; T(n-1)
        pf(n); -&amp;gt; 1
        A(n-1); -&amp;gt; T(n-1)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n)    = 2*T(n-1)+1 ; n &amp;gt; 0
T(n-1)  = 2*T(n-2)+1
T(n-2)  = 2*T(n-3)+1

T(n)    = 2(2T(n-2)+1)
         = 4T(n-2)+2+1
         = 4(2T(n-3)+1)+2+1
         = 8T(n-3)+7
         = 2^k * T(n-k) + 2^(k-1)+...+2^2+2+1
         = 2^n * T(0)+2^n-1 + 2^n-2 + ... + 2^2 + 2 + 1
         = 2^n + 2^n-1 + 2^n-2 + ... + 2^2 + 2 + 1
         = O(2^n+1) = O(2^n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the time complexity is very big &lt;code&gt;O(2^n)&lt;/code&gt; we can lower it with Dynamic Programming as we said.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>datastructres</category>
    </item>
    <item>
      <title>#009 DS&amp;A - Hashing</title>
      <dc:creator>Omar</dc:creator>
      <pubDate>Sun, 13 Sep 2020 20:17:20 +0000</pubDate>
      <link>https://dev.to/omarkhatib/009-ds-a-hashing-325k</link>
      <guid>https://dev.to/omarkhatib/009-ds-a-hashing-325k</guid>
      <description>&lt;h2&gt;
  
  
  Hashing
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Direct address Table
&lt;/h3&gt;

&lt;p&gt;we have many data structures but they are all slow at searching.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;type&lt;/th&gt;
&lt;th&gt;time complexity&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;unsorted array&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sorted array&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;linked list&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Binary Tree&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Binary Search Tree&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Balanced Binary Search Tree&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Priority (min and max)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;to minimize the searching time we use hashing which is &lt;code&gt;O(1)&lt;/code&gt; , also some people use &lt;code&gt;Direct address Table&lt;/code&gt; is just same as an array.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{1,2,3,4,5,...,100}&lt;/code&gt; if we need to put insert an element we put it with a key and store it let's say we need to store the number of occurrence for character &lt;code&gt;C&lt;/code&gt; the number of ASCII let's assume it's 128&lt;/p&gt;

&lt;p&gt;&lt;code&gt;arr[99]&lt;/code&gt; I will store in this array the number of occurrence for &lt;code&gt;c&lt;/code&gt;  where &lt;code&gt;99&lt;/code&gt; is the key of &lt;code&gt;c&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;, and now if I need to access the number of occurrence letter &lt;code&gt;c&lt;/code&gt; I can access it directly using &lt;code&gt;arr[99]&lt;/code&gt; which is &lt;code&gt;O(1)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction to hashing
&lt;/h3&gt;

&lt;p&gt;the problem with this direct access is that array should have big as key sizes , to solve this problem we use hashing.&lt;/p&gt;

&lt;p&gt;let's say we have &lt;code&gt;m&lt;/code&gt; keys and &lt;code&gt;n&lt;/code&gt; elements. in order to map a &lt;code&gt;key&lt;/code&gt; to &lt;code&gt;element&lt;/code&gt; we need an &lt;code&gt;hash function&lt;/code&gt; which will take time of &lt;code&gt;n%m&lt;/code&gt; which is constant &lt;code&gt;O(1)&lt;/code&gt; . When we need to hash a new element to element and it have same location of an previous element it's called collision  example&lt;code&gt;141%10=1&lt;/code&gt; and &lt;code&gt;21%10=1&lt;/code&gt; so this is collision because they have same key . The challenge with hash map is the collision.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  i) better hash functions
 ii) chaining (using linked list)
iii) open addressing , implemented using verious ways
  a) linear probing
  b) quadratic probing
  c) Double hashing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  chaining
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d_9FKCZm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/13/GXWNpy4dDr1vbxO.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d_9FKCZm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/13/GXWNpy4dDr1vbxO.png" alt="image-20200912232529251" width="493" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;after computing the hash if there is a collision , the previous value will link to the new value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;worst case search - O(n)
worst case deletion - O(n)

average search O(1+ alpha)
average deletion O(1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;code&gt;n&lt;/code&gt; is the number of elements represented in the linked list , and &lt;code&gt;m&lt;/code&gt; is the number of cells in linked list.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;alpha = n/m&lt;/code&gt; where alpha is the load factor.&lt;/p&gt;

&lt;p&gt;An advantage of chained hash table over open addressing scheme is deletion is easier.&lt;/p&gt;

&lt;h3&gt;
  
  
  Open addressing
&lt;/h3&gt;

&lt;p&gt;open addressing AKA closed hashing&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--THWhHaDf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/13/No81hvUKfi729Oy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--THWhHaDf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/13/No81hvUKfi729Oy.png" alt="image-20200913161525759" width="535" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;let's say we have array that can hold &lt;code&gt;m&lt;/code&gt; elements it mean all the elements I need should be inside array not outside like chaining and &lt;code&gt;U&lt;/code&gt; keys , where &lt;code&gt;U&lt;/code&gt; is very large and &lt;code&gt;m&lt;/code&gt; is limited so definitely their is a collision.&lt;/p&gt;

&lt;p&gt;we have the load factor &lt;code&gt;alpha&lt;/code&gt; which is always will be between &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;We resolve the collision by applying the hash function with a small modification , this is called probing second time. Probing is nothing but finding location in hash table where we need place an element. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--449veY2k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/13/WjyrxseFfankPdN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--449veY2k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/13/WjyrxseFfankPdN.png" alt="image-20200913162613198" width="589" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we apply the hash function if we find that the element is already picked we make a combination between &lt;code&gt;u&lt;/code&gt; and finite set &lt;code&gt;[0,m-1]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the worst time here to search an element is &lt;code&gt;O(m)&lt;/code&gt; because we need to visit all the table to find that element. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HeD0cwJr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/14/HRXyt4NcZGgKhlY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HeD0cwJr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/14/HRXyt4NcZGgKhlY.png" alt="image-20200913163716384" width="124" height="358"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;assume the &lt;code&gt;X&lt;/code&gt; is mean that slot is &lt;strong&gt;not&lt;/strong&gt; empty and &lt;code&gt;N&lt;/code&gt; is NULL (we can put element here).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;h(k)        = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;1&lt;/code&gt; is NULL so I can put &lt;code&gt;k&lt;/code&gt; in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;h(k1)       = 1
h(k1 , 1)   = 5
h(k1 , 2)   = 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;h(k1)&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt; but &lt;code&gt;1&lt;/code&gt; is not free ( their is collision ) so we run hash function again we get &lt;code&gt;5&lt;/code&gt; which also is not free , so we run it again with &lt;code&gt;2&lt;/code&gt; we get &lt;code&gt;6&lt;/code&gt; which is NULL.&lt;/p&gt;

&lt;p&gt;same for searching assume we have the element we need &lt;code&gt;k1&lt;/code&gt; is at slot &lt;code&gt;0&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;h(k,0) = 2
h(k,1) = 6
h(k,2) = 3
...
h(k,7) = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so I take &lt;code&gt;O(m)&lt;/code&gt; to find it in worst case&lt;/p&gt;

&lt;p&gt;assume the &lt;code&gt;k1&lt;/code&gt; is at slot &lt;code&gt;5&lt;/code&gt; instead of &lt;code&gt;7&lt;/code&gt; so if I reach &lt;code&gt;h(k,5)&lt;/code&gt; and find it &lt;code&gt;NULL&lt;/code&gt; I stop because I know that this element is not presented , because if it's not &lt;code&gt;NULL&lt;/code&gt; the hash function will occupy it for &lt;code&gt;k1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is the case if no elements being deleting . If it's deleted I will continue searching (probing). So deletion will cause a problem so I will assign an element that mean oh there is an element let's say &lt;code&gt;D&lt;/code&gt; here but it is deleted , and in insertion if I find &lt;code&gt;D&lt;/code&gt; I will treat it as &lt;code&gt;NULL&lt;/code&gt; so I place replace it with my element.&lt;/p&gt;

&lt;p&gt;In worst case if All elements are deleted and I need an element after them , so chaining is the best in this case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Linear probing
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t5ty-CIt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/13/qDxPlFieEzkQgry.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t5ty-CIt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/13/qDxPlFieEzkQgry.png" alt="image-20200913170534729" width="258" height="393"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;h : U --&amp;gt; {0 ... m-1}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if I run &lt;code&gt;h(k) = a&lt;/code&gt; so index is &lt;code&gt;a&lt;/code&gt; , if &lt;code&gt;a&lt;/code&gt; is not empty will I will take another hash function h prime&lt;code&gt;h'(k,i)=(h(k)+i) mod m&lt;/code&gt; which is &lt;code&gt;h'(k,1) = ( h(k)+1) mod n = (a+1) mod m = a+1&lt;/code&gt; and &lt;code&gt;h'(k,2) = ( h(k)+2) mod n = (a+2) mod m = a+2&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;it's called linear probing because if the element at &lt;code&gt;a&lt;/code&gt; is occupied it try &lt;code&gt;a+1&lt;/code&gt; if it also occupied it will try&lt;code&gt;a+2&lt;/code&gt; if it reach &lt;code&gt;m&lt;/code&gt; it will start again from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;a&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;so if it start at &lt;code&gt;1&lt;/code&gt; it will start from &lt;code&gt;1&lt;/code&gt; and end at &lt;code&gt;0&lt;/code&gt;  &lt;code&gt;{1,2,3 ... m-1 , 0}&lt;/code&gt; if it start at &lt;code&gt;k&lt;/code&gt; it will end at &lt;code&gt;k-1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the number of prob sequences is &lt;code&gt;m&lt;/code&gt; . if 2 elements have the initial prob is the same it's called secondary clustering. Primary clustering is if a continuous block of memory is sequentially occupied  it mean from &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;a+k&lt;/code&gt; is filled , so the probability of inserting an element will  be &lt;code&gt;k+1/m&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the worst search is &lt;code&gt;O(m)&lt;/code&gt; but the average is constant &lt;code&gt;O(2.5)&lt;/code&gt; their is a lot of proofs but they are hard and contain a lot of advanced probability theorems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quadratic probing
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;h'(k,i) = ( h(k) + c1*i + c2 i*i ) mod m&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the problem with quadratic probing is that we need to choose &lt;code&gt;c1 , c2 and m&lt;/code&gt;  in such a way that &lt;code&gt;h'(k,i)&lt;/code&gt; will examine entire table.&lt;/p&gt;

&lt;h3&gt;
  
  
  Double hashing
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;h'(k) = ( h1(k) + i * h2(k) ) mod m&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;if 2 elements have the same  &lt;code&gt;h1&lt;/code&gt;  , &lt;code&gt;h2&lt;/code&gt; probably will not be the same&lt;/p&gt;

&lt;p&gt;selection of &lt;code&gt;h2(k)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The secondary hash function &lt;code&gt;h2(k)&lt;/code&gt; it should never yield an index of zero&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it should cycle through the whole table&lt;/li&gt;
&lt;li&gt;it should be very fast to compute&lt;/li&gt;
&lt;li&gt;it should be pair-wise independent of  &lt;code&gt;h1(k)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The distribution characteristics of &lt;code&gt;h2(k)&lt;/code&gt;are irrelevant. It is analogous to a random-number generator - it is only necessary that &lt;code&gt;h2(k)&lt;/code&gt; be ’’relatively prime’’ to &lt;code&gt;m&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In practice, if division hashing is used for both functions, the divisors are chosen as primes.&lt;/p&gt;

</description>
      <category>datastrucres</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>#008 DS&amp;A - Graphs</title>
      <dc:creator>Omar</dc:creator>
      <pubDate>Tue, 08 Sep 2020 08:24:50 +0000</pubDate>
      <link>https://dev.to/omarkhatib/008-ds-a-graphs-719</link>
      <guid>https://dev.to/omarkhatib/008-ds-a-graphs-719</guid>
      <description>&lt;h2&gt;
  
  
  Graphs
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KU5KSwuj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2015/05/1431391699Fotolia_64334859_Subscription_Monthly_M.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KU5KSwuj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2015/05/1431391699Fotolia_64334859_Subscription_Monthly_M.jpg" alt="gt" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;graph theory is in general an algebra class to study relationships between things , in computer science the most common use is the networking.&lt;/p&gt;

&lt;h3&gt;
  
  
  Representation of Graphs
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6SvmINDU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/gP3OBURwq7Stm4N.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6SvmINDU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/gP3OBURwq7Stm4N.png" alt="image-20200906205359666" width="310" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;first representation is &lt;code&gt;adjacency matrix&lt;/code&gt; ever case represent the link between point and another as example &lt;code&gt;a&lt;/code&gt; doesn't have a relation with it self , while &lt;code&gt;a&lt;/code&gt; have relation with &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;a&lt;/code&gt; doesn't have direct relation with &lt;code&gt;c&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;a&lt;/th&gt;
&lt;th&gt;b&lt;/th&gt;
&lt;th&gt;c&lt;/th&gt;
&lt;th&gt;d&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;a&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;b&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;c&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;d&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;another popular representation is &lt;code&gt;linked list&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FjD0SxWc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/h6iKQsSqztMdDHc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FjD0SxWc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/h6iKQsSqztMdDHc.png" alt="image-20200906205820827" width="408" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;every node have pointer to nodes that connects to.&lt;/p&gt;

&lt;p&gt;a dense graph it's a graph that have lot of connections between it nodes.&lt;/p&gt;

&lt;p&gt;if the dense is&lt;code&gt;E=O(v^2)&lt;/code&gt; where &lt;code&gt;E&lt;/code&gt; is the number of edges and &lt;code&gt;v&lt;/code&gt; is the number of vertexes , and it mean that graph had lot of connection between them self , in other words matrix is almost all 1 .&lt;/p&gt;

&lt;p&gt;let's say we need a similar network for any social platform , the linked list make more sense because we are not going to connect with all in the network just with few friends.&lt;/p&gt;

&lt;p&gt;and the order of linked list is &lt;code&gt;O(V+2E)&lt;/code&gt; which is &lt;code&gt;&amp;lt;O(v^2)&lt;/code&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  BFS and DFS
&lt;/h3&gt;

&lt;p&gt;Visited mean that I visit or print it.&lt;/p&gt;

&lt;p&gt;Explored mean that I explore all the connections that she made (Neighbors).&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;visited&lt;/th&gt;
&lt;th&gt;explored&lt;/th&gt;
&lt;th&gt;meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;not visited and not explored&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;visited but not explored&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;visited and explored&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;you can use array for check visited nodes , for explorer there algorithms that use &lt;code&gt;Queue&lt;/code&gt; or &lt;code&gt;Stack&lt;/code&gt;, the one that use &lt;code&gt;Queue&lt;/code&gt; is called &lt;code&gt;BFS (Breadth First Search)&lt;/code&gt; and the one that use the &lt;code&gt;stack&lt;/code&gt; called &lt;code&gt;DFS (Depth First Search)&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;&lt;code&gt;BFS&lt;/code&gt; will traverse the tree like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QVaOa2TY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/8n3GYLCNUWsuXP2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QVaOa2TY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/8n3GYLCNUWsuXP2.png" alt="image-20200906224640923" width="492" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;DFS&lt;/code&gt; will traverse the tree like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ip-NQZ2r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/b1wpsjltOqrynUS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ip-NQZ2r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/b1wpsjltOqrynUS.png" alt="image-20200906224841615" width="468" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  BFS algorithm
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// the graph G and array visited[] are global
// visited[] is initialized to 0
BFS(v)
{
    u = v;
    visited[u] = 1;
    repeat
    {
        for all vertices w adj to u do
        {
            if(visited == 0)
            {
                add w to q;
                visited[w] = 1;
            }
        }

        if q is empty then return;
        delete the next element u from q;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;graph &lt;code&gt;G&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yj4TKayV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/GC2ONAIxYbqJQ7a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yj4TKayV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/GC2ONAIxYbqJQ7a.png" alt="image-20200907130759391" width="381" height="349"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;visited&lt;/code&gt; array is initialized to &lt;code&gt;0&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;3&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;5&lt;/th&gt;
&lt;th&gt;6&lt;/th&gt;
&lt;th&gt;7&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;queue &lt;code&gt;q&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;first iteration &lt;code&gt;u=1&lt;/code&gt; and vertices &lt;code&gt;w&lt;/code&gt; adj to &lt;code&gt;u&lt;/code&gt; are &lt;code&gt;w={2,3}&lt;/code&gt; , so visited and queue will be&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;3&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;5&lt;/th&gt;
&lt;th&gt;6&lt;/th&gt;
&lt;th&gt;7&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;after adding it to the queue &lt;code&gt;2&lt;/code&gt; will be marked as visited&lt;/p&gt;

&lt;p&gt;&lt;code&gt;visited&lt;/code&gt; array is initialized to &lt;code&gt;0&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;3&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;5&lt;/th&gt;
&lt;th&gt;6&lt;/th&gt;
&lt;th&gt;7&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;now we finish from &lt;code&gt;2&lt;/code&gt; we need to go to &lt;code&gt;3&lt;/code&gt; because we have a &lt;code&gt;for&lt;/code&gt; loop here to go to all adjacent of &lt;code&gt;1&lt;/code&gt; which they are &lt;code&gt;{2,3}&lt;/code&gt; &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;3&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;5&lt;/th&gt;
&lt;th&gt;6&lt;/th&gt;
&lt;th&gt;7&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;now we finish the &lt;code&gt;for&lt;/code&gt; loop &lt;code&gt;q&lt;/code&gt; is not empty so we are going to delete the next element from &lt;code&gt;q&lt;/code&gt; which is &lt;code&gt;2&lt;/code&gt; , now our &lt;code&gt;u&lt;/code&gt; become &lt;code&gt;2&lt;/code&gt; and neighbors of &lt;code&gt;2&lt;/code&gt; are &lt;code&gt;w={1,4,5}&lt;/code&gt; since &lt;code&gt;1&lt;/code&gt; is visited we are not going to visit it again and we do same logic as before until we reach an empty &lt;code&gt;q&lt;/code&gt; which mean all are visited and explored.&lt;/p&gt;

&lt;h3&gt;
  
  
  BFS analysis on linkedlist
&lt;/h3&gt;

&lt;p&gt;the queue implementation will take space complexity of &lt;code&gt;O(v)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the linkedlist will take &lt;code&gt;O(v+E)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  BFS analysis on adjacency matrix implementation
&lt;/h3&gt;

&lt;p&gt;if we have &lt;code&gt;8&lt;/code&gt; vertexes it mean we need a matrix of &lt;code&gt;8x8&lt;/code&gt; size , the array and Queue implementation of it each will have space complexity of &lt;code&gt;O(v)&lt;/code&gt; where v is number of vertexes , and the time complexity is &lt;code&gt;T(v^2)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Breadth First Traversal using BFS
&lt;/h3&gt;

&lt;p&gt;in order to know if the graph is connected , it mean all the nodes are in same graph &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M5b4-2hk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/VB2Q49jIGicDgEz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M5b4-2hk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/VB2Q49jIGicDgEz.png" alt="image-20200906235251651" width="395" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;those can be separated into 2 graphs &lt;code&gt;U&lt;/code&gt; and &lt;code&gt;G&lt;/code&gt; there at least one node that doesn't have intersection from &lt;code&gt;U&lt;/code&gt; with &lt;code&gt;V&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;to traverse those two sub graphs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BFT(G,n)
{
    // this loop to fill the array with not visited yet
    for i=1 to n do
        visited[i] = 0
    // this will visit every node that is not visited yet
    for i=1 to n do
        if(visited[i]==0) then BFS(i) //BFS not BFT !!!!!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let each case represent a Node&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rb-czgbF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/SlsyJ8Pv6BYuEjT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rb-czgbF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/SlsyJ8Pv6BYuEjT.png" alt="image-20200907002424315" width="250" height="123"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the first &lt;code&gt;4&lt;/code&gt; boxes are the &lt;code&gt;U&lt;/code&gt; graph with &lt;code&gt;4&lt;/code&gt; nodes , he will start from 1 and mark from &lt;code&gt;1-&amp;gt;4&lt;/code&gt; as visited , then he will break.&lt;/p&gt;

&lt;p&gt;after this it will start with &lt;code&gt;V&lt;/code&gt; and do same thing.&lt;/p&gt;

&lt;p&gt;time complexity is &lt;code&gt;O(E+V)&lt;/code&gt; and space complexity &lt;code&gt;O(V)&lt;/code&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  DFS Algorithm
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;DFS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="n"&gt;vertex&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;adj&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt;
            &lt;span class="n"&gt;DFS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in &lt;code&gt;DFS&lt;/code&gt; we start with a point and explore it , if the next node in it not explored we leave the previous node and explore the new node.&lt;/p&gt;

&lt;p&gt;initializing the visited array is done in the program that called the function , and we take it in count when we calculate the time complexity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yj4TKayV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/GC2ONAIxYbqJQ7a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yj4TKayV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/09/07/GC2ONAIxYbqJQ7a.png" alt="image-20200907130759391" width="381" height="349"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;V=1&lt;/th&gt;
&lt;th&gt;V=2&lt;/th&gt;
&lt;th&gt;V=4&lt;/th&gt;
&lt;th&gt;V=8&lt;/th&gt;
&lt;th&gt;V=5&lt;/th&gt;
&lt;th&gt;V=6&lt;/th&gt;
&lt;th&gt;V=3&lt;/th&gt;
&lt;th&gt;V=7&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;w={2,3}&lt;/td&gt;
&lt;td&gt;w={1,4,5}&lt;/td&gt;
&lt;td&gt;w={2,8}&lt;/td&gt;
&lt;td&gt;w={4,5,6,7}&lt;/td&gt;
&lt;td&gt;w={2,8}&lt;/td&gt;
&lt;td&gt;w={3,8}&lt;/td&gt;
&lt;td&gt;w={1,7}&lt;/td&gt;
&lt;td&gt;w={3,8}&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2 not visited&lt;/td&gt;
&lt;td&gt;4 not visited&lt;/td&gt;
&lt;td&gt;8 not visited&lt;/td&gt;
&lt;td&gt;5 not visisted&lt;/td&gt;
&lt;td&gt;all w visited we go back to v=8 , 6 not visited&lt;/td&gt;
&lt;td&gt;8 not visisted&lt;/td&gt;
&lt;td&gt;all w visited we go back to v=8 , 7 not visited&lt;/td&gt;
&lt;td&gt;all w visited , and all V are visited&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;as we see every time we have an unvisited node we fully explore it neighbors. if all the neighbors are visited we go back into old neighbors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Analysis of DFS and DFT
&lt;/h3&gt;

&lt;p&gt;space complexity of &lt;code&gt;DFS&lt;/code&gt; is &lt;code&gt;O(V)&lt;/code&gt; , the worst case if the graph is chain , because all the nodes will be on the stack in same time. So space complexity for &lt;code&gt;DFS&lt;/code&gt; and &lt;code&gt;BFS&lt;/code&gt; is &lt;code&gt;O(v)&lt;/code&gt; , time complexity is &lt;code&gt;O(E)+O(V)&lt;/code&gt; for the linked list representation where &lt;code&gt;O(V)&lt;/code&gt; is time to initialize the visited array and &lt;code&gt;O(V^2)&lt;/code&gt; for matrix representation , same as &lt;code&gt;BFS&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;&lt;code&gt;DFT&lt;/code&gt; will be the same as &lt;code&gt;BFT&lt;/code&gt; instead of calling &lt;code&gt;BFS&lt;/code&gt; we call &lt;code&gt;DFS&lt;/code&gt; .&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>#007 DS&amp;A - Trees</title>
      <dc:creator>Omar</dc:creator>
      <pubDate>Fri, 04 Sep 2020 16:15:56 +0000</pubDate>
      <link>https://dev.to/omarkhatib/007-ds-a-trees-490l</link>
      <guid>https://dev.to/omarkhatib/007-ds-a-trees-490l</guid>
      <description>&lt;p&gt;Binary trees are those who have max 2 childs , there are 3 types of binary tree traversal&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FXnB2zyckDYPTboH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FXnB2zyckDYPTboH.png" alt="image-20200901191830097"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;INORDER - L ROOT R - B A C&lt;/p&gt;

&lt;p&gt;PREORDER - ROOT L R - A B C&lt;/p&gt;

&lt;p&gt;POSTORDER - L R ROOT - B C A&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FrJOiZzpbSju1mwY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FrJOiZzpbSju1mwY.png" alt="image-20200901192247828"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;to write each order , we apply this method . First add dummy edges , start from A and first time you see A write it down , same for B and C. And then for inorder if you see A for second time write it and ....&lt;/p&gt;

&lt;p&gt;A B C - first time - PRE&lt;/p&gt;

&lt;p&gt;B A C - second time - IN&lt;/p&gt;

&lt;p&gt;B C A - third time - POST&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation and time complexity
&lt;/h3&gt;

&lt;p&gt;all the implementations are recursive manner so go back to recursive section and fully understand how to visualize it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// INORDER&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Inorder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Inorder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1 &lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2 &lt;/span&gt;
        &lt;span class="n"&gt;Inorder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to better understand it go back to recursion visualization and try to trace it&lt;/p&gt;

&lt;p&gt;time and space complexity are &lt;code&gt;O(n)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;to get the other traversal just change the &lt;code&gt;printf&lt;/code&gt; location&lt;/p&gt;

&lt;h3&gt;
  
  
  Double order traversal
&lt;/h3&gt;

&lt;p&gt;this is nothing but print every node twice (not necessarily successive) &lt;/p&gt;

&lt;p&gt;example : &lt;code&gt;abbcac&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// INORDER&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;DO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
        &lt;span class="n"&gt;DO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
        &lt;span class="n"&gt;DO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 4&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Triple order
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;TO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
        &lt;span class="n"&gt;TO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2 &lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
        &lt;span class="n"&gt;TO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 4&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Indirect recursion on tress
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;A&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
        &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
        &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;B&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//1&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
        &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Number of binary trees possible
&lt;/h3&gt;

&lt;p&gt;if we have &lt;code&gt;1&lt;/code&gt; node , number of binary tress are &lt;code&gt;1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FOyUjQVqLAs6ChfF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FOyUjQVqLAs6ChfF.png" alt="image-20200901204947660"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;if we have &lt;code&gt;2&lt;/code&gt; node , number of binary tress are &lt;code&gt;2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FbWf6CTi9IuSePmV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FbWf6CTi9IuSePmV.png" alt="image-20200901205019982"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;if we have &lt;code&gt;3&lt;/code&gt; node , number of binary tress are &lt;code&gt;5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2F1S3xh25RMFLtAJ7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2F1S3xh25RMFLtAJ7.png" alt="image-20200901205119486"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the pattern here is&lt;code&gt;(2*n)C(n)/(n+1)&lt;/code&gt; c is combinator . so we have here &lt;code&gt;(2*3)C3 / 4 = 6C3 / 4 = 20/4 = 5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;but if we need to label nodes (put value for every node) is &lt;code&gt;(2*n)C(n) * n! /(n+1)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  construct unique Binary tree
&lt;/h3&gt;

&lt;p&gt;We can construct a unique binary tree using combination between &lt;code&gt;2&lt;/code&gt; of &lt;code&gt;preorder&lt;/code&gt; , &lt;code&gt;inorder&lt;/code&gt; and &lt;code&gt;postorder&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;drawing all the combinations of &lt;code&gt;A,B,C,D,E,F.....&lt;/code&gt; is time consuming so we are going to prove it with logic.&lt;/p&gt;

&lt;p&gt;let's take &lt;code&gt;IN : 1,2,3,4,5,6,7,8&lt;/code&gt; , &lt;code&gt;PRE: 5,3,1,2,4,6,8,7&lt;/code&gt; , &lt;code&gt;POST = ?&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;to get &lt;code&gt;POST&lt;/code&gt; we need to get a unique Binary search tree.&lt;/p&gt;

&lt;p&gt;let's take all the cases of &lt;code&gt;IN - L ROOT R&lt;/code&gt; and&lt;code&gt;PRE - ROOT L R&lt;/code&gt; where &lt;code&gt;L&lt;/code&gt; and &lt;code&gt;R&lt;/code&gt; are the subtree respectively to left and right.&lt;/p&gt;

&lt;p&gt;this is our &lt;code&gt;INORDER&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FxpO24eRgJoKhydu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FxpO24eRgJoKhydu.png" alt="image-20200901213957759"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;let's say 5 is the &lt;code&gt;ROOT&lt;/code&gt;  and in the &lt;code&gt;PRE&lt;/code&gt; 5 is also the &lt;code&gt;ROOT&lt;/code&gt;and all the elements in the right of it will be the &lt;code&gt;L&lt;/code&gt; and &lt;code&gt;R&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FqwbTQIPAnHcXFDg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FqwbTQIPAnHcXFDg.png" alt="image-20200901214149549"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in the left sub tree&lt;code&gt;1 2 3 4&lt;/code&gt; we look at the &lt;code&gt;PRE&lt;/code&gt; we have 3 is the next &lt;code&gt;ROOT&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;in the right sub tree &lt;code&gt;6 7 8&lt;/code&gt;the &lt;code&gt;PRE&lt;/code&gt; it is &lt;code&gt;6 8 7&lt;/code&gt; so the &lt;code&gt;ROOT&lt;/code&gt; is 6 and &lt;code&gt;8 7&lt;/code&gt; to the right of it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FE6r7UNLaV1giGnQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FE6r7UNLaV1giGnQ.png" alt="image-20200901214514888"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;so we have &lt;code&gt;1 2&lt;/code&gt; in &lt;code&gt;PRE&lt;/code&gt; the &lt;code&gt;2&lt;/code&gt; is to the right of &lt;code&gt;1&lt;/code&gt; so &lt;code&gt;2&lt;/code&gt; will fall to the right&lt;/p&gt;

&lt;p&gt;for &lt;code&gt;7 8&lt;/code&gt; in &lt;code&gt;PRE&lt;/code&gt; we have &lt;code&gt;8&lt;/code&gt; is the &lt;code&gt;ROOT&lt;/code&gt; and &lt;code&gt;7&lt;/code&gt; to the left of it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2Fb9mK2BY3nG5eTVk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2Fb9mK2BY3nG5eTVk.png" alt="image-20200901214747914"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;so we form a unique &lt;code&gt;POST&lt;/code&gt; binary tree from &lt;code&gt;IN&lt;/code&gt; and &lt;code&gt;PRE&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;if we get the &lt;code&gt;POST&lt;/code&gt; of it it's mean print it if it's seen the &lt;code&gt;3&lt;/code&gt; time.&lt;/p&gt;

&lt;p&gt;POST = &lt;code&gt;2 1 4 3 7 8 6 5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;an exercise take &lt;code&gt;POST&lt;/code&gt; and &lt;code&gt;IN&lt;/code&gt; and form the &lt;code&gt;PRE&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Recursive program to count the number of nodes
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;NN(T) = 1+NN(LST)+NN(RST)&lt;/code&gt;and if &lt;code&gt;T&lt;/code&gt; is &lt;code&gt;NULL&lt;/code&gt; then &lt;code&gt;NN(T)=0&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;NN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
        &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  counting number of leaf
&lt;/h3&gt;

&lt;p&gt;a leaf is a Node with left and right sub trees are NULL&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;NL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;NL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;time complexity : &lt;code&gt;O(n)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  find the full nodes
&lt;/h3&gt;

&lt;p&gt;full node is the tree who have left and right child&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FN(t)   = 0 ; T = NULL
        = 0 ; T is a leaf
        = FN(T-&amp;gt;LST) + FN(T-&amp;gt;RST); if T has on one child
        = FN(T-&amp;gt;LST) + FN(T-&amp;gt;RST)+1; if T is full node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;FN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;FN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;FN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;time complexity : &lt;code&gt;O(n)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Recursive program to find the height of a tree
&lt;/h3&gt;

&lt;p&gt;the height is the length of the path to the last leaf you can access&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FXhP1wFIvA4CaEbj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FXhP1wFIvA4CaEbj.png" alt="image-20200902122629995"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;H(t) = {0 ; T is empty
       {0 ; T is a leaf
       {Hmax(H(LST) , H(RST)); otherwise
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;H&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Binary search tree
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;50,15,62,5,20,58,91,3,8,37,60,24&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2Faofv5LZ3PKWimcX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2Faofv5LZ3PKWimcX.png" alt="image-20200902123829442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;every sub tree in the left is smaller than the root of it and in the right is the higher&lt;/p&gt;

&lt;p&gt;&lt;code&gt;INORDER&lt;/code&gt;: &lt;code&gt;3,5,8,15,20,24,37,50,58,60,62,91&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;so &lt;code&gt;INORDER&lt;/code&gt; is nothing but the binary search tree sorted&lt;/p&gt;

&lt;h3&gt;
  
  
  Binary search tree - POST to IN to PRE
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;POST ORDER    : 10,9,23,22,27,25,15,50,95,60,40,29&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;we can get directly the &lt;code&gt;INORDER&lt;/code&gt; hence is nothing more than the &lt;code&gt;BST&lt;/code&gt; sorted&lt;/p&gt;

&lt;p&gt;&lt;code&gt;INORDER   : 9,10,15,22,23,25,27,29,40,50,60,95&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;to get that &lt;code&gt;PREORDER&lt;/code&gt; we need to draw the tree&lt;/p&gt;

&lt;p&gt;the root is &lt;code&gt;29&lt;/code&gt; since it's the last in the &lt;code&gt;POST&lt;/code&gt; , so the root construction is going to be like this , then we go backward &lt;code&gt;40&amp;gt;29&lt;/code&gt; so it is going to fall to the right and so on.. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FiYCTag2XswbVcHZ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FiYCTag2XswbVcHZ.png" alt="image-20200902125518756"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;after we trace it we get &lt;code&gt;PREODER  : 29,15,9,10,25,22,23,27,40,60,50,95&lt;/code&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Number of BST with 3 Distinct keys
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;1 2 3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FMgEOe4fDulNYmQF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FMgEOe4fDulNYmQF.png" alt="image-20200902131102208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2nCn / n+1 = 6C3 / 4 = 20 / 4 = 5&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Identify IN-ORDER , PRE-ORDER and POST-ORDER
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;1&lt;/code&gt; : &lt;code&gt;M B C A F H P Y K&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2&lt;/code&gt;: &lt;code&gt;K A M C B Y P F H&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;3&lt;/code&gt;:&lt;code&gt;M B A C K Y F P H&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;we have those three trees we don't know what is the type of traversal of each one.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;K&lt;/code&gt; appear first in &lt;code&gt;2&lt;/code&gt; and last in &lt;code&gt;1&lt;/code&gt; so it's the &lt;code&gt;ROOT&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;after drawing the tree we get  &lt;code&gt;1 is POST&lt;/code&gt; ,&lt;code&gt;2 is PRE&lt;/code&gt; and &lt;code&gt;3 is IN&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Delete a node from BST
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FkvzN72xOtocrsKX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FkvzN72xOtocrsKX.png" alt="image-20200902180736560"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;if node is a leaf we can delete it , if node is a Non Leaf and one child the child point to the parent of deleted node. If the node have a subtree we replace the greatest element of it. &lt;/p&gt;

&lt;p&gt;Examples: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We  can delete node &lt;code&gt;2&lt;/code&gt; without making any changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can delete node &lt;code&gt;25&lt;/code&gt; and point &lt;code&gt;27&lt;/code&gt; to &lt;code&gt;30&lt;/code&gt; instead&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If we need to delete node &lt;code&gt;15&lt;/code&gt; we replace it with &lt;code&gt;12&lt;/code&gt; &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Finding minimum and maximum
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;find_min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;find_max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Recursive program on testing type of tree
&lt;/h3&gt;

&lt;p&gt;there is arguing on type of trees those are one of their definition , other reference may differ&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;complete binary is a tree which have 2 or 0 childrens.
full binary tree mean every layer should be node and last row should be leafs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;iscomplete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iscomplete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;iscomplete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  AVL Trees and balancing
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;AVL tree&lt;/strong&gt; (named after inventors &lt;strong&gt;A&lt;/strong&gt;delson-&lt;strong&gt;V&lt;/strong&gt;elsky and &lt;strong&gt;L&lt;/strong&gt;andis) it's the most popular one there is others like &lt;code&gt;(R-B) and (2-3)&lt;/code&gt;  the heights of the two child subtrees of any node differ by at most one; if at any time they differ  by more than one, re-balancing is done to restore this property. Lookup,  insertion, and deletion all take &lt;code&gt;O(log n)&lt;/code&gt; time in both the average and worst cases, where &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwikimedia.org%2Fapi%2Frest_v1%2Fmedia%2Fmath%2Frender%2Fsvg%2Fa601995d55609f2d9f5e233e36fbe9ea26011b3b" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwikimedia.org%2Fapi%2Frest_v1%2Fmedia%2Fmath%2Frender%2Fsvg%2Fa601995d55609f2d9f5e233e36fbe9ea26011b3b" alt="n"&gt;&lt;/a&gt;is the number of nodes in the tree prior to the operation. Insertions  and deletions may require the tree to be rebalanced by one or more tree balancing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Ff%2Ffd%2FAVL_Tree_Example.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Ff%2Ffd%2FAVL_Tree_Example.gif" alt="AVL"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;BF = height(LST)-height(RST)&lt;/code&gt; the root should be either &lt;code&gt;{-1,0,1}&lt;/code&gt; else we rotated . If it is left-unbalance we rotate it clock wise else opposite clock wise.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FX5aRtPSDgnANIGd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FX5aRtPSDgnANIGd.png" alt="image-20200902184306140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the root is &lt;code&gt;2 != {-1,0,1}&lt;/code&gt;  so it's not balanced , and it's left unbalanced so we need to rotate &lt;code&gt;CW&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FYeoaPduzkhAHSRE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FYeoaPduzkhAHSRE.png" alt="image-20200902184539125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now the root is &lt;code&gt;0&lt;/code&gt; so now it's balanced.&lt;/p&gt;

&lt;h3&gt;
  
  
  constructing AVL trees and time complexity
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;50 , 20 , 60 ,10 , 8 , 15 , 32 ,46 , 11 , 48&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FA5eRNMyhqPwS9gj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FA5eRNMyhqPwS9gj.png" alt="image-20200902185026558"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;go clock wise&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FNywXpuz4eTALPgo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FNywXpuz4eTALPgo.png" alt="image-20200902185227460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now rotate &lt;code&gt;Anti Clock wise&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2F2Tr3g4VnoyIicpR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2F2Tr3g4VnoyIicpR.png" alt="image-20200902185617891"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now it's &lt;code&gt;Left-Left&lt;/code&gt; so we rotate &lt;code&gt;CW&lt;/code&gt; and so on we continue and in the end we get this tree&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FEFuivjxtaPqwWLz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F02%2FEFuivjxtaPqwWLz.png" alt="image-20200902185753117"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;BST&lt;/th&gt;
&lt;th&gt;Balanced BST&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;search&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O( log n )&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;height&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O( log n )&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Insertion&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;it's &lt;code&gt;O(log n)&lt;/code&gt; because the height is smaller &lt;/p&gt;

&lt;h3&gt;
  
  
  Minimum and Maximum nodes in AVL tree
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;S(h) = S(h-1) + S(h-2) + 1

S(1) = 0
S(2) = 1

So let's say h = 6, then S(h = 6) will be (just replacing):

S(6) = S(6-1) + S(6-2) + 1
S(6) = S(5) + S(4) + 1 
S(6) = 2*S(4) + S(3) + 1 + 1
S(6) = 2*(S(3) + S(2) + 1) + S(3) + 2
S(6) = 3*S(3) + 2*S(2) + 4
S(6) = 3*(S(2) + S(1) + 1) + 2*S(2) + 4
S(6) = 5*S(2) + 3*S(1) + 7
S(6) = 5*1 + 3*0 + 7
S(6) = 12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Expression trees
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;a+b&lt;/code&gt; take this and let's draw the tree&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F03%2FRKsOPAglQdvpeJH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F03%2FRKsOPAglQdvpeJH.png" alt="image-20200902201259345"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PRE : +ab
IN  : a+b
POST: ab+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;a+b*c&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F03%2F4Ki9qdCnF2fkXmN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F03%2F4Ki9qdCnF2fkXmN.png" alt="image-20200902201738424"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PRE : +a*bc
IN  : a+b*c
POST: abc*+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Various tree representations
&lt;/h3&gt;

&lt;p&gt;The method we see is a pointer to the left and right child , what if we have like 10 childs? &lt;/p&gt;

&lt;p&gt;There is a representation called &lt;code&gt;LCRS (Left Child Right Sibling)&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="/home/omar/.config/Typora/typora-user-images/image-20200902210603966.png" class="article-body-image-wrapper"&gt;&lt;img src="/home/omar/.config/Typora/typora-user-images/image-20200902210603966.png" alt="image-20200902210603966"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F05%2FkEYQxibD5MFfrhj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.loli.net%2F2020%2F09%2F05%2FkEYQxibD5MFfrhj.png" alt="image-20200904190326839"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LC = 2*i
RC = 2*i+1
Parent = x/2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because it's binary tree multiplying by &lt;code&gt;2&lt;/code&gt; is nothing but shifting by &lt;code&gt;1&lt;/code&gt; bit.&lt;/p&gt;

&lt;p&gt;if we have i = 2 (base 10) = 10 (base 2) , &lt;code&gt;2*i = 100&lt;/code&gt; and &lt;code&gt;2*i+1 = 101&lt;/code&gt;  , same for divining by 2 in binary &lt;code&gt;100/2 = 10&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;another representation is like this&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(Root (a b c) (d e f) )&lt;/code&gt; where &lt;code&gt;(a b c)&lt;/code&gt; is &lt;code&gt;LSB&lt;/code&gt; of &lt;code&gt;Root&lt;/code&gt; and &lt;code&gt;(d e f)&lt;/code&gt; is &lt;code&gt;RSB&lt;/code&gt; of &lt;code&gt;Root&lt;/code&gt; and &lt;code&gt;a&lt;/code&gt; is root of &lt;code&gt;LSB&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; is left leaf and &lt;code&gt;c&lt;/code&gt; is right leaf , same for &lt;code&gt;d e f&lt;/code&gt;&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Get Waifu on terminal</title>
      <dc:creator>Omar</dc:creator>
      <pubDate>Mon, 31 Aug 2020 13:35:25 +0000</pubDate>
      <link>https://dev.to/omarkhatib/get-waifu-on-terminal-lef</link>
      <guid>https://dev.to/omarkhatib/get-waifu-on-terminal-lef</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;I see a lot of programmers interested in having a waifu in terminal.&lt;br&gt;
So here how I do it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HKTynEwU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/y9joghovile7u8obv0dp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HKTynEwU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/y9joghovile7u8obv0dp.png" alt="waifu" width="800" height="439"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  first
&lt;/h2&gt;

&lt;p&gt;go to any search engine and search for &lt;code&gt;Anime black and white png&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P8vgEyFK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/m4eecf5yn48pbkqhe7xy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P8vgEyFK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/m4eecf5yn48pbkqhe7xy.png" alt="Alt Text" width="800" height="530"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;then open any photo editor , I use &lt;a href="https://www.photopea.com/"&gt;photopea online&lt;/a&gt; create a new transparent image with same dimension of your screen . In my case it's &lt;code&gt;1920x1080&lt;/code&gt; . Drag and drop your waifu into the corner and save it as png.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ob2pc4Gn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/vjr2gmr29xdf1s6isaxs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ob2pc4Gn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/vjr2gmr29xdf1s6isaxs.png" alt="Alt Text" width="426" height="669"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;final result should look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iS7J3-4P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/nzyfouz76c0rxtjpqe20.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iS7J3-4P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/nzyfouz76c0rxtjpqe20.png" alt="Alt Text" width="800" height="476"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  second
&lt;/h2&gt;

&lt;p&gt;move your image to any location you like mine is &lt;code&gt;$HOME/.config/kitty/&lt;/code&gt; in same folder with kitty configuration.&lt;br&gt;
first I am using the &lt;a href="https://sw.kovidgoyal.net/kitty/"&gt;kitty&lt;/a&gt; terminal.after install it go to the config files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vim ~/.config/kitty/kitty.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then add those lines to the end of the files , also you can delete the config file and make new one with those lines only because all the file is just comments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;foreground white                                                                                                                     
background black                                                                                                                     
background_opacity 0.989
background_image &lt;span class="nv"&gt;$HOME&lt;/span&gt;/.config/kitty/waifu.png                                                                                                                                                                                                                     
background_image_layout scaled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>linux</category>
      <category>bash</category>
    </item>
    <item>
      <title>#002 - Clean Code - Names</title>
      <dc:creator>Omar</dc:creator>
      <pubDate>Sat, 29 Aug 2020 17:53:06 +0000</pubDate>
      <link>https://dev.to/omarkhatib/002-clean-code-names-5akk</link>
      <guid>https://dev.to/omarkhatib/002-clean-code-names-5akk</guid>
      <description>&lt;h2&gt;
  
  
  Names
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Reveal your Intent
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Intent : Nawaya in Arabic&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Variables are all around the programm having good names will help read code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// elapsed time in days&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in past documentations were good back in old days , but now it's better to have the variable name is his own comment&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;elapsedTimeInDays&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if your code is something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mpd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it is better to write it like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;middle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;days&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;middlePlusDays&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;middle&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is an old mindset that this will cost us more cpu cycles. But we live in a new world where we know now that what matter is code maintainability.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;ID_NONE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// constant ID for NONE&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;ID_FIRST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="c1"&gt;// constant ID for FIRST&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;ID_SECOND&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="c1"&gt;// constant ID for SECOND&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;ID_BOTH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="c1"&gt;// constant ID for BOTH&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;look at those variable names and comments , what did you understand from this code?&lt;/p&gt;

&lt;p&gt;actually name nor comments mean nothing for me as reader.&lt;/p&gt;

&lt;p&gt;you will tell me read code this is a shame , I am busy/lazy I don't have time for your bad code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Avoid Disinformation
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m0SZdQy3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Ftse1.mm.bing.net%252Fth%253Fid%253DOIP.6Zc1qzDZsXqp25T9mkdWnwHaDw%2526pid%253DApi%26f%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m0SZdQy3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Ftse1.mm.bing.net%252Fth%253Fid%253DOIP.6Zc1qzDZsXqp25T9mkdWnwHaDw%2526pid%253DApi%26f%3D1" alt="disinformation" width="474" height="240"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;tuesday&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;look at this expression What you think it mean? Well what I need is to make sure that if today is tuesday&lt;/p&gt;

&lt;p&gt;isn't better if we write it like this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;isTuesday&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this is disinformation i give you an variable that you may miss understand that this is a boolean.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pronounceable Names
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;BMW&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;how I need to pronounce this?&lt;/p&gt;

&lt;p&gt;How I should understand this? does it mean the bmw car? should I read it like boomwww?&lt;/p&gt;

&lt;p&gt;isn't this better?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;BestMatchingWord&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;names are the tools to communicate with others , so make sure to express what you are saying well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Avoid Encodings
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q9QFmshM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://image3.slideserve.com/6972102/hungarian-notation1-l.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q9QFmshM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://image3.slideserve.com/6972102/hungarian-notation1-l.jpg" alt="Hungurian notation" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;those are the hungarian Notations , those was usefull back in old days to know what is the type of your variables. But now our IDE is better he can know what is the type of a variable by hovering over it , And our compiler , unit tests will protect us from any type error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parts of Speech
&lt;/h3&gt;

&lt;p&gt;your functions should be methods like &lt;code&gt;getTotal() , divide()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;structs should be Adjectives like &lt;code&gt;struct COLORS = {...}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;your booleans should be descreptive like &lt;code&gt;isUpper() , isLower()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;your calsses should be like this &lt;code&gt;class Account&lt;/code&gt; not &lt;code&gt;class DATA , class INFO , class MANAGER&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Scope Length Rule
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VPTYqILF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Fae01.alicdn.com%252Fkf%252FHTB1e7JoOVXXXXcHXVXXq6xXFXXXO%252FVERY100-Tactical-Alumnium-40mm-Rifle-Scope-Sunshade-For-Many-Rifle-Scopes-Length-76mm-3.jpg%26f%3D1%26nofb%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VPTYqILF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://external-content.duckduckgo.com/iu/%3Fu%3Dhttps%253A%252F%252Fae01.alicdn.com%252Fkf%252FHTB1e7JoOVXXXXcHXVXXq6xXFXXXO%252FVERY100-Tactical-Alumnium-40mm-Rifle-Scope-Sunshade-For-Many-Rifle-Scopes-Length-76mm-3.jpg%26f%3D1%26nofb%3D1" alt="scope" width="800" height="800"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TestResult&lt;/span&gt; &lt;span class="n"&gt;tr&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;configIssues&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Element&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;createElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tr&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;rootElement&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;appendChild&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;tr&lt;/code&gt; is a fine name because the scoop where it is declared is small , and had been used directly after his declaration.&lt;/p&gt;

&lt;p&gt;but what is not fine is &lt;code&gt;d&lt;/code&gt; it is not declared in same scope of it's defenation&lt;/p&gt;

&lt;p&gt;&lt;code&gt;d&lt;/code&gt; is an instance of a class Document , it is better to call it doc or document.&lt;/p&gt;

&lt;p&gt;so in small scope the shorter names are better , but in large scopes bigger are better , so &lt;strong&gt;yes size matter :)&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;While for functions , public functions that are going to be called in many other places outside the class should have small names. Why? the Class have a name example &lt;code&gt;File.Open()&lt;/code&gt; Imagine calline &lt;code&gt;File.openFileAndThrowIfNotFound()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and private functions should be long because they are called locally.&lt;/p&gt;

</description>
      <category>codequality</category>
      <category>productivity</category>
    </item>
    <item>
      <title>#006 DS&amp;A - Stacks and queues</title>
      <dc:creator>Omar</dc:creator>
      <pubDate>Sat, 29 Aug 2020 16:09:24 +0000</pubDate>
      <link>https://dev.to/omarkhatib/006-ds-a-stacks-and-queues-18e7</link>
      <guid>https://dev.to/omarkhatib/006-ds-a-stacks-and-queues-18e7</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Nothing to say more than hello 👋,&lt;br&gt;
In Case your wondering who is this man in the banner it's the Khwarizmi the inventor of algorithms concept.&lt;br&gt;
Make sure to follow because I will start more advanced series in future.&lt;/p&gt;
&lt;h2&gt;
  
  
  Stacks and queues
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Implementation of Stack using arrays
&lt;/h3&gt;

&lt;p&gt;the problem of arrays that we should know the max number of elements we need to use, If you can't predict the size go with linkedlist.&lt;/p&gt;

&lt;p&gt;A stack mean that we can only get last element and first input is last output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// mean stack is empty&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"overflow"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ++top will increment top first&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"underflow"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Linked List implementation of stack
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YfcRvx6a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/29/eJRWAKZ7HM3bsBN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YfcRvx6a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/29/eJRWAKZ7HM3bsBN.png" alt="image-20200829171708212" width="565" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;he head will link to the new node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"error of malloc"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;pushing time is &lt;code&gt;O(1)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"underflow"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;pop is &lt;code&gt;O(1)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Queue using circular array
&lt;/h3&gt;

&lt;p&gt;even if a circular array we visualize it as an circular array.&lt;/p&gt;

&lt;p&gt;front and rear will both be pointing to the 0 element. let's say we have an array of 4 integers&lt;/p&gt;

&lt;p&gt;first type I need to put an element the rear will move &lt;code&gt;+1&lt;/code&gt; when the rear is already reach n-1 again I need to put it at n-1 so I will use (rear+1) mod n , dashed rear it mean it pass from here it start from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e-8w7VT2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/29/gqE32LIFz75Moxk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e-8w7VT2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/29/gqE32LIFz75Moxk.png" alt="image-20200829174026353" width="428" height="317"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// delete element&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;dequeue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;front&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="n"&gt;rear&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Q is empty"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;front&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;front&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;front&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// insert element&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;rear&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rear&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;front&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;rear&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Q is full"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rear&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;rear&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;rear&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rear&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rear&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Queue will be full if &lt;/p&gt;

&lt;p&gt;&lt;code&gt;(rear+1) mod n == front&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Queue will be empty if&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rear == front&lt;/code&gt;&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>#005 DS&amp;A - Linked List</title>
      <dc:creator>Omar</dc:creator>
      <pubDate>Fri, 28 Aug 2020 19:37:44 +0000</pubDate>
      <link>https://dev.to/omarkhatib/005-ds-a-linked-list-3e43</link>
      <guid>https://dev.to/omarkhatib/005-ds-a-linked-list-3e43</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Nothing to say more than hello 👋,&lt;br&gt;
In Case your wondering who is this man in the banner it's the Khwarizmi the inventor of algorithms concept.&lt;br&gt;
Make sure to follow because I will start more advanced series in future.&lt;/p&gt;
&lt;h2&gt;
  
  
  Linked List
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Single Linked List
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;self reference structures we talk about them before.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UvnMhILO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/28/tjQoaqgiBmR9ywk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UvnMhILO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/28/tjQoaqgiBmR9ywk.png" alt="image-20200828103740415" width="578" height="196"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;first element is called head.&lt;/p&gt;

&lt;p&gt;arrays are dynamic access , while linked lists are sequential accessible , it mean you need to visit all the previous element in order to reach the element you need , it mean &lt;code&gt;O(n)&lt;/code&gt; while arrays are &lt;code&gt;O(1)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;searching in both will take &lt;code&gt;O(n)&lt;/code&gt; .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 30&lt;/span&gt;
&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 50 will link to 30 , so 60 is lost&lt;/span&gt;
&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 10 will link to 40&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// d&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;This linked list is same for all the followings. We are assuming that our linked list have more than 1 node , if it is 1 node we should put conditions to make sure that if our linked list can perform certain things.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sD95TIm6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/28/s3VXkyCtZPTLRzl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sD95TIm6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/28/s3VXkyCtZPTLRzl.png" alt="image-20200828104846860" width="404" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Traversing a single linked list
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// or while(t) have same meaning&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we are using &lt;code&gt;t&lt;/code&gt; because if we lost the head , it's mean we lost the entire node.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inserting an element in single linked list
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this is will create a pointer called new&lt;/p&gt;

&lt;p&gt;a) insert at beginning :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;b) insert at the end&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;c) insert a node at specific element&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// asume we need to insert after node 2&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Deleting a node from single linked list
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;malloc&lt;/code&gt; will create a space for us while &lt;code&gt;free&lt;/code&gt; will free the memory that we got it from &lt;code&gt;malloc&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// assume we are resetting the linked list to it's init state&lt;/span&gt;
&lt;span class="c1"&gt;// deleting a node from the head&lt;/span&gt;
&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// deleting a node from the tail&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// delete a specific node&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// or t-&amp;gt;next-&amp;gt;next&lt;/span&gt;
&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  example 1
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;rearrange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// if linkedlist have no or 1 element return&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// q pointing to 1st element and q to second&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// while q is not null do&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// swap q and p&lt;/span&gt;
        &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// if p is pointing to something it will take it otherwise 0&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// output : 2 1 4 3 6 5 7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  printing the elements using recursion
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// print then recursion&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;//1&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="c1"&gt;//2&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;//3&lt;/span&gt;
        &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;//4&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="c1"&gt;//5&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// output : 1 2 3 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will print before start go into next stack&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7kDjTNxi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/28/eKO6Xtr4YPhHgWd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7kDjTNxi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/28/eKO6Xtr4YPhHgWd.png" alt="image-20200828120742122" width="538" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;when it's go back it will return to line 5&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eAzCRzoT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/28/h3FcojkTBnJ8Psq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eAzCRzoT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/28/h3FcojkTBnJ8Psq.png" alt="image-20200828120836003" width="532" height="416"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// recursion then print === print in reverse order&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;//1&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="c1"&gt;//2&lt;/span&gt;
        &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;//3&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$d"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;//4&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="c1"&gt;//5&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// output : 4 3 2 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--epxRzWYi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/28/4S9nF8TCIoWwmzV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--epxRzWYi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/28/4S9nF8TCIoWwmzV.png" alt="image-20200828120928007" width="528" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;then when it go back it will start at line 4 which printing the value of p stored inside of each stack&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qnlXbRuQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/28/NZE4LGb8lwQB61m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qnlXbRuQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/28/NZE4LGb8lwQB61m.png" alt="image-20200828121009197" width="529" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Reversing an single linked list using iteration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nextNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;nextNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;nextNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reversing an single linked list using recursion
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; 
        &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
    &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;//..&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  circular Linked List
&lt;/h3&gt;

&lt;p&gt;circular linked list have it's tail pointing to sentinel which is the head but containing the number of nodes instead with pointer to first element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OsAux4mK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/29/dqzmLt2uBQYwe98.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OsAux4mK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/29/dqzmLt2uBQYwe98.png" alt="image-20200828222532882" width="516" height="195"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;){}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  double linked list
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YSYs7z3B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/29/l54SZnRKwiou2JT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YSYs7z3B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.loli.net/2020/08/29/l54SZnRKwiou2JT.png" alt="image-20200828222931943" width="507" height="192"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// insert at start&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// insert at the end&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// insert between &amp;gt; 1 and &amp;lt; n where n is number of node&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// p is the pointer of the element previous to t&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>datastructures</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
