<?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>Forem: German Valencia</title>
    <description>The latest articles on Forem by German Valencia (@sttamper).</description>
    <link>https://forem.com/sttamper</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%2F838926%2F1b346b61-e8d9-444c-a2cc-fa8dd5f7095e.png</url>
      <title>Forem: German Valencia</title>
      <link>https://forem.com/sttamper</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sttamper"/>
    <language>en</language>
    <item>
      <title>Semantic Versioning with Jreleaser and Upcoming4j</title>
      <dc:creator>German Valencia</dc:creator>
      <pubDate>Fri, 30 Jan 2026 19:04:17 +0000</pubDate>
      <link>https://forem.com/stamperlabs/semantic-versioning-with-jreleaser-and-upcoming4j-75b</link>
      <guid>https://forem.com/stamperlabs/semantic-versioning-with-jreleaser-and-upcoming4j-75b</guid>
      <description>&lt;p&gt;While working with &lt;a href="https://www.npmjs.com/package/@semantic-release/npm" rel="noopener noreferrer"&gt;semantic-release npm package&lt;/a&gt; to fully automate application releases in &lt;code&gt;Nodejs&lt;/code&gt;, I got used to an out-of-the-box workflow where &lt;a href="https://www.conventionalcommits.org/en/v1.0.0/" rel="noopener noreferrer"&gt;conventional commits&lt;/a&gt; drive version calculation, git tagging, changelog generation, GitHub releases, and CI-safe publishing.&lt;/p&gt;

&lt;p&gt;When I switched to gradle-java projects, I could not find an equivalent solution. After researching the ecosystem, &lt;a href="https://jreleaser.org/" rel="noopener noreferrer"&gt;Jreleaser&lt;/a&gt; stood out as the most complete tool available for creating Git tags, generating changelogs, and publishing GitHub releases. However, it expects the version to be predefined. &lt;/p&gt;

&lt;p&gt;That gap led me to create &lt;a href="https://github.com/Stamper-Labs/upcoming4j" rel="noopener noreferrer"&gt;Upcoming4j&lt;/a&gt;, a lightweight &lt;code&gt;gradle&lt;/code&gt; plugin focused solely on semantic version calculation, designed to integrate seamlessly with &lt;code&gt;Jreleaser&lt;/code&gt; to fully automate software versioning.&lt;/p&gt;

&lt;p&gt;In the following step-by-step guide, I will show how to integrate &lt;code&gt;JReleaser&lt;/code&gt; and &lt;code&gt;Upcoming4j&lt;/code&gt; to automatically increment version numbers, create Git tags, generate changelogs, and publish GitHub releases based on Git commit history using semantic versioning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Install &lt;a href="https://sdkman.io/" rel="noopener noreferrer"&gt;Sdkman&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install JDK 21&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;sdk &lt;span class="nb"&gt;install &lt;/span&gt;java 21.0.9-amzn
&lt;span class="nv"&gt;$ &lt;/span&gt;java &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install git
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;git
&lt;span class="nv"&gt;$ &lt;/span&gt;git &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 1: Download Playground Gradle-Java Project &lt;code&gt;u4j-playground&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Download and unzip &lt;a href="https://drive.google.com/file/d/1nHtiCHjIVXvqnz5xa5rzR3VabbARMiZ1/view?usp=drive_link" rel="noopener noreferrer"&gt;the playground project&lt;/a&gt; into your local file system. This is a gradle-java project that will be used to demonstrate how to integrate JReleaser and Upcoming4j in practice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Install and Configure &lt;code&gt;Jreleaser&lt;/code&gt; Plugin
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;JReleaser&lt;/code&gt; uses a hosted Git repository to create Git tags and publish releases. Create a new repository on your &lt;strong&gt;GitHub&lt;/strong&gt; account and follow the instructions to link your local playground project to the remote repository.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Download the &lt;a href="https://gist.github.com/sttamper/0bd186b222258d2c93fb5ef3b80ac919" rel="noopener noreferrer"&gt;Jreleaser Configuration file&lt;/a&gt;, and place it in the following location: &lt;code&gt;/u4j-playground/gradle/jreleaser.gradle&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open the &lt;code&gt;/u4j-playground/build.gradle&lt;/code&gt; file to install and apply &lt;code&gt;Jreleaser&lt;/code&gt; configuration. The file should look like this now:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="n"&gt;plugins&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="s1"&gt;'application'&lt;/span&gt;
    &lt;span class="c1"&gt;// install JReleaser plugin&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="s1"&gt;'org.jreleaser'&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="s1"&gt;'1.22.0'&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Apply JReleaser configuration&lt;/span&gt;
&lt;span class="n"&gt;apply&lt;/span&gt; &lt;span class="nl"&gt;from:&lt;/span&gt; &lt;span class="s1"&gt;'gradle/jreleaser.gradle'&lt;/span&gt;

&lt;span class="c1"&gt;// Set a harcoded version for now&lt;/span&gt;
&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'1.0.0'&lt;/span&gt;

&lt;span class="k"&gt;repositories&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;mavenCentral&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;dependencies&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;testImplementation&lt;/span&gt; &lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;junit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;jupiter&lt;/span&gt;
    &lt;span class="n"&gt;testRuntimeOnly&lt;/span&gt; &lt;span class="s1"&gt;'org.junit.platform:junit-platform-launcher'&lt;/span&gt;
    &lt;span class="n"&gt;implementation&lt;/span&gt; &lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;guava&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;java&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;toolchain&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;languageVersion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JavaLanguageVersion&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;application&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;mainClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'org.example.App'&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;named&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'test'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;useJUnitPlatform&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;h2&gt;
  
  
  Step 4: Verify &lt;code&gt;Jreleaser&lt;/code&gt; Configuration
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create a &lt;a href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens" rel="noopener noreferrer"&gt;Personal Access Token&lt;/a&gt; in &lt;strong&gt;Github&lt;/strong&gt;, and export the token using the following environment variable:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;JRELEASER_GITHUB_TOKEN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;your_token
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Run &lt;code&gt;JReleaser&lt;/code&gt; in &lt;code&gt;--dryrun&lt;/code&gt; mode to preview the release steps without making any changes:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./gradlew clean jreleaserRelease &lt;span class="nt"&gt;--no-configuration-cache&lt;/span&gt; &lt;span class="nt"&gt;--stacktrace&lt;/span&gt; &lt;span class="nt"&gt;--dryrun&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Check logs to verify outcomes:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fg6ocllmfskr9ujbw0v8x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fg6ocllmfskr9ujbw0v8x.png" alt="Jreleaser logs" width="800" height="853"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Notice that even in &lt;code&gt;--dryrun&lt;/code&gt; mode, &lt;code&gt;Jreleaser&lt;/code&gt; requires PAT being exported.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Notice that &lt;code&gt;JReleaser&lt;/code&gt; is not yet fully compliant with Gradle’s configuration cache. Therefore &lt;code&gt;--no-configuration-cache&lt;/code&gt; flag is being used.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 5: Install and Apply &lt;code&gt;Upcoming4j&lt;/code&gt; Plugin
&lt;/h2&gt;

&lt;p&gt;Open &lt;code&gt;/u4j-playground/build.gradle&lt;/code&gt; file and install &lt;code&gt;Upcoming4j&lt;/code&gt; plugin, the file should look like this now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="n"&gt;plugins&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="s1"&gt;'application'&lt;/span&gt;
    &lt;span class="c1"&gt;// install JReleaser plugin&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="s1"&gt;'org.jreleaser'&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="s1"&gt;'1.22.0'&lt;/span&gt;
    &lt;span class="c1"&gt;// Install Upcoming4j plugin&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="s1"&gt;'io.github.sttamper.upcoming4j'&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="s1"&gt;'0.0.4'&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Apply JReleaser configuration&lt;/span&gt;
&lt;span class="n"&gt;apply&lt;/span&gt; &lt;span class="nl"&gt;from:&lt;/span&gt; &lt;span class="s1"&gt;'gradle/jreleaser.gradle'&lt;/span&gt;

&lt;span class="c1"&gt;// Set up next version with Upcoming4j&lt;/span&gt;
&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nx&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;repositories&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;mavenCentral&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;dependencies&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;testImplementation&lt;/span&gt; &lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;junit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;jupiter&lt;/span&gt;
    &lt;span class="n"&gt;testRuntimeOnly&lt;/span&gt; &lt;span class="s1"&gt;'org.junit.platform:junit-platform-launcher'&lt;/span&gt;
    &lt;span class="n"&gt;implementation&lt;/span&gt; &lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;guava&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;java&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;toolchain&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;languageVersion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JavaLanguageVersion&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;application&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;mainClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'org.example.App'&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;named&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'test'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;useJUnitPlatform&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;h2&gt;
  
  
  Step 5: Verify &lt;code&gt;Upcoming4j&lt;/code&gt; Configuration
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Upcoming4j&lt;/code&gt; computes the next semantic version during Gradle’s configuration phase, ensuring that the correct version is always available in the &lt;code&gt;version&lt;/code&gt; property of the &lt;code&gt;build.gradle&lt;/code&gt; file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clean project:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; ./gradlew clean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Check Upcoming4j logs to confirm version calculation:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Flnlfl27llwketeu6aac1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Flnlfl27llwketeu6aac1.png" alt="Upcoming4j logs" width="800" height="251"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Step: Bump The Next Semantic Version
&lt;/h2&gt;

&lt;p&gt;Now you can run &lt;code&gt;JReleaser&lt;/code&gt; to create the next release version calculated by &lt;code&gt;Upcoming4j&lt;/code&gt;, based on the analysis of the commit history:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./gradlew clean jreleaserRelease &lt;span class="nt"&gt;--no-configuration-cache&lt;/span&gt; &lt;span class="nt"&gt;--stacktrace&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>gradle</category>
      <category>github</category>
      <category>git</category>
    </item>
    <item>
      <title>Customizing MacOS Terminal with Starship Like a Pro</title>
      <dc:creator>German Valencia</dc:creator>
      <pubDate>Sun, 18 Jan 2026 04:50:29 +0000</pubDate>
      <link>https://forem.com/stamperlabs/customizing-macos-terminal-with-starship-like-a-pro-2geb</link>
      <guid>https://forem.com/stamperlabs/customizing-macos-terminal-with-starship-like-a-pro-2geb</guid>
      <description>&lt;p&gt;As terminal applications grow more powerful and complex, managing multiple CLIs and their different versions becomes increasingly challenging. Customizing your terminal can make a big difference, helping you handle several tools at once without getting lost in the clutter.&lt;/p&gt;

&lt;p&gt;I previously relied on Oh My Zsh for prompt customization, but recently switched to &lt;a href="https://starship.rs/" rel="noopener noreferrer"&gt;Starship&lt;/a&gt; because it provides a simpler declarative approach to customization. So in this post, I will walk through how to build a clean starship prompt, including dynamic context for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AWS profiles &lt;/li&gt;
&lt;li&gt;Java (Gradle)&lt;/li&gt;
&lt;li&gt;Node.js&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Install &lt;a href="https://brew.sh/" rel="noopener noreferrer"&gt;Homebrew&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Install &lt;a href="https://iterm2.com/" rel="noopener noreferrer"&gt;Iterm2&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Install Starship
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;starship
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Integrate Starship with Zsh
&lt;/h2&gt;

&lt;p&gt;Open &lt;code&gt;.zshrc&lt;/code&gt; for edition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nano ~/.zshrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following line to load starship on every zsh session:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;eval&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;starship init zsh&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3 (Optional): Install Zsh Plugins
&lt;/h2&gt;

&lt;p&gt;The following plugins are optional, but they enhance your terminal experience by highlighting commands in real time and providing suggestions based on your command history and current input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;zsh-syntax-highlighting
brew &lt;span class="nb"&gt;install &lt;/span&gt;zsh-autosuggestions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open &lt;code&gt;.zshrc&lt;/code&gt; for edition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nano ~/.zshrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add following lines to load plugins on every zsh session:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;source&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;brew &lt;span class="nt"&gt;--prefix&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;/share/zsh-autosuggestions/zsh-autosuggestions.zsh
&lt;span class="nb"&gt;source&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;brew &lt;span class="nt"&gt;--prefix&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Install Nerd Fonts
&lt;/h2&gt;

&lt;p&gt;To ensure terminal can correctly render icons and custom glyphs, you need to install a &lt;strong&gt;Nerd Font&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew search font-fira-code-nerd-font
brew &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--cask&lt;/span&gt; font-fira-code-nerd-font
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5: Setting Terminal Font
&lt;/h2&gt;

&lt;p&gt;Update your terminal settings to use a Nerd Font: &lt;/p&gt;

&lt;h3&gt;
  
  
  Iterm2
&lt;/h3&gt;

&lt;p&gt;Navigate to: &lt;em&gt;&lt;strong&gt;Settings &amp;gt; Profiles &amp;gt; Default &amp;gt; General&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Terminal Font&lt;/td&gt;
&lt;td&gt;FiraCode Nerd Font&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Visual Studio Code integrated terminal
&lt;/h3&gt;

&lt;p&gt;Navigate to: &lt;em&gt;&lt;strong&gt;Settings &amp;gt; User&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;terminal.integrated.fontFamily&lt;/td&gt;
&lt;td&gt;FiraCode Nerd Font&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;editor.fontFamily&lt;/td&gt;
&lt;td&gt;Menlo, Monaco, 'Courier New', monospace, 'FiraCode Nerd Font’&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  MacOS native terminal
&lt;/h3&gt;

&lt;p&gt;Navigate to: &lt;em&gt;&lt;strong&gt;Settings &amp;gt; Profiles &amp;gt; Text&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Font&lt;/td&gt;
&lt;td&gt;FiraCode Nerd Font&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Step 6: Customize Starship Theme
&lt;/h2&gt;

&lt;p&gt;Create and open the &lt;code&gt;starship.toml&lt;/code&gt; configuration file for edition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; ~/.config &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; nano ~/.config/starship.toml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, download the Starship configuration from the following GitHub Gist:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://gist.github.com/sttamper/ff69056e8cb94be9397a2c5508e57018" rel="noopener noreferrer"&gt;Starship Configuration GitHub Gist&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Replace the contents of &lt;code&gt;starship.toml&lt;/code&gt; file with the configuration from the gist, then save the file and restart the terminal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Last Step: Play With The New Prompt
&lt;/h2&gt;

&lt;h3&gt;
  
  
  AWS profiles
&lt;/h3&gt;

&lt;p&gt;In the example below, after running &lt;code&gt;export AWS_PROFILE=root&lt;/code&gt;, Starship reflects the currently active AWS account directly in the prompt:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Starship displays the active AWS profile: &lt;code&gt;root&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The terminal prompt also shows the active AWS region: &lt;code&gt;us-east-1 (Virginia)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fr1n4umc7n0majdkwt7eb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fr1n4umc7n0majdkwt7eb.png" alt=" " width="800" height="557"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  NodeJS projects
&lt;/h3&gt;

&lt;p&gt;The following example shows how changing the Node.js version using NVM (&lt;code&gt;nvm use&lt;/code&gt;) is reflected in Starship:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Starship automatically updates the prompt to display the active Node.js version.&lt;/li&gt;
&lt;li&gt;Starship also shows useful Git repository information, such as the current branch and an icon indicating modified files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F70kk5tsacjh8syg0mrmu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F70kk5tsacjh8syg0mrmu.png" alt=" " width="800" height="552"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Java and Gradle projects
&lt;/h3&gt;

&lt;p&gt;In the following example, observe how Starship displays information about the current environment in a Java application directory:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The active Java version currently in use.&lt;/li&gt;
&lt;li&gt;The active Gradle version.&lt;/li&gt;
&lt;li&gt;Additional contextual information, such as the active AWS profile and region, when those tools are enabled.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F8mc1jig95dupr0wqjd4s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8mc1jig95dupr0wqjd4s.png" alt=" " width="800" height="665"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enjoy it!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>cli</category>
      <category>aws</category>
    </item>
    <item>
      <title>Pod and ReplicaSet Explained</title>
      <dc:creator>German Valencia</dc:creator>
      <pubDate>Fri, 13 Jan 2023 01:42:19 +0000</pubDate>
      <link>https://forem.com/sttamper/pod-and-replicaset-explained-3po0</link>
      <guid>https://forem.com/sttamper/pod-and-replicaset-explained-3po0</guid>
      <description>&lt;p&gt;Kubernetes is a technology plenty of features to manage containers. In this series of posts we will explore which I consider &lt;em&gt;&lt;strong&gt;the 4 key concepts&lt;/strong&gt;&lt;/em&gt; to understand whenever we want to deploy a simple application.&lt;/p&gt;

&lt;p&gt;Source code of examples &lt;a href="https://github.com/mcwiise/dev.to/tree/main/pod-replica-explained"&gt;on GitHub&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Brief Architectural Overview
&lt;/h1&gt;

&lt;p&gt;Kubernetes is based on a &lt;em&gt;&lt;strong&gt;master-worker&lt;/strong&gt;&lt;/em&gt; architecture to make sure high availability of the applications deployed in a cluster, where &lt;strong&gt;&lt;em&gt;workers&lt;/em&gt;&lt;/strong&gt; are the servers or nodes (physical machines) where applications run, and &lt;strong&gt;&lt;em&gt;master&lt;/em&gt;&lt;/strong&gt; is a node responsible for monitoring and updating the status of workers, so in case a worker gets down the master is able to start a new one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fps9opa0q1z8hp1vnflp4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fps9opa0q1z8hp1vnflp4.jpg" alt="k8s cluster" width="610" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Control Plane:&lt;/strong&gt; This is the controller node (master) which provides a set of functionalities to manage the Kubernetes cluster.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;API Server:&lt;/strong&gt; This is a super set of REST API's which allow to perform CRUD operations to resources in the cluster. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Nodes:&lt;/strong&gt; Physical machines (servers) where applications run and scale.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Kubelet:&lt;/strong&gt; This is an agent (endless process) that runs in every node, being responsible for registering a node in the control plane so it can monitor it, and given a pod specification, kubelet starts the containers making sure they keep running until the control plane decides to stop, recreate or delete them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Container runtime:&lt;/strong&gt; Kubernetes is agnostic of the technology to create containers, so it can be docker, container.d or whatever.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;kubectl:&lt;/strong&gt; This is a command line application we can use to perform actions into the cluster. This is just a wrapper of the REST APIs provided by the API server. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Objects Versioning
&lt;/h3&gt;

&lt;p&gt;Resources exposed by the REST APIs are called &lt;strong&gt;objects&lt;/strong&gt; which are under version control. A complete list of objects we can manage in a cluster can be found &lt;a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Objects can be described with .yaml files, and we can instruct the control plane to create or update them by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;kubectl apply &lt;span class="nt"&gt;-f&lt;/span&gt; object-descriptor.yaml 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's review now how to describe Pods, ReplicaSet, Deployments and Services which is the purpose of this post.&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting Started with a Pod
&lt;/h1&gt;

&lt;p&gt;This is the basic unit of deployment in Kubernetes, a pod is deployed into a node and can start from 1 to many containers, along with other objects like volumes.&lt;br&gt;
&lt;a href="https://media2.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%2F34tlvqedtpjg1xzdqt1l.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F34tlvqedtpjg1xzdqt1l.jpg" alt="pod" width="443" height="226"&gt;&lt;/a&gt;&lt;br&gt;
Pods are assigned a static IP address for internal communication, it means that &lt;em&gt;&lt;strong&gt;we cannot ping pods from outside the cluster&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Describing a Pod
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;pod-nginx.yaml&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;1. apiVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;v1&lt;/span&gt;
&lt;span class="na"&gt;2. kind&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Pod&lt;/span&gt;
&lt;span class="na"&gt;3. metadata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;4.   name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx-yaml&lt;/span&gt;
&lt;span class="na"&gt;5. spec&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;6.   containers&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;7.     - image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx&lt;/span&gt;
&lt;span class="na"&gt;8.       name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx&lt;/span&gt;
&lt;span class="na"&gt;9.       ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;10.        - containerPort&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;80&lt;/span&gt;
&lt;span class="na"&gt;11.          name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;http&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Line 1: The version of the object we want to describe.&lt;br&gt;
Line 7: Create a nginex container.&lt;br&gt;
Line 10: Starts the container in the port 80.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's create and inspect the Pod&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;kubectl apply &lt;span class="nt"&gt;-f&lt;/span&gt; pod-nginx.yaml
&lt;span class="nv"&gt;$ &lt;/span&gt;kubectl describe pods nginx-yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Port Forwarding
&lt;/h3&gt;

&lt;p&gt;There are many ways we can see containers from outside the cluster (we will talk about services further). By the moment, we can use port forwarding to see the nginx container running by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;kubectl port-forward &lt;span class="nt"&gt;--address&lt;/span&gt; 0.0.0.0 nginx-yaml 80:80
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if we go to the browser on &lt;code&gt;http://localhost:80&lt;/code&gt;, we should see:&lt;br&gt;
&lt;a href="https://media2.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%2Ff749yr92rt0k2ntw5nzf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ff749yr92rt0k2ntw5nzf.png" alt="home" width="800" height="337"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Scaling With A ReplicaSet
&lt;/h1&gt;

&lt;p&gt;Whenever a pod gets down, the ReplicaSet object will start up a new one making sure always a fixed number of available pods. Have a look at the following descriptor, where we have told the ReplicaSet to keep 3 pods running:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;replica-set-nginx.yaml&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;1. apiVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;apps/v1&lt;/span&gt;
&lt;span class="na"&gt;2. kind&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ReplicaSet&lt;/span&gt;
&lt;span class="na"&gt;3. metadata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;4.   name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx-replica-yaml&lt;/span&gt;
&lt;span class="na"&gt;5.   labels&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;6.     color&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;blue&lt;/span&gt;
&lt;span class="na"&gt;7.     version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;1"&lt;/span&gt;
&lt;span class="na"&gt;8. spec&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;9.   replicas&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;
&lt;span class="na"&gt;10.  selector&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;11.    matchLabels&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;12.      color&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;blue&lt;/span&gt;      
&lt;span class="na"&gt;13.  template&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;14.    metadata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;15.      labels&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;16.        color&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;blue&lt;/span&gt;
&lt;span class="na"&gt;17.    spec&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;18.      containers&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;19.        - image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx&lt;/span&gt;
&lt;span class="na"&gt;20.          name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx&lt;/span&gt;
&lt;span class="na"&gt;21.          ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;22.            - containerPort&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;80&lt;/span&gt;
&lt;span class="na"&gt;23.              name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;http&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Line 9: The number of pods to keep alive.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's create and inspect the ReplicaSet&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;kubectl apply &lt;span class="nt"&gt;-f&lt;/span&gt; replica-set-nginx.yaml
&lt;span class="nv"&gt;$ &lt;/span&gt;kubectl describe  nginx-replica.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>devops</category>
      <category>docker</category>
      <category>kubernetes</category>
      <category>programming</category>
    </item>
    <item>
      <title>Colorize zsh Mac Terminal</title>
      <dc:creator>German Valencia</dc:creator>
      <pubDate>Fri, 06 Jan 2023 04:49:38 +0000</pubDate>
      <link>https://forem.com/sttamper/colorize-zsh-mac-terminal-4he3</link>
      <guid>https://forem.com/sttamper/colorize-zsh-mac-terminal-4he3</guid>
      <description>&lt;h2&gt;
  
  
  1. Install &lt;a href="https://iterm2.com/" rel="noopener noreferrer"&gt;iTerm2&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;I recommend the use of iTerm over the native mac terminal, since it has additional cool features. However, you can use the native one.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Check if Zsh is configured in your mac
&lt;/h2&gt;

&lt;p&gt;Latest versions of macOS already come with zsh as default shell. However, you can check if zsh is configured going to the native terminal settings &lt;em&gt;&lt;strong&gt;general&lt;/strong&gt;&lt;/em&gt; tab:&lt;br&gt;
&lt;a href="https://media2.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%2Fg8hgy2xor2xalktm60wy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fg8hgy2xor2xalktm60wy.png" alt="check zsh" width="800" height="737"&gt;&lt;/a&gt;&lt;br&gt;
It your configuration does not look like the image, you need to install zsh before going to any other step in this post:&lt;/p&gt;
&lt;h3&gt;
  
  
  Installing Zsh
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Install &lt;a href="https://brew.sh/" rel="noopener noreferrer"&gt;Homebrew&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Check Homebrew installation
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;brew doctor
╰─ Your system is ready to brew.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Install Zsh
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;zsh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  3. Install &lt;a href="https://ohmyz.sh/#install" rel="noopener noreferrer"&gt;Oh My Zshell&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Oh My Zshell is a powerful framework for zsh plenty of plugins that enhances the terminal experience.&lt;/p&gt;

&lt;p&gt;zsh theme: &lt;a href="https://github.com/ohmyzsh/ohmyzsh/wiki/Themes#amuse" rel="noopener noreferrer"&gt;https://github.com/ohmyzsh/ohmyzsh/wiki/Themes#amuse&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  4. Install &lt;a href="https://github.com/romkatv/powerlevel10k#oh-my-zsh" rel="noopener noreferrer"&gt;Powerlevel10k&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;There are many ways to install Powerlevel10k, but I recommend to install it as Oh My Zshell plugin.&lt;/p&gt;

&lt;p&gt;Once you restart iTerm, Powerlevel10k will ask you to install &lt;strong&gt;&lt;a href="https://www.nerdfonts.com/" rel="noopener noreferrer"&gt;Meslo Nerd Fonts&lt;/a&gt;&lt;/strong&gt;, I highly recommend to let Powerlevel10k to install the fonts, otherwise you will have to do it by yourself&lt;br&gt;
&lt;a href="https://media2.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%2Fbvdlkjwsjo9ehqc2blcy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fbvdlkjwsjo9ehqc2blcy.png" alt="meslo font" width="800" height="668"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, follow the wizard which will guide you to customize the theme, finally you should have to get the following screen where you can either restart from the beginning or apply changes to &lt;code&gt;.zshrc&lt;/code&gt; file.&lt;br&gt;
&lt;a href="https://media2.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%2F13vt1vrpvbn4z7he9k45.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F13vt1vrpvbn4z7he9k45.png" alt="final" width="800" height="598"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  5. Cool Plugins
&lt;/h2&gt;

&lt;p&gt;Oh My Zshell is plenty of plugins, you can find the full list of plugins &lt;a href="https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins" rel="noopener noreferrer"&gt;developed by the community&lt;/a&gt;. Lets customize two basic ones:&lt;/p&gt;
&lt;h3&gt;
  
  
  zsh-syntax-highlighting
&lt;/h3&gt;

&lt;p&gt;This is a plugin to highlight commands of any CLI application you have installed in your mac.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git clone https://github.com/zsh-users/zsh-syntax-highlighting.git &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;ZSH_CUSTOM&lt;/span&gt;&lt;span class="k"&gt;:-&lt;/span&gt;&lt;span class="p"&gt;~/.oh-my-zsh/custom&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;/plugins/zsh-syntax-highlighting
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  zsh-autosuggestions
&lt;/h3&gt;

&lt;p&gt;This is a plugin suggest autocomplete commands you have typed before.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git clone https://github.com/zsh-users/zsh-autosuggestions &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;ZSH_CUSTOM&lt;/span&gt;&lt;span class="k"&gt;:-&lt;/span&gt;&lt;span class="p"&gt;~/.oh-my-zsh/custom&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;/plugins/zsh-autosuggestions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Activate Plugins
&lt;/h3&gt;

&lt;p&gt;It is just a matter of modify the following in your &lt;code&gt;.zshrc&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;plugins&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt; git &lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;by&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;plugins&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt; git zsh-syntax-highlighting zsh-autosuggestions &lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>productivity</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>linux</category>
    </item>
    <item>
      <title>Callback and Microtask Queues Explained</title>
      <dc:creator>German Valencia</dc:creator>
      <pubDate>Tue, 03 Jan 2023 06:16:55 +0000</pubDate>
      <link>https://forem.com/sttamper/callback-and-microtask-queues-explained-2pen</link>
      <guid>https://forem.com/sttamper/callback-and-microtask-queues-explained-2pen</guid>
      <description>&lt;p&gt;Javascript is a &lt;em&gt;&lt;strong&gt;non-blocking&lt;/strong&gt;&lt;/em&gt; single threaded programming language, which means that all functions we write in a &lt;code&gt;.js&lt;/code&gt; file are executed into the JS engine in one thread at runtime. &lt;/p&gt;

&lt;h2&gt;
  
  
  But, what does non-blocking mean?
&lt;/h2&gt;

&lt;p&gt;Whenever a javascript program needs to execute an async process provided by a &lt;em&gt;&lt;strong&gt;browser web api&lt;/strong&gt;&lt;/em&gt;, this can use a strategy to avoid blocking the main thread, thus getting async responses from web apis further on. Have a look at the following analogy with a calling to a help desk (&lt;a href="https://dev.to/mcwiise/brief-intro-to-reactive-streams-with-project-reactor-412o"&gt;you may want to read this too&lt;/a&gt;):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fo0v2qvfrigjnb5phagu1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fo0v2qvfrigjnb5phagu1.jpg" alt="no blocking" width="395" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In general terms, the goal is to receive responses from async processes by using &lt;strong&gt;&lt;em&gt;callback functions&lt;/em&gt;&lt;/strong&gt;, meanwhile the program continues its execution in the main thread (going to cinema :P )&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Web API's?
&lt;/h2&gt;

&lt;p&gt;Web API's are a bunch of &lt;em&gt;&lt;strong&gt;built in functions&lt;/strong&gt;&lt;/em&gt; which come with the browser and enable web sites written with javascript to access common functionalities such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Timeout API:&lt;/em&gt; To delay the execution of a function.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;DOM API:&lt;/em&gt; To manipulate parts of the DOM at runtime.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Fetch API:&lt;/em&gt; To send http requests to servers.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;LocalStorage API:&lt;/em&gt; To save information in the browser.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Console API:&lt;/em&gt;: To print out to the web console.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Location API:&lt;/em&gt; To get access to geo location services.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words, web apis are whatever we can do with the global scope &lt;code&gt;window&lt;/code&gt;:&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="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&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;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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)},&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;window&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="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;www.google.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&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;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;r&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tales&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tales&lt;/span&gt;&lt;span class="dl"&gt;'&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="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Callback Queue
&lt;/h2&gt;

&lt;p&gt;Whenever we use a web API from our javascript code, the program needs to register a &lt;em&gt;&lt;strong&gt;callback function&lt;/strong&gt;&lt;/em&gt; which will be executed once the web API ends with its internal stuff. &lt;/p&gt;

&lt;p&gt;Now, callback functions don't get the call stack directly, so web API's push them to an alternative channel known as &lt;em&gt;&lt;strong&gt;the callback queue&lt;/strong&gt;&lt;/em&gt; waiting for the call stack to be ready to stack them for the program to execute them. Have a look at the following snippet:&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="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&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;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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;end&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the images below we can see that as soon as the &lt;code&gt;setTimeout()&lt;/code&gt; function gets into the call stack its callback is registered, and when the timeout has elapsed (1 second) the callback function gets into the callback queue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fxxj0p57cannf3w120fws.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxxj0p57cannf3w120fws.jpg" alt="callback queue" width="667" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Event Loop
&lt;/h2&gt;

&lt;p&gt;The event loop is an endless built in process provided by the browser which checks the callback queue looking for functions to execute once the call stack is ready.&lt;/p&gt;

&lt;p&gt;And &lt;em&gt;&lt;strong&gt;When is the call stack ready?&lt;/strong&gt;&lt;/em&gt; it is ready when the Global Execution Context has been removed, then the event loop takes the next function from the callback queue and pushes it to the call stack. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1d27m06a16m1921kiezi.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1d27m06a16m1921kiezi.jpg" alt="event loop" width="546" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Microtask Queue
&lt;/h2&gt;

&lt;p&gt;There are some web API's which use &lt;em&gt;&lt;strong&gt;promises&lt;/strong&gt;&lt;/em&gt; to provide async computation, this is the case of &lt;strong&gt;&lt;em&gt;fetch web API.&lt;/em&gt;&lt;/strong&gt; Have a look at the following snippet:&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="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;www.google.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&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;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;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;p&gt;Callback functions of web API's based on promises don't get the callback queue after the promise is resolved but a different one called &lt;em&gt;&lt;strong&gt;The Microtask Queue&lt;/strong&gt;&lt;/em&gt;. Even though this queue is also checked by the event loop looking for functions to push to the call stack, this has a higher priority than the callback queue, it means that all callback functions in the &lt;strong&gt;&lt;em&gt;Microtask Queue&lt;/em&gt;&lt;/strong&gt; will be executed first than the next function available in the &lt;strong&gt;&lt;em&gt;Callback Queue.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>emptystring</category>
    </item>
    <item>
      <title>Javascript Engine and Call Stack explained</title>
      <dc:creator>German Valencia</dc:creator>
      <pubDate>Mon, 02 Jan 2023 19:45:15 +0000</pubDate>
      <link>https://forem.com/sttamper/javascript-engine-and-call-stack-explained-20l0</link>
      <guid>https://forem.com/sttamper/javascript-engine-and-call-stack-explained-20l0</guid>
      <description>&lt;p&gt;You can use &lt;a href="https://playcode.io/" rel="noopener noreferrer"&gt;playground.js&lt;/a&gt; to execute the code snippets.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Javascript Engine
&lt;/h2&gt;

&lt;p&gt;The main responsibility of any web browser is to serve static content (HTML and CSS) from the WWW, it means that a browser is just an interpreter of markup language and for being able to execute Javascript code, all browsers need to use an internal component called &lt;em&gt;&lt;strong&gt;Javascript Engine&lt;/strong&gt;&lt;/em&gt; which is developed by the browser's vendor.&lt;/p&gt;

&lt;p&gt;Here is a list of JavaScript Engines for major Internet browsers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;V8:&lt;/strong&gt;&lt;/em&gt; JavaScript Engine developed by Google for Chrome&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;SpiderMonkey:&lt;/strong&gt;&lt;/em&gt; – The JavaScript Engine used by Mozilla Firefox&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;JavaScriptCore:&lt;/em&gt;&lt;/strong&gt; – Developed by Apple for Safari&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Rhino:&lt;/strong&gt;&lt;/em&gt; – Managed by Mozilla Foundation for Firefox&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Chakra:&lt;/strong&gt;&lt;/em&gt; – A JavaScript Engine for Microsoft Edge&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Call Stack
&lt;/h2&gt;

&lt;p&gt;Javascript is a non-blocking single threaded programming language, which means that all functions we write in a program (.js file) are executed by the engine in one thread at runtime, where the engine keeps the order the functions are executed by using a LIFO (Last in First Out) data structure, and this data structure is what we call as &lt;em&gt;&lt;strong&gt;the call stack.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Few2e366hmq6tjnvvr7g1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Few2e366hmq6tjnvvr7g1.jpg" alt="js-engine" width="303" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Along with the call stack the engine comes with a &lt;strong&gt;&lt;em&gt;memory heap&lt;/em&gt;&lt;/strong&gt;, this is just a storage of variables, objects, constants and function definitions, being  functions the ones which will get into the stack during the program execution.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Global Execution Context GCE
&lt;/h3&gt;

&lt;p&gt;Just for academic purposes, think about &lt;strong&gt;&lt;em&gt;the global execution context&lt;/em&gt;&lt;/strong&gt; like a main function which wraps all code we want to execute in a single &lt;code&gt;.js&lt;/code&gt; file. We cannot control the GCE, all we need to know is that this is always the first function which gets into the stack once the engine reads the code to run.&lt;/p&gt;

&lt;p&gt;With all basic concepts in place, let's analyze the execution flow of the following program, keep in mind that the execution order does not rely on the order the functions are defined but the order they are invoked:&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;a&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="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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;b&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="mi"&gt;5&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;B&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;    &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
&lt;span class="mi"&gt;9&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nf"&gt;b&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nf"&gt;end&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The engine reads the &lt;code&gt;.js&lt;/code&gt; file to run and then the GEC gets into the stack.&lt;/li&gt;
&lt;li&gt;(Line 11) &lt;code&gt;function b(callback)&lt;/code&gt; gets into the stack.&lt;/li&gt;
&lt;li&gt;(Line 5) &lt;code&gt;console.log&lt;/code&gt; function gets into the stack and prints out &lt;strong&gt;B&lt;/strong&gt; in the web console.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log&lt;/code&gt; function is removed from the call stack after it is executed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F0r072ehk994cfb646yd9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0r072ehk994cfb646yd9.jpg" alt="call stack one" width="800" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;(line 6) &lt;code&gt;function a()&lt;/code&gt; gets into the stack. Notice that this is the same callback function.&lt;/li&gt;
&lt;li&gt;(line 2) &lt;code&gt;console.log&lt;/code&gt; function gets into the stack and prints out &lt;strong&gt;A&lt;/strong&gt; in the web console.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log&lt;/code&gt; function is removed from the stack after it is executed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;function a()&lt;/code&gt; is removed from the call stack since its execution has ended.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;function b(callback)&lt;/code&gt; is removed from the call stack since its execution has ended.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fmhwtk26cv6rcei6q1gma.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmhwtk26cv6rcei6q1gma.jpg" alt="call stack two" width="800" height="213"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;(line 12) &lt;code&gt;function end()&lt;/code&gt; gets into the stack.&lt;/li&gt;
&lt;li&gt;(Line 5) &lt;code&gt;console.log&lt;/code&gt; function gets into the stack and prints out &lt;strong&gt;End&lt;/strong&gt; in the web console.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log&lt;/code&gt; function is removed from the stack after it is executed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;function end()&lt;/code&gt; function is removed from the stack since its execution has ended.&lt;/li&gt;
&lt;li&gt;The global execution context is removed from the stack and the program ends.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fo795rnlmo3b714tumdn1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fo795rnlmo3b714tumdn1.jpg" alt="call stack three" width="800" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Final Outcome&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;B&lt;br&gt;
A&lt;br&gt;
End&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>emptystring</category>
    </item>
    <item>
      <title>Brief Intro to Reactive Streams with Project Reactor</title>
      <dc:creator>German Valencia</dc:creator>
      <pubDate>Mon, 02 Jan 2023 05:20:29 +0000</pubDate>
      <link>https://forem.com/sttamper/brief-intro-to-reactive-streams-with-project-reactor-412o</link>
      <guid>https://forem.com/sttamper/brief-intro-to-reactive-streams-with-project-reactor-412o</guid>
      <description>&lt;p&gt;Source code of the examples &lt;a href="https://github.com/mcwiise/dev.to/tree/main/java-reactive-reactor/src/main/java/org/mcwiise/cap"&gt;on Github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Traditional client–server communication is request-based: the server allocates a thread to handle each request, and the client waits until the server completes the execution, before getting either a &lt;em&gt;success&lt;/em&gt; or &lt;em&gt;failure&lt;/em&gt; response. This model is known as &lt;strong&gt;synchronous, blocking communication&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Regardless of the scalability strategy used by the server (horizontal or vertical), this synchronous blocking approach can become a performance bottleneck. To address this, we can use &lt;strong&gt;reactive programming&lt;/strong&gt;, which enables more efficient thread and resource management.&lt;/p&gt;

&lt;p&gt;To illustrate this, we’ll explore four communication scenarios using a call center as a study-case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Synchronous and Blocking
&lt;/h2&gt;

&lt;p&gt;As we saw above this is the traditional way, where the client will be blocked until getting a response from the server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fetap8t9cqg0te20t8rmi.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fetap8t9cqg0te20t8rmi.jpg" alt="sync-blocking" width="307" height="179"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Asynchronous
&lt;/h2&gt;

&lt;p&gt;The client will delegate the communication to the server to another thread, thus the client is released to execute other tasks while the server responses.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fiah34wsl9x7rzttaow2h.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fiah34wsl9x7rzttaow2h.jpg" alt="async" width="363" height="299"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Non-Blocking
&lt;/h2&gt;

&lt;p&gt;The client does not delegate the communication to a new thread, but registers a &lt;strong&gt;&lt;em&gt;callback function&lt;/em&gt;&lt;/strong&gt; where will receive the response after the server processes the request. This scenario also releases the client (main thread) to perform other tasks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fncmrvbbm2rgl3z88f9cj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fncmrvbbm2rgl3z88f9cj.jpg" alt="non blocking" width="395" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Non-Blocking Async
&lt;/h2&gt;

&lt;p&gt;the client delegates the communication to a new thread, so the new thread registers the &lt;em&gt;&lt;strong&gt;callback function&lt;/strong&gt;&lt;/em&gt; where will receive the response once the server processes the request. This scenario also releases the client (main thread) to perform other tasks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ft3pna61t1y5g8n26iifp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ft3pna61t1y5g8n26iifp.jpg" alt="non blocking async" width="594" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Publisher Subscriber Pattern
&lt;/h2&gt;

&lt;p&gt;Reactive programming relies on the Publisher and Subscriber pattern, where the communication between components that emit messages (publishers) and the ones which consume them (subscribers) relies on an external component called broker. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fmd3a3r3pfpqih9ob57at.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmd3a3r3pfpqih9ob57at.jpg" alt="pub sub pattern" width="578" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Publishers and subscribers are agnostic each other. More about this pattern in further posts about Apache Kafka.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Reactive Streams API
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.reactive-streams.org/" rel="noopener noreferrer"&gt;reactive streams API&lt;/a&gt; provides the specification for &lt;strong&gt;&lt;em&gt;non-blocking async&lt;/em&gt;&lt;/strong&gt; streams processing with back pressure mechanism, and &lt;a href="https://projectreactor.io/" rel="noopener noreferrer"&gt;Project Reactor&lt;/a&gt; is an implementation written in java.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Back Pressure?
&lt;/h3&gt;

&lt;p&gt;Having a look at the pub-sub pattern diagram, &lt;em&gt;&lt;strong&gt;back pressure&lt;/strong&gt;&lt;/em&gt; is a strategy the reactive streams provide to control the number of messages (items) a subscriber is able to consume from a publisher.&lt;/p&gt;

&lt;p&gt;Let's review the 4th interfaces provided by the API, and keep in mind the difference between these 2 concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;item:&lt;/strong&gt;&lt;/em&gt; this is the element or message sent by a publisher and consumed by a subscriber. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;signal:&lt;/em&gt;&lt;/strong&gt; a flag emitted by the publisher after an item or a set of items are sent.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Publisher Interface
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Responsible for emitting items along with a &lt;em&gt;&lt;strong&gt;success&lt;/strong&gt;&lt;/em&gt; or &lt;em&gt;&lt;strong&gt;failure&lt;/strong&gt;&lt;/em&gt; signals.&lt;/li&gt;
&lt;li&gt;It also provides a method for the subscribers to register a &lt;em&gt;&lt;strong&gt;subscription&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Subscription Interface
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This is a &lt;em&gt;&lt;strong&gt;channel&lt;/strong&gt;&lt;/em&gt; where the subscriber is able to request more items to the publisher (back pressure mechanism).&lt;/li&gt;
&lt;li&gt;The subscriber can also &lt;strong&gt;&lt;em&gt;cancel the subscription&lt;/em&gt;&lt;/strong&gt; using the same channel.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Subscriber Interface
&lt;/h3&gt;

&lt;p&gt;A subscriber &lt;em&gt;&lt;strong&gt;reacts to the items and signals&lt;/strong&gt;&lt;/em&gt; emitted by the publisher by the implementation of the following methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;onComplete()&lt;/em&gt;&lt;/strong&gt; The publisher notifies the subscriber that there are no more items to emit, then the publisher is closed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;onError(Throwable)&lt;/em&gt;&lt;/strong&gt; The publisher notifies the subscriber that there was an error when emitting the item, then the publisher is closed.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;onNext()&lt;/strong&gt;&lt;/em&gt; The publisher notifies the subscriber that a next item was just emitted.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;onSubscribe()&lt;/strong&gt;&lt;/em&gt; This is the initial state, where the publisher notifies the subscriber that the subscription was accepted.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Processor Interface
&lt;/h3&gt;

&lt;p&gt;This is an intermediary stage, a processor can consume an item and emit a new one.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://projectreactor.io/" rel="noopener noreferrer"&gt;The Project Reactor&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;As implementation of reactive streams API, reactor is based on two types of publishers: &lt;code&gt;Mono&lt;/code&gt; and &lt;code&gt;Flux&lt;/code&gt;. Be aware that reactive streams are lazy evaluated, it means that nothing happens until a subscription is created.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mono
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;Mono&lt;/code&gt; is a publisher able to emit from &lt;strong&gt;&lt;em&gt;zero to 1 items.&lt;/em&gt;&lt;/strong&gt; followed by an &lt;strong&gt;&lt;em&gt;onComplete()&lt;/em&gt;&lt;/strong&gt; or &lt;em&gt;&lt;strong&gt;onError()&lt;/strong&gt;&lt;/em&gt; signals. Let's explore some basic functions to get started.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Emit And Subscribe&lt;/strong&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="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;emitAndSubscribe&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;mono&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mono&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;just&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;   &lt;span class="n"&gt;mono&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;subscribe&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;     &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;     &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;()),&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;     &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Completed!"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;   &lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;8&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;ul&gt;
&lt;li&gt;
&lt;em&gt;line 2:&lt;/em&gt; We create mono publisher which emits 1 item.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;line 3:&lt;/em&gt; We create a subscription which receives 3 functions: 2 Consumer and 1 Runnable&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;line 4:&lt;/em&gt; the consumer that accepts the item just emitted.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;line 5:&lt;/em&gt; the consumer that accepts an exception in case it is thrown by the publisher.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;line 6:&lt;/em&gt; the runnable which reacts to &lt;em&gt;&lt;strong&gt;onComplete()&lt;/strong&gt;&lt;/em&gt; signal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Outcome:&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A&lt;br&gt;
Completed!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Flux
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;Flux&lt;/code&gt; is a publisher able to emit from &lt;strong&gt;&lt;em&gt;zero to many&lt;/em&gt;&lt;/strong&gt; items, followed by an &lt;strong&gt;&lt;em&gt;onComplete()&lt;/em&gt;&lt;/strong&gt; or &lt;em&gt;&lt;strong&gt;onError()&lt;/strong&gt;&lt;/em&gt; signals. Let's explore some basic functions to get started:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Emit And Subscribe&lt;/strong&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="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;emitAndSubscribe&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;flux&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Flux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fromArray&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]{&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="o"&gt;});&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;   &lt;span class="n"&gt;flux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;subscribe&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;     &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;     &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;()),&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;     &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Completed!"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;   &lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;8&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;ul&gt;
&lt;li&gt;
&lt;em&gt;line 2:&lt;/em&gt; We create flux publisher which emits 3 item.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;line 3:&lt;/em&gt; We create a subscription which receives 3 functions: 2 Consumer and 1 Runnable&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;line 4:&lt;/em&gt; the consumer that accepts the item just emitted.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;line 5:&lt;/em&gt; the consumer that accepts an exception in case it is thrown by the publisher.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;line 6:&lt;/em&gt; the runnable which reacts to &lt;em&gt;&lt;strong&gt;onComplete()&lt;/strong&gt;&lt;/em&gt; signal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Outcome:&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A&lt;br&gt;
B&lt;br&gt;
C&lt;br&gt;
Completed!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://javadoc.io/doc/io.projectreactor/reactor-core/latest/index.html" rel="noopener noreferrer"&gt;reactor java doc&lt;/a&gt; is plenty of methods to work with Monos and Fluxes, we have just covered some basic ones to understand the concepts of publishing and subscribe to get  started.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>architecture</category>
      <category>reactivestreams</category>
    </item>
    <item>
      <title>Binary Search Tree Vol. 2</title>
      <dc:creator>German Valencia</dc:creator>
      <pubDate>Sun, 01 Jan 2023 20:48:05 +0000</pubDate>
      <link>https://forem.com/sttamper/binary-search-tree-vol-2-4gfk</link>
      <guid>https://forem.com/sttamper/binary-search-tree-vol-2-4gfk</guid>
      <description>&lt;p&gt;Source code of the examples &lt;a href="https://github.com/mcwiise/dev.to/blob/main/binary-tree-ds/src/btree/BinarySearchTree.java"&gt;on Github&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Sorting a Binary Search Tree (ASC and DESC)
&lt;/h2&gt;

&lt;p&gt;A binary tree is an &lt;em&gt;&lt;strong&gt;unordered&lt;/strong&gt;&lt;/em&gt; data structure, which implies that elements (nodes) are not associated to a consecutive index when they are inserted (unlike lists). However a binary tree can be &lt;em&gt;&lt;strong&gt;sorted&lt;/strong&gt;&lt;/em&gt;, it means that its elements can follow a logic sorting criteria.&lt;/p&gt;

&lt;p&gt;Then, lets explore a pair of algorithms to sort a BST ascending and descending:&lt;/p&gt;

&lt;h3&gt;
  
  
  ASCending Sorting
&lt;/h3&gt;

&lt;p&gt;Given the nature of a BST where nodes whose values are less than the root node go to the left, for ascending sorting it is just a matter to print out nodes starting by the leftmost node in the tree onwards. In other words we need to use &lt;a href="https://dev.to/mcwiise/binary-tree-data-structure-253b"&gt;&lt;strong&gt;&lt;em&gt;the inorder traversal algorithm.&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the parent node is &lt;code&gt;null&lt;/code&gt; then just break the recursion.&lt;/li&gt;
&lt;li&gt;Look for the &lt;strong&gt;&lt;em&gt;leftmost&lt;/em&gt;&lt;/strong&gt; node of the tree in the &lt;em&gt;&lt;strong&gt;parent left subtree&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;You have found the node, then just print out its value.&lt;/li&gt;
&lt;li&gt;Look for the &lt;em&gt;&lt;strong&gt;leftmost&lt;/strong&gt;&lt;/em&gt; node of the BST in the &lt;em&gt;&lt;strong&gt;parent right subtree&lt;/strong&gt;&lt;/em&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printAscending&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printAscending&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printAscending&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;);&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;h3&gt;
  
  
  DESCending Sorting
&lt;/h3&gt;

&lt;p&gt;Now, given that nodes whose values are greater than the root node in a BST go to the right, for descending sorting it is just a matter to print out nodes starting by the rightmost node in the tree onwards. In other words we need to use &lt;a href="https://dev.to/mcwiise/binary-tree-data-structure-253b"&gt;&lt;strong&gt;&lt;em&gt;the inorder traversal algorithm.&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the parent node is &lt;code&gt;null&lt;/code&gt; then just break the recursion.&lt;/li&gt;
&lt;li&gt;Look for the &lt;strong&gt;&lt;em&gt;rightmost&lt;/em&gt;&lt;/strong&gt; node of the tree in the &lt;em&gt;&lt;strong&gt;parent right subtree&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;You have found the node, then just print out its value.&lt;/li&gt;
&lt;li&gt;Look for the &lt;strong&gt;&lt;em&gt;rightmost&lt;/em&gt;&lt;/strong&gt; node of the BST in the &lt;em&gt;&lt;strong&gt;parent left subtree&lt;/strong&gt;&lt;/em&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printDescending&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printDescending&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printDescending&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;);&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;h2&gt;
  
  
  Lowest Common Ancestor
&lt;/h2&gt;

&lt;p&gt;By definition the lowest common ancestor of 2 nodes &lt;em&gt;&lt;strong&gt;x&lt;/strong&gt;&lt;/em&gt; and &lt;strong&gt;&lt;em&gt;y&lt;/em&gt;&lt;/strong&gt; is the lowest node which has both &lt;em&gt;&lt;strong&gt;as descendants&lt;/strong&gt;&lt;/em&gt;, be aware that a node itself is considered its own descendant.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F6s671g5miwbruxwd6zep.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6s671g5miwbruxwd6zep.jpg" alt="Lowest common ancestor" width="379" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the &lt;em&gt;&lt;strong&gt;parent value is less&lt;/strong&gt;&lt;/em&gt; than x value and y value, it means that we have to look for &lt;em&gt;&lt;strong&gt;the LCA in the parent right subtree&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;If the &lt;em&gt;&lt;strong&gt;parent value is greater&lt;/strong&gt;&lt;/em&gt; than x value and y value, it means that we have to look for &lt;em&gt;&lt;strong&gt;the LCA in the parent left subtree&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Otherwise we have found the LCA then just returns it.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="nf"&gt;lca&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;lca&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;lca&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;parent&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;



</description>
      <category>watercooler</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Binary Search Tree Vol. 1</title>
      <dc:creator>German Valencia</dc:creator>
      <pubDate>Sat, 31 Dec 2022 19:15:33 +0000</pubDate>
      <link>https://forem.com/sttamper/binary-search-tree-vol-1-2c5m</link>
      <guid>https://forem.com/sttamper/binary-search-tree-vol-1-2c5m</guid>
      <description>&lt;p&gt;Source code of the examples &lt;a href="https://github.com/mcwiise/dev.to/blob/main/binary-tree-ds/src/btree/BinarySearchTree.java"&gt;on Github&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Binary Search Tree (BST)?
&lt;/h2&gt;

&lt;p&gt;BST stands for Binary Search Tree; this is a special type of a tree which must be compliant with the following conditions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;&lt;strong&gt;left subtree&lt;/strong&gt;&lt;/em&gt; of the root node must contain values which &lt;em&gt;&lt;strong&gt;are less than&lt;/strong&gt;&lt;/em&gt; the root node value.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;&lt;strong&gt;right subtree&lt;/strong&gt;&lt;/em&gt; of the root node must contain values which &lt;em&gt;&lt;strong&gt;are greater than&lt;/strong&gt;&lt;/em&gt; the root node value.&lt;/li&gt;
&lt;li&gt;In lights of the above, a BST does not contain duplicate values.&lt;/li&gt;
&lt;li&gt;Every left or right subtree of any node must also be a BST.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F7s9s17a6c6vdg9pjkcec.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7s9s17a6c6vdg9pjkcec.jpg" alt="simple bst" width="587" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Searching for a key in a BST
&lt;/h2&gt;

&lt;p&gt;We may guess that searching keys in a binary tree it is just a matter of using one of the traversing algorithms we've seen on this &lt;a href="(https://dev.to/mcwiise/binary-tree-data-structure-253b)"&gt;post&lt;/a&gt;, but given the fact that a BST is &lt;em&gt;&lt;strong&gt;a sorted data structure&lt;/strong&gt;&lt;/em&gt;, traversing all the nodes searching for a key could represent an unnecessary overhead, thus a better algorithm should look like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check if the key to search equals &lt;code&gt;null&lt;/code&gt; or  the parent node value, then just returns the node.&lt;/li&gt;
&lt;li&gt;Else, compare the key with the parent node value.&lt;/li&gt;
&lt;li&gt;So, if the key to search is less than the parent node value, then look for the key in the &lt;em&gt;&lt;strong&gt;left subtree&lt;/strong&gt;&lt;/em&gt;, otherwise look for the key in the &lt;strong&gt;&lt;em&gt;right subtree.&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&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="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&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;h2&gt;
  
  
  Inserting a new key in a BST
&lt;/h2&gt;

&lt;p&gt;As we already understood that keys less than the root node value go to the left subtree and the greater ones to the right, implementing an algorithm to insert keys in a BST should look like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check if the parent node is null, then create a new node with the key and just returns it.&lt;/li&gt;
&lt;li&gt;Else, compare the key with the parent value.&lt;/li&gt;
&lt;li&gt;So, if the key to insert is less than the parent value, then insert the key in the left subtree, otherwise insert the key in the right subtree.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;bts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BinarySearchTree&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;    &lt;span class="n"&gt;bts&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bts&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bts&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;    &lt;span class="n"&gt;bts&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bts&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;    &lt;span class="n"&gt;bts&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bts&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;   &lt;span class="o"&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="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;     &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;   &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;     &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="mi"&gt;16&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;Have a look at the &lt;code&gt;main&lt;/code&gt; method (line 3) where we start the recursive function to insert keys in the tree.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deleting a key from a BST
&lt;/h2&gt;

&lt;p&gt;Deleting a key means deleting the node itself. Whenever we need to delete a node from a BST, we have to always make sure that &lt;em&gt;&lt;strong&gt;none of the 4th conditions of a BST break&lt;/strong&gt;&lt;/em&gt; after deletion, specially the one which says that keys less than the parent value go to the left and the greater ones go the the right, thus we need to analyze the following 3 scenarios to implement a &lt;em&gt;free bugs&lt;/em&gt; algorithm:&lt;/p&gt;

&lt;h3&gt;
  
  
  Node to be deleted is a &lt;strong&gt;&lt;em&gt;leaf&lt;/em&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is the simplest scenario where the parent node must address to &lt;code&gt;null&lt;/code&gt; after the child is deleted.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fdkeee4eta32ow9tl63k6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fdkeee4eta32ow9tl63k6.jpg" alt="delete a leave from a BST" width="346" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Node to be deleted has 1 child
&lt;/h3&gt;

&lt;p&gt;In this case the parent node of the child to delete, must now address to the child's child.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fs2lrjti2p0d3t3vqsy8w.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fs2lrjti2p0d3t3vqsy8w.jpg" alt="delete a node with 1 child from BST" width="499" height="243"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Node to be deleted has 2 children
&lt;/h3&gt;

&lt;p&gt;This is the complex case but it can be solved easily, we just need to replace the node to be deleted by its &lt;a href="https://dev.to/mcwiise/binary-tree-data-structure-vol-2-5a52"&gt;inorder predecessor&lt;/a&gt;, and why by the inorder predecessor? because this will be &lt;em&gt;&lt;strong&gt;the largest node of the left subtree&lt;/strong&gt;&lt;/em&gt; after the deletion happens.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fxw0je58t5ah2lcvbn5qk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxw0je58t5ah2lcvbn5qk.jpg" alt="delete a node with 2 child from BST" width="659" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, the algorithm:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check the parent, if it is &lt;code&gt;null&lt;/code&gt; then return it (this is a leaf).&lt;/li&gt;
&lt;li&gt;Else, if the key to delete is less than parent value then delete the key from the parent left subtree&lt;/li&gt;
&lt;li&gt;Else, if the key to delete is greater than parent value then delete the key from the parent right subtree.&lt;/li&gt;
&lt;li&gt;Finally, if the key equals the parent value, it means we have found the node to delete.&lt;/li&gt;
&lt;li&gt;Find and replace the parent value with its inorder predecessor.&lt;/li&gt;
&lt;li&gt;Then, delete the predecessor from the parent left subtree.&lt;/li&gt;
&lt;li&gt;Finally return the parent which will represent the new state of the entire tree after deletion.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;    &lt;span class="o"&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="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;      &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;    &lt;span class="o"&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="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;      &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;      &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;predecessor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;inOrderPredecessor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;     &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;predecessor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;     &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="mi"&gt;14&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;h3&gt;
  
  
  Finding the Inorder predecessor of a Node
&lt;/h3&gt;

&lt;p&gt;Lets explore line 9 of the above code snippet where we need to find the Inorder predecessor of the node to delete:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The algorithm needs to find the rightmost node of the parent left subtree, since this is the place where we can find &lt;em&gt;&lt;strong&gt;the largest node of the left subtree.&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="nf"&gt;inOrderPredecessor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;tempNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tempNode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="n"&gt;tempNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tempNode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;tempNode&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;



</description>
      <category>java</category>
      <category>beginners</category>
      <category>programming</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Binary Tree Data Structure Vol 2.</title>
      <dc:creator>German Valencia</dc:creator>
      <pubDate>Sat, 31 Dec 2022 16:16:36 +0000</pubDate>
      <link>https://forem.com/sttamper/binary-tree-data-structure-vol-2-5a52</link>
      <guid>https://forem.com/sttamper/binary-tree-data-structure-vol-2-5a52</guid>
      <description>&lt;p&gt;Let's review some problems we can solve by using the traversal algorithms we learnt in previous posts.&lt;/p&gt;

&lt;p&gt;Source code of the examples &lt;a href="https://github.com/mcwiise/dev.to/blob/main/binary-tree-ds/src/btree/BinaryTree.java"&gt;on Github&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Inorder Predecesor
&lt;/h2&gt;

&lt;p&gt;We can use the traversing algorithms to perform some operations in a binary tree, for example we may want to replace every node by its &lt;em&gt;&lt;strong&gt;inorder predecessor&lt;/strong&gt;&lt;/em&gt;, being this the previous node when traversing the binary tree (inorder).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F9ip426hduk1hp6cl3f09.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9ip426hduk1hp6cl3f09.jpg" alt="swap nodes inorder" width="482" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A recursive algorithm should look like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define an scoped variable to keep track the predecessors while traversing the binary tree.&lt;/li&gt;
&lt;li&gt;If the &lt;em&gt;&lt;strong&gt;parent node&lt;/strong&gt;&lt;/em&gt; is &lt;code&gt;null&lt;/code&gt;, then just return the node.&lt;/li&gt;
&lt;li&gt;Otherwise, perform predecessor swapping to the parent left subtree, swap the values, and then perform predecessor swapping to the parent right subtree.
&lt;/li&gt;
&lt;/ol&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;predecessor&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="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;inOrderPredecessorSwap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;inOrderPredecessorSwap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;swap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;inOrderPredecessorSwap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;swap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;var&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;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;predecessor&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;predecessor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&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;blockquote&gt;
&lt;p&gt;Inorder traversal outcome:  6,5,8,7&lt;br&gt;
After swapping outcome:    -1,6,5,8&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Highest Level of a Binary Tree
&lt;/h2&gt;

&lt;p&gt;The highest level of a tree is represented by the number of edges between the root node and the deepest leaf, then we have:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check if the parent node is &lt;code&gt;null&lt;/code&gt;, then return -1.&lt;/li&gt;
&lt;li&gt;Now, calculates the highest level of the &lt;em&gt;&lt;strong&gt;left subtree&lt;/strong&gt;&lt;/em&gt; and &lt;strong&gt;&lt;em&gt;the right subtree.&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;If the highest level of the left subtree is greater than the right subtree, then return the left one plus 1, otherwise return the right one plus 1.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;highestLevel&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&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="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;hLeft&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;highestLevel&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;hRight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;highestLevel&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hLeft&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;hRight&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hLeft&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="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hRight&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="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Copy a Binary Tree
&lt;/h2&gt;

&lt;p&gt;Another application of &lt;em&gt;&lt;strong&gt;the preorder traversal algorithm&lt;/strong&gt;&lt;/em&gt; is when we need to create a new binary tree by copying all nodes from another one.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check if the parent is null, then just returns it.&lt;/li&gt;
&lt;li&gt;Else, create a new instance of a node from the parent node.&lt;/li&gt;
&lt;li&gt;Now, copy the parent left subtree to the recently created node, and do the same with the parent right subtree.&lt;/li&gt;
&lt;li&gt;Just return the node created.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;newParent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;newParent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;newParent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;newParent&lt;/span&gt;&lt;span class="o"&gt;;&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;h2&gt;
  
  
  Deleting a Binary Tree
&lt;/h2&gt;

&lt;p&gt;We can use the &lt;strong&gt;&lt;em&gt;Postorder traversal algorithm&lt;/em&gt;&lt;/strong&gt; to delete all nodes of a binary tree.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check if the parent is &lt;code&gt;null&lt;/code&gt;, then just returns it.&lt;/li&gt;
&lt;li&gt;Else, delete the parent left subtree and the parent right subtree.&lt;/li&gt;
&lt;li&gt;Then, just returns &lt;code&gt;null.&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;There are many other operations we need to perform when working with binary trees, but most of them can be implemented just by applying a variation of the 3 traversal algorithms we have reviewed in this series of posts.&lt;/p&gt;

</description>
      <category>community</category>
    </item>
    <item>
      <title>Binary Tree Data Structure Vol. 1</title>
      <dc:creator>German Valencia</dc:creator>
      <pubDate>Fri, 30 Dec 2022 16:57:52 +0000</pubDate>
      <link>https://forem.com/sttamper/binary-tree-data-structure-253b</link>
      <guid>https://forem.com/sttamper/binary-tree-data-structure-253b</guid>
      <description>&lt;p&gt;Source code of the examples &lt;a href="https://github.com/mcwiise/dev.to/blob/main/binary-tree-ds/src/btree/BinaryTree.java"&gt;on Github&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Tree?
&lt;/h2&gt;

&lt;p&gt;A tree is a non-linear data structure composed of nodes and edges without cycles. It means that every node in a tree has one and only one edge connecting two nodes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Node&lt;/strong&gt;: it is an element that contains a particular information plus the addresses to downward nodes also called children.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge&lt;/strong&gt;: it just connects 2 nodes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fg3lfzm8o4uve0w57m23m.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fg3lfzm8o4uve0w57m23m.jpg" alt="Simple Tree" width="458" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Binary Tree?
&lt;/h2&gt;

&lt;p&gt;A binary tree is the simplest type of a tree, where nodes can have from zero to 2 children at most.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Concepts
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Root:&lt;/strong&gt; this is the node at the top of the tree.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Path:&lt;/strong&gt; refers to a sequence of nodes along with the edges.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parent:&lt;/strong&gt; every node except for the root node has one edge connecting to an upward node, this one is called parent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Child:&lt;/strong&gt; a node below another node connected by one edge is referred as child.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leaf:&lt;/strong&gt; a node which does not have any children.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Subtree:&lt;/strong&gt; all descendants of a node below the root node.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Level:&lt;/strong&gt; number of edges between the root node and the deepest leaf.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Value:&lt;/strong&gt; the content of the node also referred as &lt;em&gt;info.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fcukcrqonhv4kjszz5out.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fcukcrqonhv4kjszz5out.jpg" alt="Key Concepts Binary Tree" width="458" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A Brief Intro to Recursiveness
&lt;/h2&gt;

&lt;p&gt;Traversing a binary tree is usually a &lt;em&gt;&lt;strong&gt;recursive process&lt;/strong&gt;&lt;/em&gt;, and I say &lt;em&gt;usually&lt;/em&gt; because of there are other alternatives to implement traversing algorithms with &lt;em&gt;stacks&lt;/em&gt; or &lt;em&gt;queues&lt;/em&gt;. However, I consider is better to analyze binary trees from a recursive perspective for academic purposes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recursive Function
&lt;/h3&gt;

&lt;p&gt;A recursive function is no more than a function which calls itself until a condition is meet. Have a look at the following example:&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="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Tales&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sillySum&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="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sillySum&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;     &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sillySum&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="mi"&gt;13&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;Where we can identify 2 key parts of a recursive function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;halting condition:&lt;/strong&gt;&lt;/em&gt; the condition that stops the recursive process (line 7), this is also called the &lt;strong&gt;&lt;em&gt;base case.&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;recursive statement:&lt;/strong&gt;&lt;/em&gt; when the function is called itself (line 11).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Traversing a Binary Tree
&lt;/h2&gt;

&lt;p&gt;Traversing means to pass through all nodes of a binary tree just one time in a specific order, therefore we can say that every node is a potential &lt;strong&gt;&lt;em&gt;subtree&lt;/em&gt;&lt;/strong&gt; to be traversed, even if it is a &lt;strong&gt;&lt;em&gt;leaf&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now, lets explore the 3 flavors we have to traverse the binary tree of the chart, and refer to &lt;em&gt;visit&lt;/em&gt; as the action of printing out the &lt;strong&gt;&lt;em&gt;value&lt;/em&gt;&lt;/strong&gt; of a node, please keep in mind the 2 key concepts of a recursive function, so you can identify where the recursiveness happens:&lt;/p&gt;

&lt;h3&gt;
  
  
  Inorder traversal
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Traverse all nodes of the &lt;em&gt;left subtree&lt;/em&gt; (inorder).&lt;/li&gt;
&lt;li&gt;Visit the node itself.&lt;/li&gt;
&lt;li&gt;Traverse all nodes of the &lt;em&gt;right subtree&lt;/em&gt; (inorder).
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;inOrderTraversal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;inOrderTraversal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;inOrderTraversal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;);&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;blockquote&gt;
&lt;p&gt;Outcome: [4,2,6,5,7,8,1,3]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Preorder traversal
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Visit the node itself.&lt;/li&gt;
&lt;li&gt;Traverse all nodes of the &lt;em&gt;left subtree&lt;/em&gt; (preorder).&lt;/li&gt;
&lt;li&gt;Traverse all nodes of the &lt;em&gt;right subtree&lt;/em&gt; (preorder).
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;preOrderTraversal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;preOrderTraversal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;preOrderTraversal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;);&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;blockquote&gt;
&lt;p&gt;Outcome: [1,2,4,5,6,7,8,3]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Postorder traversal
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Traverse all nodes of the &lt;em&gt;left subtree&lt;/em&gt; (postorder).&lt;/li&gt;
&lt;li&gt;Traverse all nodes of the &lt;em&gt;right subtree&lt;/em&gt; (postorder).&lt;/li&gt;
&lt;li&gt;Visit the node itself.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;postOrderTraversal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;postOrderTraversal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;postOrderTraversal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&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;blockquote&gt;
&lt;p&gt;Outcome: [4,6,8,7,5,2,3,1]&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>emptystring</category>
    </item>
  </channel>
</rss>
