<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jenny Shaw</title>
    <description>The latest articles on DEV Community by Jenny Shaw (@jenshaw).</description>
    <link>https://dev.to/jenshaw</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%2F174905%2F6e9bc129-da74-445b-a8a8-011d96e9cc3b.jpg</url>
      <title>DEV Community: Jenny Shaw</title>
      <link>https://dev.to/jenshaw</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jenshaw"/>
    <language>en</language>
    <item>
      <title>Now You Have a Job! But How Does Learning Fit in at Work?</title>
      <dc:creator>Jenny Shaw</dc:creator>
      <pubDate>Thu, 07 May 2020 17:54:18 +0000</pubDate>
      <link>https://dev.to/jenshaw/now-you-have-a-job-but-how-does-learning-fit-in-at-work-30d0</link>
      <guid>https://dev.to/jenshaw/now-you-have-a-job-but-how-does-learning-fit-in-at-work-30d0</guid>
      <description>&lt;p&gt;I'm now a professional junior developer, a little over 3 months in at very my first company, a tiny startup, and one of my team's biggest struggles has been figuring out how to integrate routine learning and education into a junior developer's work cycle.&lt;/p&gt;

&lt;p&gt;I want to get the most out of my first dev job, especially as a junior dev of whom learning is expected! I want to help my team help me improve this aspect of my experience, so share your experiences with me here -- &lt;/p&gt;

&lt;p&gt;👉🏼 &lt;strong&gt;What have your companies done to make sure that you learn and grow as developers while also balancing work?&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>career</category>
      <category>productivity</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>The Binary Numeral System (in Under 7500 Bytes)</title>
      <dc:creator>Jenny Shaw</dc:creator>
      <pubDate>Mon, 06 Jan 2020 22:46:10 +0000</pubDate>
      <link>https://dev.to/jenshaw/the-binary-numeral-system-in-under-7500-bytes-5ach</link>
      <guid>https://dev.to/jenshaw/the-binary-numeral-system-in-under-7500-bytes-5ach</guid>
      <description>&lt;p&gt;In this second article of my Unicode blog series, I'm going to introduce the &lt;strong&gt;binary system&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A computer accepts input, stores, processes, and outputs &lt;em&gt;all&lt;/em&gt; data in binary -- that includes everything you consume on your computers, from the text and color on this page, to images, gifs, video, and sound. &lt;/p&gt;

&lt;p&gt;So if we want to understand how Unicode is structured and how it's possible for us to have access and use over 150 scripts and over 3000 emojis, we need to understand a little about how binary works.&lt;/p&gt;




&lt;h2&gt;
  
  
  What are Binary Numbers?
&lt;/h2&gt;

&lt;p&gt;Binary data is composed of 1s and 0s.&lt;/p&gt;

&lt;p&gt;A binary digit, or a &lt;strong&gt;bit&lt;/strong&gt;, is &lt;em&gt;the smallest unit or building block of information that can be stored in a computer, and it can have one of two values, either 0 or 1&lt;/em&gt;. Varying sequences of bits strung together can each represent unique data. So, with more bits, we can compose more complex pieces of information.&lt;/p&gt;




&lt;h2&gt;
  
  
  Sizing Up
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Decimal Values&lt;/th&gt;
&lt;th&gt;Decimal Measure Names&lt;/th&gt;
&lt;th&gt;Decimal Symbols&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;sup&gt;0&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Byte&lt;/td&gt;
&lt;td&gt;b&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;sup&gt;3&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Kilobyte&lt;/td&gt;
&lt;td&gt;KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;sup&gt;6&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Megabyte&lt;/td&gt;
&lt;td&gt;MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;sup&gt;9&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Gigabyte&lt;/td&gt;
&lt;td&gt;GB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;sup&gt;12&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Terabyte&lt;/td&gt;
&lt;td&gt;TB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Binary Values&lt;/th&gt;
&lt;th&gt;Binary Measure Names&lt;/th&gt;
&lt;th&gt;Binary Symbols&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;sup&gt;0&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Byte&lt;/td&gt;
&lt;td&gt;b&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;sup&gt;10&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Kibibyte&lt;/td&gt;
&lt;td&gt;KiB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;sup&gt;20&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Mebibyte&lt;/td&gt;
&lt;td&gt;MiB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;sup&gt;30&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Gibibyte&lt;/td&gt;
&lt;td&gt;GiB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;sup&gt;40&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Tebibyte&lt;/td&gt;
&lt;td&gt;TiB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A &lt;strong&gt;byte&lt;/strong&gt; consists of &lt;em&gt;8 bits&lt;/em&gt;. We'll discuss in more depth this later on as we discuss ASCII and Unicode, but a byte is typically enough to store &lt;em&gt;1 typed character&lt;/em&gt;. That means your longest tweet will usually max out at 280 bytes, &lt;em&gt;or that 500-word cover letter I recently slaved over for a certain company was likely just under 3000 bytes, nearly 3&lt;/em&gt; &lt;strong&gt;kilobytes&lt;/strong&gt; &lt;em&gt;-- but I won't dwell on that.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;An image is composed of pixels each represented by a binary number. A high-resolution image can contain millions of pixels, which means that an image of that quality could be anywhere between 1 to 5 &lt;strong&gt;megabytes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A video, basically a collection of sequential frames or images, run between 24 to 30 frames per second, and that's a lot more data than just one image! A YouTube video in HD could require as much as 12 megabytes per minute. But if we were to stretch to movie-length, then that could require as much as 4 &lt;strong&gt;gigabytes&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;The amount of data required to fit a terabyte would be extraordinary. Check out the numbers from this &lt;a href="https://aimblog.uoregon.edu/2014/07/08/a-terabyte-of-storage-space-how-much-is-too-much/"&gt;blog&lt;/a&gt; and &lt;a href="https://www.dropbox.com/features/cloud-storage/how-much-is-1tb"&gt;Dropbox&lt;/a&gt; and you'll quickly get the idea.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Does the Binary System Work
&lt;/h2&gt;

&lt;p&gt;I think it's easiest to explain the binary system by refreshing our memories with a system we're all already very familiar with.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick Review: The Decimal System
&lt;/h3&gt;

&lt;p&gt;When we learned how to count bigger numbers in school, we were taught to memorize &lt;strong&gt;base 10 place values&lt;/strong&gt; from right to left -- 1s, 10s, 100s, 1000s, and so on. And we learned that each place fit one digit, and each digit value or &lt;strong&gt;state&lt;/strong&gt; could range from 0 to 9. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Base Ten&lt;/th&gt;
&lt;th&gt;10&lt;sup&gt;3&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;10&lt;sup&gt;2&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;10&lt;sup&gt;1&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;10&lt;sup&gt;0&lt;/sup&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Place Values&lt;/td&gt;
&lt;td&gt;1000&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So if we were to break down our present year, 2020, it would have a value of &lt;em&gt;2&lt;/em&gt; &lt;strong&gt;thousands&lt;/strong&gt;, &lt;em&gt;0&lt;/em&gt; &lt;strong&gt;hundreds&lt;/strong&gt;, &lt;em&gt;2&lt;/em&gt; &lt;strong&gt;tens&lt;/strong&gt;, and &lt;em&gt;0&lt;/em&gt; &lt;strong&gt;ones&lt;/strong&gt;. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Digit / State&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Place Values&lt;/td&gt;
&lt;td&gt;10&lt;sup&gt;3&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;10&lt;sup&gt;2&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;10&lt;sup&gt;1&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;10&lt;sup&gt;0&lt;/sup&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So when we multiply the digits with their respective place values and sum them together, we get the value 2020. &lt;/p&gt;

&lt;p&gt;Let's the math: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;(2 * 1000) + (0 * 100) + (2 * 10) + (0 * 1)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;= 2000 + 0 + 20 + 0&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;= 2020&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Now Introducing: The Binary System
&lt;/h3&gt;

&lt;p&gt;The binary system, on the other hand, has place values of base 2s.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Base Two&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;7&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;6&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;5&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;4&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;3&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;2&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;1&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;0&lt;/sup&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Place Values&lt;/td&gt;
&lt;td&gt;128&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And the system has only 2 states available, 0 and 1. &lt;/p&gt;




&lt;h3&gt;
  
  
  Counting Up in Binary
&lt;/h3&gt;

&lt;p&gt;So let's start counting. The first two numbers are easy -- &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;0, 1...&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Great! We made it this far -- &lt;em&gt;But how do we count to the next sequence of numbers if we're limited to using only 0s and 1s?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Just like with the decimal system, we assign a state to each place value.&lt;/p&gt;

&lt;p&gt;So, to count to two, we'd place a 1 over the 2s place.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Binary Number&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Place Values&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Let's find the decimal value here:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;(1 x 2) + ( 0 x 1)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;= 2 + 0&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;= 2&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And to count to three, we'd place a 1 over the 2s and the 1s place.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Binary Number&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Place Values&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;(1 x 2) + ( 1 x 1)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;= 2 + 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;= 3&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Good job! We can count to three!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/IXB6mQUgOqWQM/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/IXB6mQUgOqWQM/giphy.gif" alt="Sesame Street's the Count counts to three"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Counting with the Bits You're Given
&lt;/h3&gt;

&lt;p&gt;Currently, our tables contain 4 bits. If we were to max out all 4 bits, our largest value would be 15, (and the largest number of values 4 bits can store is 16 when we include 0).&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Binary Number&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Place Values&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;(1 x 8) + (1 x 4) + (1 x 2) + ( 1 x 1)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;= 8 + 4 + 2 + 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;= 15&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Handy Tip: If you have ten fingers to count with, you can count up to 1023 in binary!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/hvLwZ5wmarjnNKEJqq/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/hvLwZ5wmarjnNKEJqq/giphy.gif" alt="Toddler trying to count on her fingers"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Converting Decimal Numbers to Binary
&lt;/h3&gt;

&lt;p&gt;Now, let's try converting our year into binary. Because 2020 is such a large value and well over 15, it's going to require a lot more bits.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;11&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;10&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;9&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;8&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;7&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;6&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;5&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;4&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;3&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;2&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;1&lt;/sup&gt;
&lt;/th&gt;
&lt;th&gt;2&lt;sup&gt;0&lt;/sup&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Place Values&lt;/td&gt;
&lt;td&gt;2048&lt;/td&gt;
&lt;td&gt;1024&lt;/td&gt;
&lt;td&gt;512&lt;/td&gt;
&lt;td&gt;256&lt;/td&gt;
&lt;td&gt;128&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To do this, we'll find the place that is equal our target value or the largest valued place that is less than our target value, and assign it a state of 1.&lt;/p&gt;

&lt;p&gt;We subtract that place value, 1024, from our target, 2020. And that leaves us with the remainder, 996. &lt;/p&gt;

&lt;p&gt;Then we repeat the process with the following place values. If our remainder is ever less than the place value, we assign it a state of 0 and move on to the next place. We continue this algorithm until we're left with a remainder of 0. &lt;/p&gt;

&lt;p&gt;Here's our result!&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Place Values&lt;/td&gt;
&lt;td&gt;2048&lt;/td&gt;
&lt;td&gt;1024&lt;/td&gt;
&lt;td&gt;512&lt;/td&gt;
&lt;td&gt;256&lt;/td&gt;
&lt;td&gt;128&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now we know -- 2020 is equal to 011111100100 in binary!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/1GlDW1HBD3q2A/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/1GlDW1HBD3q2A/giphy.gif" alt="Long distance air high five"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Don't be fooled by place values. It's easy to see as above the 12th place of the binary system (2&lt;sup&gt;11&lt;/sup&gt;) having a value of 2048 and underestimate the potential of 12 bits. Don't forget that with 12 bits, by using every available place we can actually count up to 2&lt;sup&gt;12&lt;/sup&gt; - 1. That's 4095 in decimal and nearly double in value! So remember that when we're provided with more bits, we get &lt;em&gt;substantially&lt;/em&gt; more room for data. &lt;/p&gt;




&lt;h2&gt;
  
  
  Applying Binary to Character Encoding
&lt;/h2&gt;

&lt;p&gt;If you read my &lt;a href="https://dev.to/jenshaw/decoding-character-encoding-5a1i"&gt;previous blog on character encoding&lt;/a&gt;, you'll see that the binary system is important here because it is the language that computers communicate in and therefore essential for character encoding. And with more bits available, the more space there is to accommodate for more as well as more complex data, such as additional language scripts. &lt;/p&gt;

&lt;p&gt;So, coming up in later posts, I'll discuss ASCII's 7-bit and Unicode's 16-bit encoding systems, how those systems and charts work, and what &lt;em&gt;potential&lt;/em&gt; each of those systems can offer technologically as well as socially. &lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>Decoding Character Encoding</title>
      <dc:creator>Jenny Shaw</dc:creator>
      <pubDate>Mon, 30 Dec 2019 17:53:26 +0000</pubDate>
      <link>https://dev.to/jenshaw/decoding-character-encoding-5a1i</link>
      <guid>https://dev.to/jenshaw/decoding-character-encoding-5a1i</guid>
      <description>&lt;h2&gt;
  
  
  There was a time when...
&lt;/h2&gt;

&lt;p&gt;...If my grandmother had typed out a message, "哈囉" (meaning "hello" in Chinese) and sent it out from her computer in Taiwan to mine in the US, I wouldn't have been able to read it.&lt;/p&gt;

&lt;p&gt;You might think, &lt;em&gt;But, of course! She was writing in Chinese, and your Chinese skills would've been too terrible back then to be able to read it!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;(...which I suppose could have been true...)&lt;/p&gt;

&lt;p&gt;...except that the message would've been unrecognizable and unreadable by anyone, including herself, once it had reached me! When I should've seen this message that she sent me, "哈囉," I might've instead seen something like "" or some other text that clearly wouldn't have read like Chinese. The same might've happened if a message was sent the other way around. &lt;/p&gt;

&lt;p&gt;And that wouldn't have been &lt;em&gt;my&lt;/em&gt; fault! Or my grandmother's!&lt;/p&gt;

&lt;p&gt;For that, we could blame &lt;em&gt;character encoding&lt;/em&gt;.&lt;/p&gt;




&lt;p&gt;Before the standardization of character encoding in electronic communication, computers made by different manufacturers utilized different encoding schemas, which meant that &lt;em&gt;even users communicating in the same language&lt;/em&gt; would've been presented with messy strings of nonsense (also known as &lt;a href="https://en.wikipedia.org/wiki/Mojibake" rel="noopener noreferrer"&gt;&lt;em&gt;mojibake&lt;/em&gt;&lt;/a&gt;) when receiving a message from someone else. By using different encoding schemas, their computers weren't "speaking the same language," and so they read the same data differently.&lt;/p&gt;

&lt;p&gt;We take it for granted now, but with present standards for character encoding like &lt;a href="https://home.unicode.org/" rel="noopener noreferrer"&gt;&lt;strong&gt;Unicode&lt;/strong&gt;&lt;/a&gt; and &lt;a href="http://www.asciitable.com/" rel="noopener noreferrer"&gt;&lt;strong&gt;ASCII&lt;/strong&gt;&lt;/a&gt;, problems like the one I just described are easily prevented from happening. They help us type, send, and receive data the way it was intended to be written and read, and they are what makes our computing and internet experiences productive and enjoyable.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is character encoding?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Character encoding&lt;/strong&gt; is &lt;em&gt;using an encoding system to represent text characters&lt;/em&gt; like letters, numbers, punctuation, or spaces. One of the most famous and recognizable examples of character encoding is &lt;a href="https://en.wikipedia.org/wiki/Morse_code" rel="noopener noreferrer"&gt;Morse Code&lt;/a&gt;, which is a system of letters and numbers that are each encoded into a sequence of dots and dashes. We could send an encoded message using those dots and dashes, and the recipient of that message could then translate it back into their own language. &lt;/p&gt;

&lt;p&gt;In the context of computers, ASCII and Unicode are two encoding schemas that utilize the &lt;a href="https://en.wikipedia.org/wiki/Binary_number" rel="noopener noreferrer"&gt;binary number system&lt;/a&gt;. Computers can't read human languages, but they read binary numbers super well, so both schemas assign characters to binary numbers so that computers can read the binary sequence and convert it into a language we can understand.&lt;/p&gt;




&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fd%2Fdd%2FASCII-Table.svg%2F738px-ASCII-Table.svg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fd%2Fdd%2FASCII-Table.svg%2F738px-ASCII-Table.svg.png" alt="ASCII conversion table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  &lt;center&gt;The ASCII conversion table with code points assigned to each character&lt;/center&gt;
&lt;/h6&gt;

&lt;p&gt;Let's assume that our computers both use ASCII's encoding schema. ASCII has the capability to support 127 different characters, and each character is assigned a &lt;strong&gt;code point&lt;/strong&gt;. The code point can be a unique decimal number between 1 and 127** as well as a unique binary number whose value is between those numbers. The decimal code point for the character “A” is &lt;em&gt;65&lt;/em&gt;, which is represented as &lt;em&gt;1000001&lt;/em&gt; in binary.***&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmx7jgnp1j8821cpv7yab.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmx7jgnp1j8821cpv7yab.jpg" alt="A flow chart demonstrating how a standardized schema like ASCII facilitates consistent data transfer"&gt;&lt;/a&gt;&lt;br&gt;
So, if I were to type out the letter “A” on my keyboard, the keyboard would send the binary sequence, &lt;em&gt;1000001&lt;/em&gt;, to the computer, which would then look up the ASCII table to convert the binary code point to the character "A" for display on my screen. &lt;/p&gt;

&lt;p&gt;When I send it to your computer, it wouldn't be sent as the character "A", but as its binary code point. When your computer receives the message, it would also use the ASCII table to decode and convert the binary code and display the expected “A” onto your screen. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fer15w6677pvhfb83gwno.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fer15w6677pvhfb83gwno.jpg" alt="A flow chart demonstrating how two schemas can cause misinterpretation of data"&gt;&lt;/a&gt;&lt;br&gt;
If, however, your computer had utilized a different encoding schema where code point &lt;em&gt;65&lt;/em&gt; was assigned differently (let's just assume it was assigned to "Z"), you would have read "Z" because was converted according to that specific schema rather than the ASCII schema.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why is character encoding important?
&lt;/h2&gt;

&lt;p&gt;If the reasons aren't obvious by now, character encoding, especially &lt;em&gt;standardized&lt;/em&gt; character encoding, is important because it helps all parties understand each other. It helps us as users to communicate with our computers, and it helps our computers transmit and receive accurate data so that we can also communicate with each other! &lt;/p&gt;




&lt;h3&gt;
  
  
  Notes About This Series
&lt;/h3&gt;

&lt;p&gt;This is the first blog of my new series on standardized character encoding and Unicode. Over the following month and a half, I'll be explaining how the binary system works, diving into ASCII and Unicode, and discussing why Unicode is especially important to our global community. &lt;/p&gt;

&lt;p&gt;Character encoding itself is pretty straightforward, but I believe that as we dive deeper into it together, you'll be very surprised by how multifaced and fascinating the topic can be! This series deeply involves many of my personal passions, not just &lt;strong&gt;technology&lt;/strong&gt;, but also &lt;strong&gt;language&lt;/strong&gt;, &lt;strong&gt;culture&lt;/strong&gt;, and &lt;strong&gt;social issues&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;I'm &lt;em&gt;so&lt;/em&gt; excited to share these upcoming blogs with you, and I really hope you'll follow along! &lt;/p&gt;




&lt;h5&gt;
  
  
  Footnotes:
&lt;/h5&gt;

&lt;p&gt;** &lt;em&gt;Technically, there are 128 code points in the ASCII range, but 0 is assigned as a null character, so that leaves 127 usable code spaces.&lt;/em&gt;&lt;br&gt;
*** &lt;em&gt;It's worth noting that decimal code points are meant for human readability, but binary code points are for computers.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Tired of LinkedIn? 5 Ways to Search for Your Next Job</title>
      <dc:creator>Jenny Shaw</dc:creator>
      <pubDate>Sat, 28 Dec 2019 01:54:05 +0000</pubDate>
      <link>https://dev.to/jenshaw/tired-of-linkedin-5-ways-to-search-for-your-next-job-bl9</link>
      <guid>https://dev.to/jenshaw/tired-of-linkedin-5-ways-to-search-for-your-next-job-bl9</guid>
      <description>&lt;p&gt;When I first started my job search, the only way I knew to look for open positions was to browse through the most popular and common internet job boards such as Glassdoor, Indeed, or LinkedIn. And although they're all fantastic websites, I've learned over time that I much prefer to actively engage in the search by using other resources, ones that appeal to my personal interests, specific skills, and diverse background, and especially ones that involve more human interaction.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Meetups and Conferences
&lt;/h2&gt;

&lt;p&gt;Meetups and conferences are a great way to find job opportunities through personal connection building. At any event, you could meet someone who knows about an open position in their own or a friend's company, or at some larger events, you could browse job fairs or sponsored tables run by companies looking to hire. No matter what the event is though, it's always a chance to meet recruiters and other professionals and to make first-impressions far more memorable than your paper or digital resume. &lt;/p&gt;

&lt;p&gt;Here are some meetups and conferences that I've had great experiences with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://shescoding.org/"&gt;She's Coding&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://juniordevstrugglebus.com/"&gt;Junior Dev Struggle Bus&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://outintech.com/"&gt;Out in Tech&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.writespeakcode.com/"&gt;Write/Speak/Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.newtechnorthwest.com/"&gt;New Tech Northwest&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://womenintechregatta.com/"&gt;WiT Regatta&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some people stress out over about these in-person interactions and worry that they'll appear more thirsty and less genuine. But try to remember a lot of these meetups are made to facilitate connections and help them happen organically, and also remember that other people are attending these same events to have a good time and to bond over similar interests and causes. So just show up, be polite, and be yourself. And if you find that you like and care about the event enough to attend regularly, you'll soon enough find that you're part of a community that cares just as much about you and wants to contribute to your success. &lt;/p&gt;




&lt;h2&gt;
  
  
  2. Diversity Job Boards
&lt;/h2&gt;

&lt;p&gt;As a queer woman of color, it's extremely important to me that I find companies and teams that see value in diverse people and want to support them, so I try to access and utilize communities that I identify with as much as possible. &lt;/p&gt;

&lt;p&gt;In addition to the job boards of some meetups I attend, here are a few other favorites:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.diversifytech.co/"&gt;Diversify Tech&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.pocitjobs.com/"&gt;POCIT (People of Color in Tech)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.hiretechladies.com/"&gt;Tech Ladies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, there is definitely something to be said about companies that go out of their way to post on diversity job boards. To me, it's a strong indication that they want to create a diverse team, that they have an interest in the individuals in their company and what they can contribute, and that they value change and progress. So, whenever I see a job listing on any of these boards, I tend to be more interested and hyped about the company that posted it. &lt;/p&gt;




&lt;h2&gt;
  
  
  3. Networks from a Past Life: Alumni, Club, Societies
&lt;/h2&gt;

&lt;p&gt;I'm very lucky and privileged to be a part of many social networks, and I try to take full advantage of that by staying connected to as many organizations, clubs, and schools I've ever been involved with -- That includes my middle school, high school, college, semester program, bootcamp, and sub-chapters of each, like an LGBTQ or Seattle chapter if those exist as well. &lt;/p&gt;

&lt;p&gt;If you were part of a sorority or fraternity, a club, a specific school department, or whatever else you can think of, try to reconnect with those groups through all their social media sites. Follow them everywhere -- on Facebook, LinkedIn, Instagram, even Twitter -- introduce yourself, broadcast that you're on the market, and check regularly to see if anyone has any leads on job opportunities. People who are active in these groups are often eager and excited to help out members of their own community, so let them help you!&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Online Community Newsletters
&lt;/h2&gt;

&lt;p&gt;A small thing I do to expose myself to jobs that either I'll be more interested in or I'm better suited for is subscribe to various newsletters. I subscribe to the newsletters of diversity organizations like the ones I listed above and to tech-specific news digests like &lt;a href="https://javascriptweekly.com/"&gt;Javascript Weekly&lt;/a&gt; or &lt;a href="https://react.statuscode.com/"&gt;React Status&lt;/a&gt;, which not only keep you up to date on new changes but also include job listings that are often skill-specific. &lt;/p&gt;




&lt;h2&gt;
  
  
  5. Slack and Social Media
&lt;/h2&gt;

&lt;p&gt;To make sure that I fully take advantage of any tech community, meetup, or event, I &lt;strong&gt;always&lt;/strong&gt; make sure that I join their Slack group. If that option doesn't exist, then I'll look into LinkedIn, Facebook, and Twitter groups. &lt;/p&gt;

&lt;p&gt;Slack is great because most tech groups have a jobs channel or channel specific to your region where people share with the community about job openings and are enthusiastic to talk about their company and the role. If you reach out to them, some may offer to connect you with a manager or recruiter, or they may even offer to refer you.&lt;/p&gt;

&lt;p&gt;Combine Slack with LinkedIn, and you can more effectively research the company and play the networking game that helps get you one degree closer to communicating with someone who works at your dream company. &lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Using websites like LinkedIn and Glassdoor makes plenty of sense -- Almost every hiring company and job-seeker uses them, so there are always plenty of new job openings and updates to browse there, and the possibilities are endless. But for me, there can be such a thing as too many possibilities -- I could scroll through generic LinkedIn listings forever and ever, and that can feel overwhelming very quickly. &lt;/p&gt;

&lt;p&gt;Also, as a career-changer, having a curated feed isn't always helpful because those websites may return jobs I qualify for based on my skills and experiences I had (in education, management, and fitness), and not the jobs I want in software engineer.&lt;/p&gt;

&lt;p&gt;Most importantly though, what these websites don't consider are my interests and who I am as a person. I'm a software engineer, yes, but I'm also a career changer, a bootcamp graduate, and a self-learner. I identify as queer, a woman, a person of color, Asian, and Taiwanese-American. I care about learning, education, and social justice. I want to find a company that's going to value and embrace me for all of these qualities and interests, but I won't easily find that match through a giant job board, nor will I easily stand out among an ocean of candidates. That's why I think it's important to use these other specific resources in addition to the usual job search engines.&lt;/p&gt;

&lt;p&gt;The only big disadvantage to using these resources is that it takes more time and effort to comb through all the options, and some opportunities only come by chance, but the extra effort is worth it -- I'm much more motivated and engaged in the job search, and I'm actually excited to find jobs and companies that I can actually see myself working in!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I'm always looking for more resources that I can use and that I can share with others. If there are any awesome ones you love to use, share them in the comments below!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>career</category>
      <category>discuss</category>
      <category>webdev</category>
      <category>womenintech</category>
    </item>
    <item>
      <title>Binary Trees (Part 5) - Stay Abreast of Breadth-First Search</title>
      <dc:creator>Jenny Shaw</dc:creator>
      <pubDate>Mon, 16 Dec 2019 17:39:22 +0000</pubDate>
      <link>https://dev.to/jenshaw/binary-trees-stay-abreast-of-breadth-first-search-3pi7</link>
      <guid>https://dev.to/jenshaw/binary-trees-stay-abreast-of-breadth-first-search-3pi7</guid>
      <description>&lt;p&gt;&lt;em&gt;This week's blog post is a continuation of last week's article about &lt;a href="https://dev.to/jenshaw/binary-trees-discussing-in-depth-first-traversals-4a8n"&gt;Depth-First Searches and Traversals&lt;/a&gt; in binary trees where I briefly &lt;a href="https://dev.to/jenshaw/binary-trees-discussing-in-depth-first-traversals-4a8n#searches"&gt;compared Depth-First (DFS) and Breadth-First (BFS) Searches&lt;/a&gt; and then went into more depth explaining &lt;a href="https://dev.to/jenshaw/binary-trees-discussing-in-depth-first-traversals-4a8n#dfs"&gt;three common DFS methods&lt;/a&gt;: &lt;a href="https://dev.to/jenshaw/binary-trees-discussing-in-depth-first-traversals-4a8n#in-order"&gt;in-order&lt;/a&gt;, &lt;a href="https://dev.to/jenshaw/binary-trees-discussing-in-depth-first-traversals-4a8n#pre-order"&gt;pre-order&lt;/a&gt;, and &lt;a href="https://dev.to/jenshaw/binary-trees-discussing-in-depth-first-traversals-4a8n#post-order"&gt;post-order&lt;/a&gt;. For today's blog post, I'd like to discuss a couple of situations where we'd use DFS or BFS, and also share some code to explain how BFS works.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  A Quick Review of DFS and BFS
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/ii2FtzyirI4MXpoxX4/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ii2FtzyirI4MXpoxX4/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://i.giphy.com/media/iD6qdVmNOZH9frN6J4/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/iD6qdVmNOZH9frN6J4/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As discussed in my previous post, DFS allows us to recursively traverse through a binary tree, diving deeply, &lt;strong&gt;edge-by-edge&lt;/strong&gt;, and exhaustively exploring one branch of a tree before backtracking to the next unvisited branch, whereas BFS or Level-First Traversals allow us to visit nodes of the tree &lt;strong&gt;level-by-level&lt;/strong&gt;. &lt;/p&gt;




&lt;p&gt;Here's an (imperfect, but relatable) metaphor to help us visualize how DFS and BFS might process nodes.&lt;/p&gt;

&lt;p&gt;Imagine the binary tree as a buffet spread -- a long counter lined with various trays of food. DFS and BFS are eating tonight, and each has a different strategy for dining and traversing this buffet.&lt;/p&gt;

&lt;p&gt;BFS, like most of us, would take a serving of each dish onto its plate as it makes a single pass along the buffet counter. After it completes a pass, it would return to the start of the buffet counter and go another round. Each time, the food in all the trays would make it onto BFS's plate and eventually into its mouth.&lt;br&gt;
&lt;a href="https://i.giphy.com/media/L84mZKrSkc2JO/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/L84mZKrSkc2JO/giphy.gif" alt="Cookie Monster eating cookies across every tray on the kitchen counter"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DFS, on the other hand, would start at the first tray of the buffet counter lineup and keep scooping food until it's reached the bottom of the container. And only when it's completely emptied that tray, it would move onto the next tray in line and proceed to empty that one as well.&lt;br&gt;
&lt;a href="https://i.giphy.com/media/ttlpDZo5g7PPO/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ttlpDZo5g7PPO/giphy.gif" alt="Homer Simpson eating all the burgers in single pile"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Breadth-First Search
&lt;/h2&gt;

&lt;p&gt;In BFS, we traverse a tree from &lt;em&gt;top to bottom, left to right&lt;/em&gt;, so when we're processing the node values, we're doing so across levels. After we've exhausted all the nodes in a level, we proceed down to the next level.&lt;/p&gt;




&lt;h3&gt;
  
  
  Steps to Breadth-First Search:
&lt;/h3&gt;

&lt;p&gt;Before beginning the search, create the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a queue to keep track of all the nodes and their children that we'll need to process and&lt;/li&gt;
&lt;li&gt;a result array to print the nodes in order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To begin the traversal, first push the root node into the queue. Then,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Assign the first node in the queue to be the &lt;em&gt;current node&lt;/em&gt;,&lt;/li&gt;
&lt;li&gt;Process/Print the current node,&lt;/li&gt;
&lt;li&gt;If the current node has a left child, push the left child node into the queue,&lt;/li&gt;
&lt;li&gt;If the current node has a right child, push the right child node into the queue, and&lt;/li&gt;
&lt;li&gt;Shift or remove the first node from the queue.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Repeat steps 1 - 5 until the queue is empty again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/LP0RRlb0SXqw6GqcyK/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/LP0RRlb0SXqw6GqcyK/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Code: Printing Nodes in BFS Order
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;bfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Explanation:
&lt;/h3&gt;

&lt;p&gt;You may recall that in DFS, we'd traverse a tree using &lt;em&gt;recursion&lt;/em&gt;. The call stack that results from recursion would help us keep track of which node needed to be processed or bookmarked for later. &lt;/p&gt;

&lt;p&gt;However, in BFS, we'd use a queue* to keep track of the nodes that need to be processed. The first in the queue is always the &lt;em&gt;current node&lt;/em&gt;, and it's usually followed by a sibling node or a descendant node of the next level below. When we handle the current node, we process its value before we add their left and right children to the queue so that they can be processed later.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are other differences between DFS and BFS?
&lt;/h2&gt;

&lt;p&gt;As far as &lt;strong&gt;run-time&lt;/strong&gt; goes, DFS and BFS are the same at &lt;strong&gt;O(V+E)&lt;/strong&gt; (V for &lt;em&gt;vertices&lt;/em&gt; and E for &lt;em&gt;edges&lt;/em&gt;) or simply &lt;strong&gt;O(N)&lt;/strong&gt; because both searches will &lt;em&gt;visit every node in the tree once&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;And with regards to &lt;strong&gt;extra space&lt;/strong&gt;, DFS requires &lt;strong&gt;O(H)&lt;/strong&gt; space, where H stands for the &lt;em&gt;maximum height of the tree&lt;/em&gt;. It requires O(H) space because of recursion and the function call stack that &lt;em&gt;stores all the node ancestors&lt;/em&gt; as we traverse further down the tree. BFS also requires extra space, &lt;strong&gt;O(W)&lt;/strong&gt;, where W stands for the &lt;em&gt;maximum width of the tree&lt;/em&gt;. This is because the queue at a maximum must keep track of all of the &lt;em&gt;descendants at the widest level of the tree&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can we do with DFS and BFS?
&lt;/h2&gt;

&lt;p&gt;Now that we know how DFS and BFS work, we need to know what advantages one has over the other and situations when these searches could be applied!&lt;/p&gt;

&lt;p&gt;A target or a solution's distance from the root can be a deciding factor in which search to apply. For example, if we suspect that a target node is located deep inside of a tree, possibly closer to a leaf node, we might opt to use DFS because it searches nodes from leaves to root. However, if we're fairly certain that a node is located closer to the root instead, it would be wiser to use BFS since it searches from root to leaves. &lt;/p&gt;

&lt;p&gt;In addition, if you're searching for the shortest path from root to node, BFS is an obvious and efficient choice. DFS, however, is less ideal because even though it will always find the target node, it may not take the shortest route, especially because of how it dives deeply in and out of branches.&lt;/p&gt;

&lt;p&gt;Finally, DFS is more suitably used for games where decision making is involved in finding a solution. Think of finding the exit in a maze or encountering success in a quest or choose your own adventure game. BFS wouldn't be as useful in these situations though because it doesn't exhaustively explore paths the way that DFS does. However, while we're still on the topic of games, BFS is more concerned with finding the shortest path, so it might be better suited for a puzzle like a Rubik's cube where the goal is to solve the puzzle, not after exhausting all possibilities, but in as few turns as possible. &lt;/p&gt;

&lt;p&gt;Check out these pages by GeeksforGeeks if you're interested in learning more about where to apply &lt;a href="https://www.geeksforgeeks.org/applications-of-depth-first-search/"&gt;Depth-First&lt;/a&gt; and &lt;a href="https://www.geeksforgeeks.org/applications-of-breadth-first-traversal/"&gt;Breadth-First Traversals&lt;/a&gt;!&lt;/p&gt;

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

&lt;p&gt;That's all for Breadth-First Search, and for all things binary trees! &lt;/p&gt;

&lt;p&gt;This Binary Tree Blog Series all started with a couple of binary tree problems that I wasn't able to solve and then an obsessive desire to understand it better. This series is by no means a full and comprehensive guide to binary trees, but I hope that it's informative enough to help ease other newbie programmers like myself into learning more about the topic!&lt;/p&gt;

&lt;p&gt;Thanks for reading and learning along with me! &lt;/p&gt;




&lt;p&gt;For more information on binary trees, check out these other blogs from my 5-part binary tree series!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/the-basics-of-binary-trees-2kf8"&gt;Part 1 - The Basics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/binary-search-trees-are-the-best-gkk"&gt;Part 2 - Binary Search Trees (Insertion and Search)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/deleting-nodes-in-binary-search-trees-4nhm"&gt;Part 3 - Node Deletion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/binary-trees-discussing-in-depth-first-traversals-4a8n"&gt;Part 4 - Depth-First Traversals&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;&lt;em&gt;Footnotes:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;What's the difference between&lt;/em&gt; &lt;strong&gt;stack&lt;/strong&gt; &lt;em&gt;and&lt;/em&gt; &lt;strong&gt;queue&lt;/strong&gt; &lt;em&gt;data structures? A queue is like a waiting line at a cafeteria, where the first person to show up is also the first to be served and to leave. A stack, on the other hand, is much like a stack of dishes or trays at the cafeteria, where the first ones placed into the stack are later always the last ones to be taken out and used.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>todayilearned</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>Binary Trees (Part 4) - Discussing (in) Depth-First Traversals</title>
      <dc:creator>Jenny Shaw</dc:creator>
      <pubDate>Mon, 09 Dec 2019 17:10:32 +0000</pubDate>
      <link>https://dev.to/jenshaw/binary-trees-discussing-in-depth-first-traversals-4a8n</link>
      <guid>https://dev.to/jenshaw/binary-trees-discussing-in-depth-first-traversals-4a8n</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;Prerequisites:&lt;/em&gt;&lt;/strong&gt; &lt;br&gt;
&lt;em&gt;It'd obviously be beneficial to have some knowledge of &lt;a href="https://dev.to/jenshaw/the-basics-of-binary-trees-2kf8"&gt;binary trees&lt;/a&gt; and &lt;a href="https://dev.to/jenshaw/binary-search-trees-are-the-best-gkk"&gt;binary-search trees&lt;/a&gt; since this article is about binary tree traversals. Also, a concept that is frequently mentioned here and used in the context of binary trees is &lt;a href="https://medium.com/@chopra.chet/tech-blog-5156e152a608" rel="noopener noreferrer"&gt;recursion&lt;/a&gt;. If you're not familiar with any of these, I'd highly recommend you catch up on them first before reading further.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Traversing Through Binary Trees
&lt;/h2&gt;

&lt;p&gt;This week, we'll be exploring &lt;strong&gt;Binary Tree Traversals&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/l2JeavIMKpXLaJYXK/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l2JeavIMKpXLaJYXK/giphy.gif" alt="Smithers struggling to traverse through a forest brush"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;&lt;h6&gt;Not this kind of tree traversal, although sometimes the struggle feels the same.&lt;/h6&gt;&lt;/center&gt;

&lt;p&gt;First, I'll briefly explain the two types of traversals and searches, &lt;strong&gt;Depth-First Search (DFS)&lt;/strong&gt; and &lt;strong&gt;Breadth-First Search (BFS)&lt;/strong&gt;. Then, I'll focus on the three DFS methods, &lt;strong&gt;Pre-&lt;/strong&gt;, &lt;strong&gt;Post-&lt;/strong&gt;, and &lt;strong&gt;In-Order&lt;/strong&gt;. For each one, I'll share a tip to help you remember how each traversal works, explain how the traversal is used, and demonstrate what it would look like visually and in code.&lt;/p&gt;

&lt;p&gt;Sounds like an adventure! Let's go!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/gqh8uVtwvemTm/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/gqh8uVtwvemTm/giphy.gif" alt="Totoro and gang hiking through the forest"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  First, a Quick Story about Tree Traversals and Video Games
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ytimg.com%2Fvi%2F9BA167Scd4E%2Fmaxresdefault.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ytimg.com%2Fvi%2F9BA167Scd4E%2Fmaxresdefault.jpg" alt="Betrayal at Krondor Opening Title"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;&lt;h6&gt;Betrayal at Krondor - the greatest RPG of all time&lt;/h6&gt;&lt;/center&gt;

&lt;p&gt;I remember obsessively playing my all-time favorite RPG, &lt;a href="https://en.wikipedia.org/wiki/Betrayal_at_Krondor" rel="noopener noreferrer"&gt;&lt;em&gt;Betrayal at Krondor&lt;/em&gt;&lt;/a&gt;, and spending many endless hours getting helplessly lost trying to explore various towns, caves, tunnels, and other maze-like areas. There were times when I got so frustrated, I would reset the level so that I could strategize and test new ways to get through them without wasting so much effort. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.abandonwaredos.com%2Fpublic%2Faban_files_giochi%2Fkrondor_map.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.abandonwaredos.com%2Fpublic%2Faban_files_giochi%2Fkrondor_map.gif" alt="A map of Krondor"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here was the strategy I ultimately came up with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Whenever I encountered a split in the path, I would always take the left turn. &lt;/li&gt;
&lt;li&gt;And when I ever encountered a dead-end, I would backtrack to the split-off where I would take the next unexplored path to the left. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This strategy worked extremely well for me in the end because decision-making at the crossroads was a super straightforward process that required very little thought, and I never experienced again the same level of head-spinning confusion and anguish that I had when I was choosing paths arbitrarily. Most of all, I was able to explore every single path and find every treasure chest, character, and baddie that existed within the maze. And I was &lt;em&gt;always&lt;/em&gt; able to (eventually) find the exit. &lt;/p&gt;

&lt;p&gt;The best part of recalling this experience is realizing that, as a child, I was unknowingly applying a common binary tree traversal strategy, in-order traversal! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/hFROvOhBPQVRm/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/hFROvOhBPQVRm/giphy.gif" alt="Smart!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you know that the traversals we'll learn about here aren't just for trees: they can be applied to real-life situations, and you've probably already been using them!&lt;/p&gt;




&lt;h2&gt;
  
  
  Let's Get into Binary Tree Traversals!
&lt;/h2&gt;

&lt;p&gt;So, what does it mean to &lt;em&gt;traverse a tree&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;To &lt;strong&gt;&lt;em&gt;traverse&lt;/em&gt;&lt;/strong&gt; means to &lt;em&gt;move or pass through&lt;/em&gt;, so when we're traversing and searching through a tree, we're moving through it by &lt;strong&gt;&lt;em&gt;visiting each node along every branch until we encounter what we're looking for&lt;/em&gt;&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;When any node in a binary tree could potentially branch out in two directions, it can quickly become overwhelming and confusing for us when we have to consider efficient yet thorough ways to traverse the tree and find the target value. It's a lot like finding the exit in a maze without a map. &lt;/p&gt;

&lt;p&gt;Fortunately, there are several commonly used methods that can help us systematically traverse through trees!&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Depth-First Search vs. Breadth-First Search
&lt;/h2&gt;

&lt;p&gt;There are two broad categories of tree traversals and searches, &lt;strong&gt;Depth-First Search (DFS)&lt;/strong&gt; and &lt;strong&gt;Breadth-First Search (BFS)&lt;/strong&gt;. Both use unique approaches that allow us to visit every node in a tree.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/ii2FtzyirI4MXpoxX4/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ii2FtzyirI4MXpoxX4/giphy.gif" alt="Depth-First Search through a binary tree"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Depth-First Search&lt;/strong&gt; focuses on recursively processing nodes along a path between the root node and the leaf nodes. So imagine we're traversing straight down a path -- when a leaf node is finally reached, we &lt;em&gt;backtrack&lt;/em&gt; from there and return back up our previously traversed path until we reach the first un-explored branch, and then we traverse that branch. This cycle of exhaustively exploring then backtracking repeats until all of the nodes of the tree have been visited.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/iD6qdVmNOZH9frN6J4/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/iD6qdVmNOZH9frN6J4/giphy.gif" alt="Breadth-First Search through a binary tree"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Breadth-First Search&lt;/strong&gt; is also known as a &lt;strong&gt;Level-First Search&lt;/strong&gt; (Personally, I prefer the term Level-First because it emphasizes the concept of traversing by &lt;strong&gt;levels&lt;/strong&gt;. That - and I like simple words.) With BFS, we search all the nodes &lt;strong&gt;&lt;em&gt;level by level&lt;/em&gt;&lt;/strong&gt;, from the top of the tree to the bottom. This means that at the first level, we'd visit the root node, then on the second level, we'd visit its two children, and at each deeper level, we'd visit &lt;em&gt;all&lt;/em&gt; the descendants of that same generation, including children, their siblings, and their cousins. &lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Depth-First Traversals
&lt;/h2&gt;

&lt;p&gt;Here, we'll focus on these three Depth-First Searches.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Pre-Order&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;In-Order&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Post-Order&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each of these traversal methods is an algorithm or set of directions that dictates the order that we visit nodes and their subtrees. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Search&lt;/th&gt;
&lt;th&gt;Order&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;pre-&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;ROOT&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;left&lt;/td&gt;
&lt;td&gt;right&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;in-&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;left&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;ROOT&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;right&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;post-&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;left&lt;/td&gt;
&lt;td&gt;right&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;ROOT&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Although the traversal order changes with each method, there's one pattern that remains consistent: &lt;em&gt;The left node is&lt;/em&gt; &lt;strong&gt;&lt;em&gt;always&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;visited before the right node&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Also pay attention to the &lt;em&gt;prefixes&lt;/em&gt; of each search name because they'll help us better anticipate and understand what's going on. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Pre-&lt;/em&gt;&lt;/strong&gt; means '&lt;em&gt;before&lt;/em&gt;', so in this order, the root will be visited &lt;strong&gt;first&lt;/strong&gt; before either the left or right nodes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, think of &lt;strong&gt;&lt;em&gt;-in&lt;/em&gt;&lt;/strong&gt; as in &lt;em&gt;inside&lt;/em&gt;, and see how the root is located &lt;strong&gt;in&lt;/strong&gt; the middle of the node order.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, &lt;strong&gt;&lt;em&gt;-post&lt;/em&gt;&lt;/strong&gt; means '&lt;em&gt;after&lt;/em&gt;', so in this order, the root will be visited &lt;strong&gt;last&lt;/strong&gt;, &lt;em&gt;after&lt;/em&gt; the left and right nodes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now you can easily remember the respective orders of pre-order, in-order, and post-order!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3o7bukEYVSINz9FwKQ/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3o7bukEYVSINz9FwKQ/giphy.gif" alt="You guys are so smart!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  In-Order - Left, Root, Right
&lt;/h2&gt;

&lt;p&gt;With an in-order search, we traverse the tree from left to right, bottom to top, and its often used to &lt;em&gt;print out a list of visited nodes&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;When used on a Binary-Search Tree where values are sorted with smaller values to the left and larger values to the right, you'd get &lt;em&gt;a list of increasing values&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps to In-Order Traversal:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Traverse the &lt;strong&gt;left subtree&lt;/strong&gt; by recursively calling &lt;code&gt;inOrder&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;Process the &lt;strong&gt;root value&lt;/strong&gt; by pushing it into &lt;code&gt;nodes&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Traverse the &lt;strong&gt;right subtree&lt;/strong&gt; by recursively calling &lt;code&gt;inOrder&lt;/code&gt; function&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/cOosHV2GzVkdLwD2IG/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/cOosHV2GzVkdLwD2IG/giphy.gif" alt="In-Order Traversal through a binary tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Code: Writing the inOrder Function
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;nodes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;inOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;inOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;inOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before writing the &lt;code&gt;inOrder&lt;/code&gt; function, we assign an empty array to a variable called &lt;code&gt;nodes&lt;/code&gt;, which will later compile a list of node values processed in-order. As we traverse the tree, new node values will be added to it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;inOrder&lt;/code&gt; is the function that will determine the steps and order that nodes will be visited. By using it we'll recursively traverse the left subtree, process the node, then recursively traverse the right subtree.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Explanation
&lt;/h3&gt;

&lt;p&gt;Let's say we call the &lt;code&gt;inOrder&lt;/code&gt; function on the root node at the top of the tree. From the root node, we check for a left node, and if one exists, the function calls itself again using that node. Then from &lt;em&gt;that&lt;/em&gt; node, the process is repeated. As we move down the tree leftwards, we're creating a stack of &lt;code&gt;inOrder&lt;/code&gt; calls until we can't move leftwards anymore. &lt;/p&gt;

&lt;p&gt;When we finally reach the leftmost node at the end of the branch, the most recent &lt;code&gt;inOrder&lt;/code&gt; call runs the following line that pushes the root value to &lt;code&gt;nodes&lt;/code&gt;, and then the last line that checks if there's a right node to traverse. (In this case, there isn't, but if there were, &lt;code&gt;inOrder&lt;/code&gt; would be called again to process the right node and its descendants.)&lt;/p&gt;

&lt;p&gt;When that most recent call is complete, it's removed from the call stack, and as a result, we backtrack to the previous node that &lt;code&gt;inOrder&lt;/code&gt; was called with, process that node there, then follow down its right subtree.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Pre-Order - Root, Left, Right
&lt;/h2&gt;

&lt;p&gt;Pre-order search, similarly to in-order search, lets us print a list of the visited nodes, but this time from top to bottom, left to right. Pre-order traversal is often used to &lt;em&gt;copy a tree&lt;/em&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Steps to Pre-Order Traversal:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Process the &lt;strong&gt;root value&lt;/strong&gt; by pushing it into &lt;code&gt;nodes&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Traverse the &lt;strong&gt;left subtree&lt;/strong&gt; by recursively calling &lt;code&gt;preOrder&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;Traverse the &lt;strong&gt;right subtree&lt;/strong&gt; by recursively calling &lt;code&gt;preOrder&lt;/code&gt; function&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/dxCT5ok6OM7MS5zAqA/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/dxCT5ok6OM7MS5zAqA/giphy.gif" alt="Pre-Order Traversal through a binary tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Code: Writing a preOrder Function
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;nodes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;preOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;root&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;nodes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;preOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;preOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;preOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Explanation
&lt;/h3&gt;

&lt;p&gt;The process for pre-order search is very similar to in-order search, only that the order of nodes processed is rearranged to root, left, then right.&lt;/p&gt;

&lt;p&gt;When we want to &lt;a href="https://www.techiedelight.com/build-binary-search-tree-from-preorder-sequence/" rel="noopener noreferrer"&gt;construct a binary-search tree&lt;/a&gt; or &lt;a href="https://www.geeksforgeeks.org/construct-tree-from-given-inorder-and-preorder-traversal/" rel="noopener noreferrer"&gt;a new tree&lt;/a&gt;, we can use the pre-order as well as in-order lists to help us build it from the top down. The root node, the first of the printed list, would be established first before introducing the children nodes that connect to it.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Post-Order - Left, Right, Root
&lt;/h2&gt;

&lt;p&gt;Post-order traversal can be used to &lt;em&gt;delete a tree&lt;/em&gt; one node at a time, starting with the children, then their parent, all the way up to the root node. &lt;/p&gt;

&lt;h3&gt;
  
  
  Steps to Post-Order Traversal:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Traverse the &lt;strong&gt;left subtree&lt;/strong&gt; by recursively calling &lt;code&gt;postOrder&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;Traverse the &lt;strong&gt;right subtree&lt;/strong&gt; by recursively calling &lt;code&gt;postOrder&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;Process the &lt;strong&gt;root value&lt;/strong&gt; by pushing it into &lt;code&gt;nodes&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/Ri8whiqAwDo8A11rPY/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Ri8whiqAwDo8A11rPY/giphy.gif" alt="Post-Order Traversal through a binary tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Code: Writing the postOrder Function
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;nodes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;postOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;postOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;postOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;postOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Explanation
&lt;/h3&gt;

&lt;p&gt;Post-order traversal is almost the reverse of pre-order. While pre-order processes root, left, right, essentially from top to bottom, post-order processes left, right, and root, from bottom to top. &lt;/p&gt;

&lt;p&gt;If we were to apply it by deleting nodes from a tree, we'd process each external or leaf node, assign null to it, and effectively remove each one from the tree, then exposing internal nodes and making them the new leaf nodes ready to be removed later.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrap-Up &amp;amp; Conclusion
&lt;/h2&gt;

&lt;p&gt;That's it for Depth-First Traversals! There are many other kinds of depth-first traversals&lt;/p&gt;

&lt;p&gt;That was a lot of information to take in, but now that you know more about traversals, they probably seem a lot less scary and overwhelming than before! &lt;/p&gt;

&lt;p&gt;Next week and coming up last in my five-part Binary Tree series is the Breadth-First Traversal! Whew! We got this!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/43JOOvm7SbDbHTKQUm/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/43JOOvm7SbDbHTKQUm/giphy.gif" alt="Dust off"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Resources:
&lt;/h2&gt;

&lt;p&gt;If you want to see a video demonstration of all three depth-first traversals, this is an excellent view:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=BHB0B1jFKQc&amp;amp;t=290s" rel="noopener noreferrer"&gt;Binary Tree Bootcamp: Full, Complete, &amp;amp; Perfect Trees. Preorder, Inorder, &amp;amp; Postorder Traversal.&lt;/a&gt; - Back to Back SWE&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're interested in knowing more about constructing binary-search trees with in-order and pre-order sequences, check out these links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.geeksforgeeks.org/construct-tree-from-given-inorder-and-preorder-traversal/" rel="noopener noreferrer"&gt;Construct Tree from given Inorder and Preorder traversals&lt;/a&gt; - GeeksForGeeks&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.techiedelight.com/build-binary-search-tree-from-preorder-sequence/" rel="noopener noreferrer"&gt;Build a Binary Search Tree from a PreOrder Sequence&lt;/a&gt; - Techie Delight&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And for more about deleting trees using a post-order sequence, take a look at this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.geeksforgeeks.org/write-a-c-program-to-delete-a-tree/" rel="noopener noreferrer"&gt;Write a Program to Delete a Tree&lt;/a&gt; - GeeksForGeeks&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;For more information on binary trees, check out these other blogs from my 5-part binary tree series!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/the-basics-of-binary-trees-2kf8"&gt;Part 1 - The Basics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/binary-search-trees-are-the-best-gkk"&gt;Part 2 - Binary Search Trees (Insertion and Search)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/deleting-nodes-in-binary-search-trees-4nhm"&gt;Part 3 - Node Deletion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/binary-trees-stay-abreast-of-breadth-first-search-3pi7"&gt;Part 5 - Breadth-First Traversals&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>todayisearched</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Give Yourself Some TLC with Some TDD (Test-Driven Development)</title>
      <dc:creator>Jenny Shaw</dc:creator>
      <pubDate>Mon, 02 Dec 2019 21:48:56 +0000</pubDate>
      <link>https://dev.to/jenshaw/give-yourself-some-tlc-with-some-tdd-test-driven-development-42fd</link>
      <guid>https://dev.to/jenshaw/give-yourself-some-tlc-with-some-tdd-test-driven-development-42fd</guid>
      <description>&lt;p&gt;&lt;strong&gt;Test-Driven Development&lt;/strong&gt;, or more simply &lt;strong&gt;TDD&lt;/strong&gt;, is a development technique of writing tests prior to writing code to fulfill those tests. It's a technique that's reputed to be underappreciated and underused, however, there are many excellent benefits to using it, especially when it's implemented correctly.&lt;/p&gt;

&lt;p&gt;A lot of developers might skip over test-writing because they feel it's tedious and time-consuming. They may believe that manual testing and debugging tools are sufficient enough for gauging the functionality of their code, and that all the time spent writing tests could instead be put towards just building and completing their tasks sooner. &lt;/p&gt;

&lt;p&gt;But before I share with you the benefits of TDD, allow me to first share a couple of personal experiences and scenarios where this sort of mindset I described can actually be debilitating to your workflow, your productivity, and your mental health.&lt;/p&gt;




&lt;h2&gt;
  
  
  Scenario One
&lt;/h2&gt;

&lt;h3&gt;
  
  
  There was a time when I thought I was a pretty good developer...
&lt;/h3&gt;

&lt;p&gt;What I mean by that is that with skills and knowledge aside, I have many personal qualities that contribute to making me a good developer. For example, I'm organized and meticulous, and I try to prevent mistakes by planning ahead before starting any writing.&lt;/p&gt;

&lt;p&gt;But...I'll fully admit that my "planning" sometimes only takes place in my head...&lt;/p&gt;

&lt;p&gt;...so &lt;em&gt;sometimes&lt;/em&gt;, I might get &lt;strong&gt;sidetracked&lt;/strong&gt; as I'm working...&lt;/p&gt;

&lt;p&gt;...then &lt;em&gt;sometimes&lt;/em&gt;, I'd fall down a deep, &lt;em&gt;deep&lt;/em&gt; rabbit hole, and eventually &lt;strong&gt;lose track&lt;/strong&gt; of what I'm doing... &lt;/p&gt;

&lt;p&gt;...and &lt;strong&gt;&lt;em&gt;sometimes&lt;/em&gt;&lt;/strong&gt;...&lt;/p&gt;

&lt;p&gt;...just...sometimes...&lt;/p&gt;

&lt;p&gt;...I might entirely forget what I was doing to begin with.&lt;/p&gt;

&lt;p&gt;And just like time itself, the task I was working on passes with it, silently and without me ever noticing. &lt;/p&gt;




&lt;h2&gt;
  
  
  Scenario Two
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Code is EASY! Until there's more of it...
&lt;/h3&gt;

&lt;p&gt;I like to believe that for the &lt;em&gt;most&lt;/em&gt; part I think critically and carefully. I enjoy problem-solving and building, so it's fun and satisfying for me to write a few lines of code, run it, and instantly watch it work. I also like the challenge of writing longer, more complex lines of code. By manipulating multiple moving parts, finding ways to bring and bind them together into a smoothly operating machine, it feels like I'm performing the perfect juggling act. However, if there's so much as a single-letter typo, I get a broken, non-functional app and wasted hours filled with frustration and humiliation. &lt;/p&gt;

&lt;p&gt;Like &lt;em&gt;any&lt;/em&gt; person when given enough time, I'll make the occasional mistake here or there while writing long snippets of code. I'll continue building up, manually testing, sometimes even blindly trusting my code. And everything seems fine until I find a bug. Not a big deal, though. Bugs are common. I'll just patch it up. Easy peasy. &lt;/p&gt;

&lt;p&gt;But I debug it, I'll find something else that was previously working perfectly fine now broken. Then I'll work on that bug and effectively break something else. That chain will continue, and by then it'll be especially clear and visible to me that the problem did not simply stem from just one small bug, but from a series of bad foundational habits. At that point, it feels near-impossible to draw a clear line between the buggy feature and the single-line of code that caused it, and what follows may be many anxious hours of debugging and scanning through hundreds of lines of code. &lt;/p&gt;

&lt;p&gt;And during those long tension-filled hours, I'll also spend deeply and regretfully reflecting on how I could've made better choices and better spent my time.&lt;/p&gt;




&lt;h2&gt;
  
  
  Allow me a moment of denial
&lt;/h2&gt;

&lt;p&gt;But, but, BUT... I had &lt;em&gt;reasons&lt;/em&gt; for doing things the way that I did.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I was on a deadline! I just didn't always have time to write out careful, detailed plans!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And it seemed easier and more productive to just tap into my manic energy and type frantically as I things came to my head! I swear, it really felt like I'm getting things done faster.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And mistakes happen! They're &lt;em&gt;natural&lt;/em&gt;! And I thought I was sharp and SMART! With my massively-oversized confidence, I thought I'd make fewer mistakes. With my eagle eyes, I was sure I'd see errors as they presented. And with my powerful brain, I knew I'd &lt;em&gt;eventually&lt;/em&gt; find some way to debug them!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Besides, I may not have known exactly everything that I wanted and needed to do right at that moment, but I understood the bigger picture, and I knew what I wanted to do next. I figured that everything in the middle would eventually fall into place.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Snap back to reality
&lt;/h2&gt;

&lt;p&gt;I know now that all of those reasons were just excuses for poor habits and mediocre outcomes. After my last couple of experiences developing small projects using TDD, I've truly grown to appreciate tests and the time that I take to write them. And it's remarkable how much it's improved the way that I think about problems and progress through my code development.&lt;/p&gt;




&lt;h2&gt;
  
  
  Top 10 List: Benefits to TDD
&lt;/h2&gt;

&lt;p&gt;Despite most people's attitudes towards Test-Driven Development, I have experienced these upsides to using the technique. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It's helped me &lt;strong&gt;think critically&lt;/strong&gt; about my code.&lt;/li&gt;
&lt;li&gt;It encourages me to &lt;strong&gt;plan ahead&lt;/strong&gt; before blindly diving into a project. &lt;/li&gt;
&lt;li&gt;It creates a &lt;strong&gt;tighter, faster feedback loop&lt;/strong&gt;, allowing me to catch errors more &lt;strong&gt;efficiently&lt;/strong&gt; and code more &lt;strong&gt;confidently&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;It is 100% supportive of my experimental endeavors and efforts to &lt;strong&gt;improve&lt;/strong&gt; and &lt;strong&gt;refactor my code&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Continuing on from the previous two points, it helps keep my code &lt;strong&gt;tidier&lt;/strong&gt;, &lt;strong&gt;DRYer&lt;/strong&gt;, and &lt;strong&gt;less prone to bugginess&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It doubles as &lt;strong&gt;documentation&lt;/strong&gt; that &lt;strong&gt;clearly communicates&lt;/strong&gt; to you and others who read your code what you're trying to accomplish. &lt;/li&gt;
&lt;li&gt;It helps me &lt;strong&gt;set goals&lt;/strong&gt;, and keep me &lt;strong&gt;focused&lt;/strong&gt; and &lt;strong&gt;on task&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;It's an &lt;strong&gt;objective communicator&lt;/strong&gt; that helps bring &lt;strong&gt;transparency&lt;/strong&gt; to where my code is functioning correctly and where it's not. &lt;/li&gt;
&lt;li&gt;It brings &lt;strong&gt;clarity&lt;/strong&gt; to my workflow and &lt;strong&gt;minimizes my stress&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Most of all, it &lt;strong&gt;&lt;em&gt;improves my happiness&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TDD - Excellent for Mental Health and Productivity
&lt;/h2&gt;

&lt;p&gt;I want to focus on the &lt;strong&gt;happiness&lt;/strong&gt; aspect of TDD. Without sounding like some cheesy self-help author, I honestly feel that Test-Driven Development has helped my mental health. &lt;/p&gt;

&lt;h3&gt;
  
  
  Before TDD
&lt;/h3&gt;

&lt;p&gt;As I used to code out projects, I often watched my confidence like a rollercoaster dramatically rising and dipping, and it exacerbated a lot of my problems with anxiety and my self-image. &lt;/p&gt;

&lt;p&gt;Encountering surprise bugs or ones that were time-consuming to fix led me to believe that I wasn't a strong developer. That, in turn, made me frequently second guess my decisions and question my knowledge and abilities. And as I was consumed by imposter syndrome, I would become paranoid about making mistakes, and then I would have extreme anxiety about my poor time-management and my lack of focus. &lt;/p&gt;

&lt;p&gt;I wasted a lot of time overthinking a problem to make sure I got a solution right the first time (and as would be expected, it rarely happens that way), or I wasted time debugging my way through a chain of broken features. And no matter how much I tried to compensate for all of those issues, no matter how hard I worked to keep my code functional and clean, nearly 100% of the time, I had to accept my work as it was, brittle and fragile, and there was no room to improve without risking the chance to completely break everything. I was utterly saturated with stress. &lt;/p&gt;

&lt;h3&gt;
  
  
  How TDD Helped Me
&lt;/h3&gt;

&lt;p&gt;Test-Driven Development can prevent this downward spiral from happening. In a way, TDD is a lot like journaling. I document my goals, and then I focus on them. And as I accomplish those goals, I celebrate my success with a checkmark, and I move on to the next task. &lt;/p&gt;

&lt;p&gt;But what makes TDD even better than that is that my tests instantly communicate with me, signaling back and forth between red and green, letting me know what's going wrong or right. And so, instead of coding blindly and helplessly, I'm actively being guided and encouraged by my own tests. &lt;/p&gt;

&lt;p&gt;TDD helps give me many reasons to be happy. It helps me stay focused and productive and saves me time from debugging, which means I can use that time to do other fun and enjoyable things. It provides a clear sense of progress and acknowledges my wins and successes. Most of all, it helps me shed my anxiety and allows me to code with greater confidence. &lt;/p&gt;




&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;If you haven't had a chance to try out Test-Driven Development, I really recommend that you do! Here are a few free resources where you can learn about a few of the testing frameworks and libraries available out there. &lt;/p&gt;

&lt;p&gt;If you had to choose to look through only one of these, I highly, HIGHLY encourage you to check out &lt;a href="https://www.youtube.com/watch?v=Eu35xM76kKY&amp;amp;list=PL0zVEGEvSaeF_zoW9o66wa_UCNE3a7BEr"&gt;Fun Fun Function&lt;/a&gt;. The host of the channel, MPJ, does a fantastic job acting out all of the thoughts, feelings, and perspectives around TDD as he teaches &lt;a href="https://jestjs.io/"&gt;Jest&lt;/a&gt; basics. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fun Fun Function - &lt;a href="https://www.youtube.com/watch?v=Eu35xM76kKY&amp;amp;list=PL0zVEGEvSaeF_zoW9o66wa_UCNE3a7BEr"&gt;Video Playlist on Unit Testing&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The Odin Project - &lt;a href="https://www.theodinproject.com/courses/javascript/lessons/testing-basics"&gt;Testing Basics with Jest&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;freeCodeCamp - &lt;a href="https://www.freecodecamp.org/learn"&gt;Information Security and Quality Assurance Certification with Chai&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>testing</category>
      <category>codenewbie</category>
      <category>firstyearincode</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Binary Trees (Part 3) - Deleting Nodes in Binary-Search Trees</title>
      <dc:creator>Jenny Shaw</dc:creator>
      <pubDate>Mon, 25 Nov 2019 17:54:19 +0000</pubDate>
      <link>https://dev.to/jenshaw/deleting-nodes-in-binary-search-trees-4nhm</link>
      <guid>https://dev.to/jenshaw/deleting-nodes-in-binary-search-trees-4nhm</guid>
      <description>&lt;p&gt;&lt;em&gt;Node deletion was a basic method that I at first struggled with while learning how to manipulate&lt;/em&gt; &lt;a href="https://dev.to/jenshaw/binary-search-trees-are-the-best-gkk"&gt;Binary-Search Trees&lt;/a&gt; &lt;em&gt;(or&lt;/em&gt; &lt;strong&gt;BSTs&lt;/strong&gt;&lt;em&gt;). Already knowing how to delete nodes in a Linked List, I thought I could apply the same concepts and processes to a BST, but in cases deleting a node wasn't as intuitive as I expected it to be.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In this blog, I'm going to compare deleting nodes in a Linked List and in a Binary-Search Tree and discuss how the processes are similar, and where they differ. I'll also code a class method and a function that removes nodes, and I'll explain the code as I write it.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Review: Deleting a Node from a Linked List
&lt;/h2&gt;

&lt;p&gt;For those of you who are familiar with Linked Lists, the process for deleting a node from one is simple. You traverse through the list until you find the node that you want to delete. If that node happens to be at the end of the list, just delete it by pointing the previous node to null. And, &lt;em&gt;poof&lt;/em&gt;, gone. It's as simple as that. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/dU5hPLsCZzZv6Moxrt/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/dU5hPLsCZzZv6Moxrt/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Deleting a node in the middle of a list, however, requires a little more effort. If the target node falls in the middle of the list, we can't simply delete it because if we do, we also end up trashing the remaining successive nodes that it points to. That would be a massively unfortunate mistake whether you did that with a Linked List or with a BST. Here's a dramatized example of what that could look like in a BST.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/UVwZwhLDhM1sOLE7YY/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/UVwZwhLDhM1sOLE7YY/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, to avoid losing the rest of a Linked List, we point its previous node to its next node. By re-directing the previous node's pointer this way, we cut off any reference to the target node, essentially deleting it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/kG3iHs2e3WyhD1tR3W/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/kG3iHs2e3WyhD1tR3W/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Challenge: Deleting a Node from a BST
&lt;/h2&gt;

&lt;p&gt;Knowing what I knew about Linked Lists, I assumed that it'd be just as simple to delete nodes in a BST. And in &lt;em&gt;most&lt;/em&gt; cases, I was right. &lt;/p&gt;

&lt;h3&gt;
  
  
  Deleting a Node with 1 or Fewer Children
&lt;/h3&gt;

&lt;p&gt;In the case that the target node was a leaf at the end of a branch, we'd just delete it. &lt;br&gt;
&lt;a href="https://i.giphy.com/media/YrZ18grMiPQ6J2QWhP/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/YrZ18grMiPQ6J2QWhP/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And if the target node were to have only one child, we would just connect that node's parent to its child. &lt;br&gt;
&lt;a href="https://i.giphy.com/media/ZcLLXw3CXlqGrFVtRi/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ZcLLXw3CXlqGrFVtRi/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;But, here's where my assumptions were wrong and insufficient. Unlike a Linked List, BSTs don't follow a linear sequence where one node is followed another, which is then followed by another. Nodes in a BST branch out and could have as many as &lt;em&gt;two&lt;/em&gt; node children, a left &lt;em&gt;and&lt;/em&gt; a right. So you might ask some questions like,&lt;/p&gt;

&lt;p&gt;1) &lt;em&gt;How would we choose&lt;/em&gt; &lt;strong&gt;which&lt;/strong&gt; &lt;em&gt;of the node's children to connect to its parent?&lt;/em&gt; And after choosing,&lt;br&gt;
2) &lt;em&gt;How would we reattach and restructure the&lt;/em&gt; &lt;strong&gt;other&lt;/strong&gt; &lt;em&gt;child subtree so that we maintain the hierarchical rules of BSTs?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Thankfully, we don't have to worry about either of these questions because there's a special, yet straightforward approach to handling this problem. &lt;/p&gt;


&lt;h3&gt;
  
  
  Deleting a Node with 2 Children
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/S78Dap8pBddwvipsNi/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/S78Dap8pBddwvipsNi/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we do is after we find the target node, we mark its place, and we continue traversing down the branch, first by moving to the first right child, and then continuing to move down the branch, moving as far leftwards as possible until we reach a leaf node.&lt;/p&gt;

&lt;p&gt;The leaf we visit would have the smallest value of all the target node's right and larger-valued descendants, which makes it a perfect substitute for the target node we're about to delete. As the target's replacement, it keeps everything in order as it already is. Its left descendants still have smaller values than it, and its right descendants also still have values greater than it, and it maintains the bridge between upper and lower levels of the branch.&lt;/p&gt;


&lt;h2&gt;
  
  
  Write Code: deleteNode()
&lt;/h2&gt;

&lt;p&gt;In my &lt;a href="https://dev.to/jenshaw/binary-search-trees-are-the-best-gkk"&gt;previous blog&lt;/a&gt;, we learned how to get started coding BSTs (creating Node and BST classes, insert and find methods). We'll continue from where we left off last time and write the &lt;code&gt;deleteNode&lt;/code&gt; method step by step. &lt;/p&gt;


&lt;h3&gt;
  
  
  Setting Up: Create Remove Method and a Helper Function
&lt;/h3&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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="c1"&gt;// EVALUATING NODE&lt;/span&gt;
      &lt;span class="c1"&gt;// REMOVING VALUE&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;removeNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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;Create a method called remove that takes a target value as an argument. &lt;/p&gt;

&lt;p&gt;And inside of our method, we'll create a helper function called &lt;code&gt;removeNode&lt;/code&gt;. It'll be responsible for actually removing the node value in the tree, and we'll be using it recursively. This function will take in two arguments, a node and a value (the same value as the target value or the value of the node we want to remove). We'll call the function inside the remove method, and it will take in our root node as its first argument.&lt;/p&gt;


&lt;h3&gt;
  
  
  Compare Target and Node Values
&lt;/h3&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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="c1"&gt;// if no node exists, return null&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// *** COMPARE TARGET AND NODE VALUES BELOW***&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// if they match, &lt;/span&gt;
  &lt;span class="c1"&gt;// REMOVE VALUE HERE&lt;/span&gt;

  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// if target value is lesser than node value,&lt;/span&gt;
    &lt;span class="c1"&gt;// search and remove target in left subtree&lt;/span&gt;
    &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;removeNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="c1"&gt;// return updated node after removal&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// if target value is greater than node value&lt;/span&gt;
    &lt;span class="c1"&gt;// search and remove target in right subtree&lt;/span&gt;
    &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;removeNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="c1"&gt;// return updated node after removal&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Inside of &lt;code&gt;remove node&lt;/code&gt;, we're going to check first if the node is even valid. If it isn't, then the tree doesn't exist, and we just return null.&lt;/p&gt;

&lt;p&gt;Following that, compare this node's value against the target value. We want to check to see if it matches or not. If it does, we'll take further steps to start the removal. If it doesn't, we see if the target value is lesser or greater than the current node's. If it's lesser, we move to the left child, and if it's greater, then we move to the right. Either way, we'll call &lt;code&gt;removeNode&lt;/code&gt; again using our child node. And we'll recursively continue this search cycle until there's a match. &lt;/p&gt;


&lt;h3&gt;
  
  
  Finding a Match: Delete Node with One or No Children
&lt;/h3&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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="c1"&gt;// previous code&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// the node is a leaf,&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="c1"&gt;// delete the node&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// if there isn't a left child,&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="c1"&gt;// then replace node with right child&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// if there isn't a right child,&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="c1"&gt;// then replace node with left child&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now, let's focus on what to do when there's a match. First, we'll check if the node has any children. If it doesn't, that means it's a leaf node and we can safely delete it by giving it a value of null.&lt;/p&gt;

&lt;p&gt;But if the node does, in fact, have &lt;em&gt;one child&lt;/em&gt;, then we can replace it with its child node. &lt;/p&gt;

&lt;p&gt;At this point, we've covered all the simple steps of deleting a leaf node and replacing the node with the only available child.&lt;/p&gt;


&lt;h3&gt;
  
  
  Finding a Match: Delete Node with Two Children
&lt;/h3&gt;

&lt;p&gt;And now this is where it gets fun. And by fun, I mean messy. Maybe you'll want to take a brief brain break before we continue.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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="c1"&gt;// previous code&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="c1"&gt;// previous code&lt;/span&gt;

      &lt;span class="c1"&gt;// assigning right child node to temp&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

      &lt;span class="c1"&gt;// while there is a left child,&lt;/span&gt;
      &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// traverse along left branches &lt;/span&gt;
        &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="c1"&gt;// replace node value with temp value&lt;/span&gt;
      &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="c1"&gt;// delete leaf&lt;/span&gt;
      &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;removeNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Continuing the logic from here, we're assuming that the node has &lt;em&gt;two children&lt;/em&gt;, but we're only going to work with the &lt;em&gt;right child subtree&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;From the root of this subtree, we're going to traverse all the way down the branch, as far left as we can go until we reach a leaf. When we reach that destination, we replace the node value with the leaf (temp) value.&lt;/p&gt;

&lt;p&gt;Great! We've successfully deleted the target value from the node by replacing it with another already existing value. &lt;/p&gt;

&lt;p&gt;But we're not done! Now we need to delete the leaf node so that we're not left with doubles of the same value.&lt;/p&gt;

&lt;p&gt;We'll call the function &lt;code&gt;removeNode&lt;/code&gt; again, this time to remove the leaf node value of the same right child subtree.&lt;/p&gt;

&lt;p&gt;And &lt;strong&gt;now&lt;/strong&gt;, we're done.&lt;/p&gt;




&lt;p&gt;Here's the full code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;removeNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  

    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;removeNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;removeNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;removeNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That's it for now with BSTs and object methods. Next week, we dive into &lt;a href="https://dev.to/jenshaw/binary-trees-discussing-in-depth-first-traversals-4a8n"&gt;Binary Tree Traversals&lt;/a&gt;! &lt;/p&gt;




&lt;p&gt;For more information on binary trees, check out these other blogs from my 5-part binary tree series!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/the-basics-of-binary-trees-2kf8"&gt;Part 1 - The Basics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/binary-search-trees-are-the-best-gkk"&gt;Part 2 - Binary Search Trees (Insertion and Search)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/binary-trees-discussing-in-depth-first-traversals-4a8n"&gt;Part 4 - Depth-First Traversals&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/binary-trees-stay-abreast-of-breadth-first-search-3pi7"&gt;Part 5 - Breadth-First Traversals&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>datastructures</category>
      <category>algorithms</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Binary Trees (Part 2) - Binary-Search Trees are the BeST</title>
      <dc:creator>Jenny Shaw</dc:creator>
      <pubDate>Mon, 18 Nov 2019 16:48:03 +0000</pubDate>
      <link>https://dev.to/jenshaw/binary-search-trees-are-the-best-gkk</link>
      <guid>https://dev.to/jenshaw/binary-search-trees-are-the-best-gkk</guid>
      <description>&lt;p&gt;&lt;em&gt;In this blog, I'll be covering Binary-Search Trees, focusing primarily on BST structuring, how to create a BST class, insert new nodes, and check for a value in Javascript.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  What are Binary-Search Trees?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Binary-Search Trees (BSTs)&lt;/strong&gt; are &lt;a href="https://dev.to/jenshaw/the-basics-of-binary-trees-2kf8"&gt;a binary tree data structure&lt;/a&gt; that come with a special quality -- &lt;em&gt;sortedness&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;A BST is naturally sorted, which makes searching for a value extremely efficient and quick. And the BST class possesses methods to insert and delete nodes in ways that always preserve and maintain that sorted state.&lt;/p&gt;

&lt;p&gt;Nodes in a binary tree can point to no more than two children. In a BST, however, there are additional supreme rules about a node's location in relation to other nodes, and this is to maintain the hierarchical order of the tree. &lt;/p&gt;

&lt;p&gt;Each parent node points to a left child and/or a right child. If a child's value is &lt;strong&gt;&lt;em&gt;less&lt;/em&gt;&lt;/strong&gt; than the parent's, the child must be the &lt;strong&gt;left child&lt;/strong&gt; node. On the other hand, if the child's value is &lt;strong&gt;&lt;em&gt;greater&lt;/em&gt;&lt;/strong&gt;, then that child must be the &lt;strong&gt;right child&lt;/strong&gt; node.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4grWbmJ4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/6tu0uihduk35iv2a2zwy.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4grWbmJ4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/6tu0uihduk35iv2a2zwy.jpeg" width="720"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Break: Node and BST Classes
&lt;/h2&gt;

&lt;p&gt;Let's build out the basic pieces of a BST in Javascript. &lt;/p&gt;

&lt;p&gt;First, we'd write out a Node class. A node would have a &lt;em&gt;value&lt;/em&gt; property which contains the value used as we initialize a node object. It would also have references to a &lt;em&gt;left node&lt;/em&gt; and a &lt;em&gt;right node&lt;/em&gt;, both of which will be null since at the moment of its creation it'll just be a standalone node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To start building out the tree, we would also create a BST class. The class would contain a reference to the root, and because a new tree begins with a new node, the root would be the first newly initialized node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;BST&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You might've noticed that I also added another property to BST called &lt;code&gt;count&lt;/code&gt;. It refers to the number of nodes existing in the tree, and it'll be useful when you want to keep track of your node count as you're inserting or deleting nodes.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  BST Method: Node Insertion
&lt;/h2&gt;

&lt;p&gt;So, in the event that we want to insert a new node into a tree, we have to consider its value. A new node's value determines our path through the tree's branches all the way to the very end. It a potentially zigzagging journey all the way to the bottom.&lt;/p&gt;

&lt;p&gt;At every node that we visit, the new node compares its own value with the currently visited node to determine whether we should follow the left or right path from there. If the new node's value is smaller, we'll travel further left, or if it's larger, then we'll travel further right. &lt;/p&gt;

&lt;p&gt;And finally, when we reach a node where the next direction we'd want to follow points to null, we then point the current node to our new node and complete the insertion. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/hWvbD0RFojdpRiqR9A/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/hWvbD0RFojdpRiqR9A/giphy.gif" alt="Adding new nodes to a tree"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Break: Insert Method
&lt;/h2&gt;

&lt;p&gt;Inside of the BST class, following the constructor, we'll create a method called &lt;code&gt;insertNode&lt;/code&gt; which will do what we just described above. &lt;/p&gt;

&lt;p&gt;First we'll initialize the new Node that we want to insert.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// insert method inside of BST class&lt;/span&gt;
&lt;span class="nx"&gt;insertNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then, we need a helper method, &lt;code&gt;search&lt;/code&gt;, to help us with two tasks. &lt;/p&gt;

&lt;p&gt;The first is to search for the appropriate path from the current node to the next -- in other words, it chooses whether we go left or right. &lt;/p&gt;

&lt;p&gt;The second is to determine if there's a node following that path. If there isn't, the &lt;code&gt;search&lt;/code&gt; inserts the new node by pointing the current node to it. However, if there is, we'd continue in that direction and visit the next node where we start the search cycle all over again. &lt;/p&gt;

&lt;p&gt;This search cycle can be accomplished recursively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// write search helper method inside of insertNode() method&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;search&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//if the new node value is less than the current node value, we'll look left&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="c1"&gt;// if there's no left child,&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="c1"&gt;// then insert the new node&lt;/span&gt;
      &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="c1"&gt;// search the left node by calling the method on it &lt;/span&gt;
      &lt;span class="c1"&gt;// (yay, recursion!)&lt;/span&gt;
      &lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// if new node is greater than current node, we'll look right&lt;/span&gt;
  &lt;span class="c1"&gt;// repeat similar logic&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To wrap the &lt;code&gt;insertNode&lt;/code&gt; method up, we'd call &lt;code&gt;search&lt;/code&gt; on the root. This starts the search beginning on the root and then on every node that we visit thereafter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// at the end of insertNode method...&lt;/span&gt;

&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here's the entire method in a single snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;insertNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;search&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  BST Method: Checking if a Tree Contains a Value
&lt;/h2&gt;

&lt;p&gt;Now let's see if we can find target values!&lt;/p&gt;

&lt;p&gt;If I were to search for a value in a BST, it'd super quick. Even in your worst-case scenario, it wouldn't even have a time complexity of O(N) (meaning that you had visited and processed every single node on the tree) but of &lt;strong&gt;O(log N)&lt;/strong&gt;. You would never have to process more than half of the values in a tree to find your target. &lt;/p&gt;

&lt;p&gt;Remember when I mentioned that the left child always has a smaller value than the parent, meanwhile the right child has a greater value? Because it's set up this way, every time I compare the value I'm searching for with a node and as soon as I've decided whether to visit the left or right subtree, I've essentially discarded the other half of the tree. And each time I do this on a new node, I'm discarding my remaining search pile by half, thus saving significant time and effort. &lt;/p&gt;

&lt;p&gt;Below is an example of a successful search for the target value on a tree.&lt;br&gt;
&lt;a href="https://i.giphy.com/media/PnI2JjNhz5hKUg8xVY/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/PnI2JjNhz5hKUg8xVY/giphy.gif" alt="finding a value in a tree and returning true"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And below here is how we search and conclude that the target value doesn't exist.&lt;br&gt;
&lt;a href="https://i.giphy.com/media/d5wSRRGa4rE0L43XOO/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/d5wSRRGa4rE0L43XOO/giphy.gif" alt="not finding a value in a tree and returning false"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Code Break: Contains Method
&lt;/h2&gt;

&lt;p&gt;First, we start our search from the top of the tree. We'll want to establish a &lt;em&gt;current node&lt;/em&gt;, a marker to help us keep track of our location on the tree as we travel down it. We'll start the marker at the root by assigning &lt;code&gt;this.root&lt;/code&gt; to &lt;code&gt;current&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then we'll do two things. First, we'll compare the target value to the current node value and see if they match. If they do, we return true, and we're done! If they don't match, then we'll do the second thing, move down the tree one node. If the target value is less than the current value, then we'll move on to the left node by assigning the left node to &lt;code&gt;current&lt;/code&gt;. Otherwise, the right node is &lt;code&gt;current&lt;/code&gt;. When the loop is complete, we'll repeat the process on the following node. If we've searched the tree from top to bottom with no success, then we break out of the loop and simply return false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// add a new method to BST class&lt;/span&gt;

&lt;span class="nx"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// while there is a current node&lt;/span&gt;
    &lt;span class="c1"&gt;// compare values&lt;/span&gt;

    &lt;span class="c1"&gt;// is it a match?&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// if not, move down a node&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Binary-Search Trees are one of the most satisfyingly useful and efficient data structures. Once you understand the structure, they're rather intuitive and easy to understand. And because they're already sorted, they're excellent for searches, insertions, and deletions. Deletions are a little more complicated than the methods I covered here, so I'll be writing more about it in the &lt;a href="https://dev.to/jenshaw/deleting-nodes-in-binary-search-trees-4nhm"&gt;next blog&lt;/a&gt;. Stay tuned!&lt;/p&gt;




&lt;p&gt;For more information on binary trees, check out these other blogs from my 5-part binary tree series!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/the-basics-of-binary-trees-2kf8"&gt;Part 1 - The Basics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/deleting-nodes-in-binary-search-trees-4nhm"&gt;Part 3 - Node Deletion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/binary-trees-discussing-in-depth-first-traversals-4a8n"&gt;Part 4 - Depth-First Traversals&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/binary-trees-stay-abreast-of-breadth-first-search-3pi7"&gt;Part 5 - Breadth-First Traversals&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>datastructure</category>
      <category>algorithms</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Binary Trees (Part 1) - The Basics</title>
      <dc:creator>Jenny Shaw</dc:creator>
      <pubDate>Wed, 13 Nov 2019 21:10:09 +0000</pubDate>
      <link>https://dev.to/jenshaw/the-basics-of-binary-trees-2kf8</link>
      <guid>https://dev.to/jenshaw/the-basics-of-binary-trees-2kf8</guid>
      <description>&lt;p&gt;&lt;em&gt;After a few surprise encounters with binary trees last week, I thought it'd be good for me to finally get more familiar with them so that later I can tackle binary tree problems with more confidence and less angst.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is simply an introduction to trees and the different parts that make up the structure. I also touch on what various binary tree structures can look like and why trees, especially binary trees, are applicable and useful.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Binary Trees?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.fineartamerica.com%2Fimages-medium-large-5%2Fupside-down-tree-marty-kugler.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.fineartamerica.com%2Fimages-medium-large-5%2Fupside-down-tree-marty-kugler.jpg" alt="A tree once torn out of the ground planted upside down on its branches"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;center&gt;Behold, a perfect specimen of the tree data structure in its natural habitat, shot by &lt;a href="https://fineartamerica.com/featured/upside-down-tree-marty-kugler.html" rel="noopener noreferrer"&gt;Marty Kugler&lt;/a&gt;
&lt;/center&gt;
&lt;/h5&gt;

&lt;p&gt;A &lt;strong&gt;tree&lt;/strong&gt; is a data structure that looks like an upside-down tree consisting of nodes. A &lt;strong&gt;binary tree&lt;/strong&gt; is a structure of nodes that each point to no more than two children.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Physiology of a Tree
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/cPg6XJTNxDlhQz8zen/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/cPg6XJTNxDlhQz8zen/giphy.gif" alt="Image of a basic binary tree structure and a subtree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;tree&lt;/strong&gt; is a structure composed of &lt;strong&gt;nodes&lt;/strong&gt; that each contains a value or data. The &lt;strong&gt;root&lt;/strong&gt; is a single node located at the top of the tree. It is the start of the tree where nodes point to child nodes connected by branchlets called &lt;strong&gt;edges&lt;/strong&gt;. A &lt;strong&gt;subtree&lt;/strong&gt;, much like a clipping, is a subsection or part of a tree.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://i.giphy.com/media/TJglzdRKOELC3W12mZ/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/TJglzdRKOELC3W12mZ/giphy.gif" alt="Three nodes connected, a parent and its two children"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The root of this tree is the &lt;strong&gt;parent&lt;/strong&gt; of two &lt;strong&gt;child&lt;/strong&gt; nodes, each either a left or a right child. Two children or sub-trees of a shared parent are &lt;strong&gt;siblings&lt;/strong&gt;. It's possible for a child to also be the parent of two other child nodes further down the branch. &lt;/p&gt;




&lt;p&gt;&lt;a href="https://i.giphy.com/media/KezoursDTpXWUjG3PO/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/KezoursDTpXWUjG3PO/giphy.gif" alt="Image illustrating the relationship between ancestor and descendant nodes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we follow a branch down from a parent node, any other node located down that branch is its &lt;strong&gt;descendent&lt;/strong&gt;. And if we follow a branch upwards from a child node, any node up that branch is an &lt;strong&gt;ancestor&lt;/strong&gt; of the child node. &lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxk1qnu5mdxow8a58zzow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxk1qnu5mdxow8a58zzow.png" alt="A tree showing internal nodes that have children, and external nodes that have no children"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the bottom or terminal of a branch is the &lt;strong&gt;leaf&lt;/strong&gt; or &lt;strong&gt;external node&lt;/strong&gt;, which has no children. Nodes that branch further to one or more child nodes are &lt;strong&gt;branch&lt;/strong&gt; or &lt;strong&gt;internal nodes&lt;/strong&gt;.&lt;/p&gt;




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

&lt;p&gt;When we describe a binary tree, we can discuss its &lt;strong&gt;height&lt;/strong&gt; or its &lt;strong&gt;depth&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6qwyiiz5i0zuufy2lhhg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6qwyiiz5i0zuufy2lhhg.png" alt="A view of a tree and each node's depth from the root"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we talk about a node's depth, we're specifying how many branches or levels down a node is from the root. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/MY1IngRAdaHVuNQbSu/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/MY1IngRAdaHVuNQbSu/giphy.gif" alt="A step by step view of a tree increasing in height as new levels of nodes are added"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, when we talk about height, we can either describe the height of a node or the height of the tree. In both cases, we're describing the distance from an external node to the node or root. &lt;/p&gt;




&lt;h2&gt;
  
  
  Binary Tree Identification
&lt;/h2&gt;

&lt;p&gt;Binary trees can be structured differently and possess different kinds of characteristics or qualities. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fr2pju3fgkd5vguciusbo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fr2pju3fgkd5vguciusbo.png" alt="A full binary tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;full&lt;/strong&gt; or &lt;strong&gt;strictly binary tree&lt;/strong&gt; is structured so that every node possesses either two or no children.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F87vvozjt3vb4p94equv7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F87vvozjt3vb4p94equv7.png" alt="complete binary tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;complete binary tree&lt;/strong&gt; is a tree where nodes at every level but the last is required to have two children. On the last level, the children are positioned as far to the left as possible. So, complete binary tree nodes are connected from top to bottom, left to right. &lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fu9ybf23isl86jzdkei89.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fu9ybf23isl86jzdkei89.png" alt="perfect binary tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;perfect binary tree&lt;/strong&gt; is both &lt;em&gt;full&lt;/em&gt; and &lt;em&gt;complete&lt;/em&gt;. All the leaf nodes are located at the same depth, and all internal nodes have two children.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://i.giphy.com/media/iJgItT1WOBadn4NGle/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/iJgItT1WOBadn4NGle/giphy.gif" alt="Balanced and Unbalanced Binary Tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;balanced binary tree&lt;/strong&gt; describes a tree where two sibling subtrees don't differ in height by more than one level. If the difference in height is greater than that, then it is unbalanced.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ffm989bdx4uhs98ey8qv6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ffm989bdx4uhs98ey8qv6.png" alt="Degenerate Tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If every node in a tree has only one child, the structure is a &lt;strong&gt;degenerate&lt;/strong&gt; or &lt;strong&gt;pathological tree&lt;/strong&gt;, and is essentially a linked list.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why are Binary Trees Useful?
&lt;/h2&gt;

&lt;p&gt;Like the family trees many of us are familiar with, trees are hierarchical and can represent structural relationships in the data. More technological examples and applications of trees can be the DOM or your file directory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/jenshaw/binary-search-trees-are-the-best-gkk"&gt;&lt;strong&gt;Binary Search Trees (BSTs)&lt;/strong&gt;&lt;/a&gt; are especially useful in algorithms because they are naturally sorted, which makes &lt;a href="https://dev.to/jenshaw/binary-search-trees-are-the-best-gkk"&gt;search, insertion&lt;/a&gt;, and &lt;a href="https://dev.to/jenshaw/deleting-nodes-in-binary-search-trees-4nhm"&gt;deletion&lt;/a&gt; of values especially quick and efficient. This is definitely worth diving into in another blog post!&lt;/p&gt;




&lt;p&gt;For more information on binary trees, check out these other blogs from my 5-part binary tree series!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/binary-search-trees-are-the-best-gkk"&gt;Part 2 - Binary Search Trees (Insertion and Search)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/deleting-nodes-in-binary-search-trees-4nhm"&gt;Part 3 - Node Deletion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/binary-trees-discussing-in-depth-first-traversals-4a8n"&gt;Part 4 - Depth-First Traversals&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/jenshaw/binary-trees-stay-abreast-of-breadth-first-search-3pi7"&gt;Part 5 - Breadth-First Traversals&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>codenewbie</category>
      <category>todayilearned</category>
      <category>datastructures</category>
      <category>trees</category>
    </item>
    <item>
      <title>Reversing a Linked List</title>
      <dc:creator>Jenny Shaw</dc:creator>
      <pubDate>Mon, 14 Oct 2019 14:51:55 +0000</pubDate>
      <link>https://dev.to/jenshaw/reversing-a-linked-list-11o5</link>
      <guid>https://dev.to/jenshaw/reversing-a-linked-list-11o5</guid>
      <description>&lt;h4&gt;
  
  
  A Linked List Problem
&lt;/h4&gt;

&lt;p&gt;I'm learning about &lt;a href="https://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/linked%20lists.html" rel="noopener noreferrer"&gt;Linked Lists&lt;/a&gt; and trying my hand at my first linked list problems. Here's a basic one that I want to focus on today.&lt;/p&gt;

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

Task: Reverse a singly linked list.

Input: 1 -&amp;gt; 2 -&amp;gt; 3 -&amp;gt; 4 -&amp;gt; 5 -&amp;gt; NULL
Output: 5 -&amp;gt; 4 -&amp;gt; 3 -&amp;gt; 2 -&amp;gt; 1 -&amp;gt; NULL


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h4&gt;
  
  
  Failed First Tries
&lt;/h4&gt;

&lt;p&gt;Remember the last time I blogged about &lt;a href="https://dev.to/jenshaw/reversing-an-integer-mathematically-fmg"&gt;reversing strings and integers&lt;/a&gt;? I mentioned then that the first time I attempted an integer reversal, I approached it the same way I did with strings and arrays, which didn't work out as great as I would've liked. As per my usual habit, I made a similar mistake here with reversing a linked list.&lt;/p&gt;

&lt;p&gt;I started by thinking I'd use the old 'pop' and 'push' approach and realized almost immediately that that just wasn't going to work with this data structure. With singly-linked lists, just to pop or remove the last node would involve traversing the entire length of the linked list, from head to tail, one node at a time. And then, there was a second partial journey to consider. Starting once again from the head of the list, I'd have to re-traverse until I found the appropriate place to re-insert the node. That means, for each and every node I wanted to move, I had to traverse the list at least one and a half times, and that could take forever if your list happened to be just a few nodes too long. It seemed an awfully redundant approach that just didn't make great sense. I was sure there was a better way to go about it.&lt;/p&gt;

&lt;p&gt;And there was. Unfortunately, though, I didn't quite figure it out on my own. Ah well.&lt;/p&gt;

&lt;p&gt;After about a half-hour of honest effort, I looked up the solution, which I admittedly couldn't understand, but later also found a great &lt;a href="https://www.youtube.com/watch?v=O0By4Zq0OFc" rel="noopener noreferrer"&gt;video explanation&lt;/a&gt; by Back to Back SWE that helped clarify my confusion.&lt;/p&gt;




&lt;h4&gt;
  
  
  A Step By Step Solution Explanation
&lt;/h4&gt;

&lt;p&gt;The video covers two solutions, one iterative and the other recursive, but I'm just going to focus on explaining the iterative solution for now.&lt;/p&gt;

&lt;p&gt;I'll set this explanation up in three stages:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverseList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Stage 1: The Setup&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Stage 2: The Reversal&lt;/span&gt;
    &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Stage 3: The Shift (Variable Reassignment)&lt;/span&gt;
    &lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h5&gt;
  
  
  Stage One
&lt;/h5&gt;

&lt;p&gt;In the first stage, I'll have three variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;curr&lt;/code&gt; to keep track of the &lt;em&gt;current&lt;/em&gt; node starting at the head of the list,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;prev&lt;/code&gt; to track the node previous to the &lt;code&gt;curr&lt;/code&gt; and is null only for now because there's no attached node prior to &lt;code&gt;curr&lt;/code&gt; at the moment, and finally...&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;temp&lt;/code&gt;, a temporary container for the node &lt;code&gt;curr&lt;/code&gt; presently points to. I won't assign anything to it just yet, so for now, it'll be null.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frxp7os9opcz9cptvp1oj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frxp7os9opcz9cptvp1oj.png" alt="linked list with node 1 assigned to curr, null assigned to prev, and null assigned to temp variables"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h5&gt;
  
  
  Stage Two
&lt;/h5&gt;

&lt;p&gt;At the second stage, we'll open a while loop, during which we'll be re-arranging the nodes.&lt;/p&gt;

&lt;p&gt;Keep in mind that with every loop, &lt;code&gt;curr&lt;/code&gt; is going to move up the nodes in the list. As &lt;code&gt;curr&lt;/code&gt; moves forward node by node, it'll eventually reach null, the end of the list, and that will break the while loop.&lt;/p&gt;

&lt;p&gt;At the first line of the loop, we'll assign &lt;code&gt;curr.next&lt;/code&gt;, the node following &lt;code&gt;curr&lt;/code&gt;, to our variable, &lt;code&gt;temp&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1zdswamrnd6lag5lwzf8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1zdswamrnd6lag5lwzf8.png" alt="node 2 as curr dot next, assigned to temp"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is &lt;code&gt;temp&lt;/code&gt;'s job to help us keep that particular node and the connecting nodes thereafter safe and within reach. Why is that important? Because we're about to sever that node from &lt;code&gt;curr&lt;/code&gt;, and we don't want to lose it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/Mr0t48ZNCfJGo/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Mr0t48ZNCfJGo/giphy.gif" alt="Dog on boat detaching a rope attached to a person"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the following line, by assigning &lt;code&gt;prev&lt;/code&gt; to &lt;code&gt;curr.next&lt;/code&gt;, we direct &lt;code&gt;curr&lt;/code&gt;'s only pointer towards &lt;code&gt;prev&lt;/code&gt;, thus breaking the link to what used to be our old &lt;code&gt;curr.next&lt;/code&gt; node as well as the rest of the list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ft9ut5kefdfhlzt9pal1p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ft9ut5kefdfhlzt9pal1p.png" alt="prev, currently null, assigned as curr dot next"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fyo3gxrb7mwl1eqdek1v2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fyo3gxrb7mwl1eqdek1v2.png" alt="breaks link from node 1 to node 2"&gt;&lt;/a&gt;&lt;br&gt;
Good thing we were prepared and kept that node secured in &lt;code&gt;temp&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3krrjoL0vHRaWqwU3k/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3krrjoL0vHRaWqwU3k/giphy.gif" alt="Whew"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h5&gt;
  
  
  Stage Three
&lt;/h5&gt;

&lt;p&gt;One last thing before we complete this loop. In preparation for the next loop, we have to shift our variables over by one node. The current node is now &lt;code&gt;prev&lt;/code&gt;, and &lt;code&gt;curr&lt;/code&gt; is the head of our severed list in &lt;code&gt;temp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmp47p03kg5iya56bmm86.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmp47p03kg5iya56bmm86.png" alt="node 1 becomes prev, node 2 becomes curr"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;You might notice that we now essentially have two separate lists, &lt;br&gt;
&lt;code&gt;1 -&amp;gt; NULL&lt;/code&gt; and &lt;code&gt;2 -&amp;gt; 3 -&amp;gt; 4 -&amp;gt; 5 -&amp;gt; NULL&lt;/code&gt;. But no worries because as we continue looping, we're going to rejoin them node by node until the reversed list is complete.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/d9TdpWRIDu9CXONF8P/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/d9TdpWRIDu9CXONF8P/giphy.gif" alt="step by step demonstration of reverse linked list algorithm"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h4&gt;
  
  
  Some Thoughts
&lt;/h4&gt;

&lt;p&gt;When I finally understood the solution, I felt like my mind was blown. It's really &lt;em&gt;not&lt;/em&gt; that complicated a problem or a solution, but as the process of the algorithm was drawn out, there became significant shift in my perspective. As I watch the reversal step by step, I realize that all that's being done here is not the rearrangement of the nodes into reverse order, but the &lt;em&gt;reversal of direction&lt;/em&gt; in the linked list. I've been so focused on the order of nodes, looking at them like in an array, that I wasn't looking at the pointers and the role they played in the data structure. I'd completely overlooked that simply redirecting the pointer could've achieved the same thing. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fv4326p4rma8x9ktxesry.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fv4326p4rma8x9ktxesry.png" alt="Two identical linked lists going in reverse direction"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://i.giphy.com/media/l41YfdYdptDB9RHIA/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l41YfdYdptDB9RHIA/giphy.gif" alt="Of course"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There's really no difference between &lt;code&gt;NULL &amp;lt;- 1 &amp;lt;- 2 &amp;lt;- 3 &amp;lt;- 4 &amp;lt;- 5&lt;/code&gt; and &lt;code&gt;5 -&amp;gt; 4 -&amp;gt; 3 -&amp;gt; 2 -&amp;gt; 1 -&amp;gt; NULL&lt;/code&gt;, but for me, seeing the list rotated just 180 degrees changed the way that I perceived linked lists, and I think that it helps me be more flexible in the ways that I approach them in the future. &lt;/p&gt;

&lt;p&gt;I hope these illustrations I created made it easier for you visualize, understand, and replicate this solution to the problem too! &lt;/p&gt;




&lt;p&gt;&lt;em&gt;Blog References&lt;/em&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/linked%20lists.html" rel="noopener noreferrer"&gt;Linked Lists&lt;/a&gt;, Victor S.Adamchik, CMU, 2009&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=O0By4Zq0OFc" rel="noopener noreferrer"&gt;How To Reverse A Singly Linked List | The Ultimate Explanation (Iteratively &amp;amp; Recursively)&lt;/a&gt;, Benyam Ephrem, Back to Back SWE, 2018&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/jenshaw/reversing-an-integer-mathematically-fmg"&gt;Reversing an Integer Mathematically&lt;/a&gt;, Jenny Shaw, dev.to, 2019&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Other Helpful Videos&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=WwfhLC16bis&amp;amp;t=383s" rel="noopener noreferrer"&gt;Introduction to Linked List&lt;/a&gt;, CS Dojo&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=_jQhALI4ujg" rel="noopener noreferrer"&gt;Linked Lists&lt;/a&gt;, Computerphile&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=zQI3FyWm144" rel="noopener noreferrer"&gt;Singly-Linked Lists&lt;/a&gt;, CS50&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>todayilearned</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Reversing an Integer Mathematically</title>
      <dc:creator>Jenny Shaw</dc:creator>
      <pubDate>Wed, 09 Oct 2019 20:37:12 +0000</pubDate>
      <link>https://dev.to/jenshaw/reversing-an-integer-mathematically-fmg</link>
      <guid>https://dev.to/jenshaw/reversing-an-integer-mathematically-fmg</guid>
      <description>&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;This an algorithm problem I've encountered a couple of times called &lt;em&gt;Reverse the Integer&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Write a program or function called reverseInteger 
to reverse the order of digits of a given integer.

Input: 12345
Output: 54321
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, it appeared easy enough to figure out. One of the first problems I ever remember having to solve was &lt;em&gt;Reverse the String&lt;/em&gt;, and so, because the two seemed similar enough, I figured I could use the same approach for both.&lt;/p&gt;




&lt;h3&gt;
  
  
  Reverse the String
&lt;/h3&gt;

&lt;p&gt;Here's one solution to &lt;em&gt;Reverse the String&lt;/em&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reverseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;reversedStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// initialize variable with empty string&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;// iterate backwards through each character of str (input)&lt;/span&gt;
    &lt;span class="nx"&gt;reversedStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reversedStr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;  &lt;span class="c1"&gt;// add each character to end of reversedStr&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;reversedStr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// after completion of iterations, return reversedStr&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;reverseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// returns 'god' &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, I plan to return a variable called &lt;code&gt;reversedStr&lt;/code&gt; at the end of my function. First, I initialize it as an empty string, and as I iterate backwards through each character of &lt;code&gt;str&lt;/code&gt;, the original string input, I take that character to build up &lt;code&gt;reversedStr&lt;/code&gt; using concatenation. Almost like &lt;code&gt;.pop()&lt;/code&gt; and &lt;code&gt;.push()&lt;/code&gt; in an array situation.&lt;/p&gt;




&lt;h3&gt;
  
  
  Reverse the Integer (like a string)
&lt;/h3&gt;

&lt;p&gt;We &lt;em&gt;could&lt;/em&gt; reverse integers using a similar algorithm, but there are a couple of caveats: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;integers can't be iterated through, and&lt;/li&gt;
&lt;li&gt;digits can't be concatenated. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If our input for &lt;code&gt;reverseTheString()&lt;/code&gt; were an integer, the function would just spit back an empty string. Useless.&lt;/p&gt;

&lt;p&gt;To resolve this, we'd have to first convert the integer input into a string before iteration and concatenation. And if we're expected to return an integer in the end, we'd also have to convert the string &lt;em&gt;back&lt;/em&gt; into an integer before returning the value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reverseInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;numStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// &amp;lt;-- convert integer to string&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;reversedNumStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numStr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;reversedNumStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reversedNumStr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numStr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;reversedInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reversedNumStr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- convert string back to integer&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;reversedInt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// return a reversed integer&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;reverseInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12345&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// returns 54321&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I've never been very enthusiastic about reversing an integer like a string for a few reasons. &lt;/p&gt;

&lt;p&gt;Although this function certainly gets the job done for (most) integer inputs, I don't like having to go through the extra trouble of converting data types back and forth. I'd rather stick to just one data type. &lt;/p&gt;

&lt;p&gt;Also, we're asked to reverse &lt;em&gt;integers&lt;/em&gt;, yet we're largely manipulating strings, so this feels like a rather tangential approach, a little like a cheat. And I'm no cheater, so we're going to learn to do this right. &lt;/p&gt;




&lt;h3&gt;
  
  
  Reverse the Integer with Math
&lt;/h3&gt;

&lt;p&gt;Let's approach this problem instead in a way where we can still cleanly 'pop' and 'push' digits, do it all mathematically, and completely avoid the need to convert our integer into a string and back.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(By the way, if you're worried about the math, don't be. We're sticking with basic arithmetic here. Elementary school level stuff. If you understand subtraction, multiplication, division, and place values, then you've got this, kid.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Keep in mind that in this function, we'll be handling two variables. The first, &lt;code&gt;num&lt;/code&gt;, is the input from which we'll be 'popping' digits until there are none left. The second, &lt;code&gt;reversedInteger&lt;/code&gt;, will be our output. Here we'll be building up the reversed order of digits by 'pushing' on the 'popped' digits from &lt;code&gt;num&lt;/code&gt;. &lt;/p&gt;




&lt;h4&gt;
  
  
  Step 1:
&lt;/h4&gt;

&lt;p&gt;We'll start with the variable, &lt;code&gt;reversedInteger&lt;/code&gt;, and initialize its value at 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reverseIntegerWithMath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;reversedInteger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- initialize reversedInteger&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Step 2:
&lt;/h4&gt;

&lt;p&gt;We're going to start a while loop and continue it while &lt;code&gt;num&lt;/code&gt; still has a value greater than 0. Every loop, we'll be chipping away one digit from &lt;code&gt;num&lt;/code&gt; and using the digit to build &lt;code&gt;reversedInteger&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reverseIntegerWithMath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;reversedInteger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- open while loop&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;h4&gt;
  
  
  Step 3:
&lt;/h4&gt;

&lt;p&gt;At the beginning of each loop, we'll multiply &lt;code&gt;reversedInteger&lt;/code&gt; by 10.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reverseIntegerWithMath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;reversedInteger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;reversedInteger&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- set up for proper place value&lt;/span&gt;


  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Let's keep track of what num and reversedNumber look like &lt;/span&gt;
&lt;span class="c1"&gt;// starting from here...&lt;/span&gt;

&lt;span class="c1"&gt;// num =&amp;gt; 1234&lt;/span&gt;

&lt;span class="c1"&gt;// reversedInteger =&amp;gt; 0 * 10 &lt;/span&gt;
&lt;span class="c1"&gt;//                 =&amp;gt; 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Step 4:
&lt;/h4&gt;

&lt;p&gt;Now, let's take our &lt;code&gt;num&lt;/code&gt; and divide by 10 using the modulo operator. This is to find a single-digit remainder which equals the current last digit of &lt;code&gt;nums&lt;/code&gt;. We'll initialize a variable called &lt;code&gt;rem&lt;/code&gt; at the top of our function, and tuck that value safely into it.&lt;/p&gt;

&lt;p&gt;Then subtract &lt;code&gt;rem&lt;/code&gt; from &lt;code&gt;num&lt;/code&gt; and divide the result by 10. And now we're left with the same integer, but one digit less. &lt;/p&gt;

&lt;p&gt;POP!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reverseIntegerWithMath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;reversedInteger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;rem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// &amp;lt;-- initialize remainder&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;reversedInteger&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;rem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// &amp;lt;-- remainder grabs last digit&lt;/span&gt;
    &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;rem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// &amp;lt;-- eliminate zero in num&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// rem =&amp;gt; 1234 % 10 &lt;/span&gt;
&lt;span class="c1"&gt;//     =&amp;gt; 4&lt;/span&gt;

&lt;span class="c1"&gt;// num =&amp;gt; 1234 - rem &lt;/span&gt;
&lt;span class="c1"&gt;//     =&amp;gt; 1230 / 10&lt;/span&gt;
&lt;span class="c1"&gt;//     =&amp;gt; 123&lt;/span&gt;

&lt;span class="c1"&gt;// reversedInteger =&amp;gt; 0&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case you're curious...&lt;br&gt;
&lt;em&gt;Why are we dividing and multiplying numbers by 10?&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;It's because we're replicating decimal place values where each place has a value of times ten from right to left. &lt;br&gt;
Dividing by 10 eliminates the last zero in &lt;code&gt;num&lt;/code&gt;, which then gives us access to the next digit that ends up in the ones place. &lt;br&gt;
Multiplying &lt;code&gt;reversedInteger&lt;/code&gt; by 10 makes room in the ones place where we can place the digit we popped off from &lt;code&gt;num&lt;/code&gt;.&lt;/p&gt;


&lt;h4&gt;
  
  
  Step 5:
&lt;/h4&gt;

&lt;p&gt;Next, it's time to "push" the "popped" digit from &lt;code&gt;num&lt;/code&gt; by taking the remainder and adding it to &lt;code&gt;reversedInteger&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;PUSH!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reverseIntegerWithMath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;reversedInteger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;rem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;reversedInteger&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;rem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;rem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;reversedInteger&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// &amp;lt;-- 'push' remainder onto end of reversedInteger&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// rem =&amp;gt; 4&lt;/span&gt;

&lt;span class="c1"&gt;// num =&amp;gt; 123&lt;/span&gt;

&lt;span class="c1"&gt;// reversedInteger =&amp;gt; 0 + 4&lt;/span&gt;
&lt;span class="c1"&gt;//                 =&amp;gt; 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Step 6:
&lt;/h4&gt;

&lt;p&gt;We've completed one cycle of this process. Repeat until &lt;code&gt;num&lt;/code&gt;'s value dwindles to 0 and there are no more digits to 'pop' or 'push'. &lt;br&gt;
After the reversal of digits is complete, we can finally return &lt;code&gt;reversedInteger&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reverseIntegerWithMath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;reversedInteger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;rem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;reversedInteger&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;rem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;rem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;reversedInteger&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;reversedInteger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// &amp;lt;-- done!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// if you want to see what happens in the next loop&lt;/span&gt;
&lt;span class="c1"&gt;// num =&amp;gt; 123 - 3 (step 4)&lt;/span&gt;
&lt;span class="c1"&gt;//     =&amp;gt; 120 / 10&lt;/span&gt;
&lt;span class="c1"&gt;//     =&amp;gt; 12 [pops the 3 from original integer]&lt;/span&gt;

&lt;span class="c1"&gt;// rem =&amp;gt; 123 % 10 (step 3)&lt;/span&gt;
&lt;span class="c1"&gt;//     =&amp;gt; 3&lt;/span&gt;

&lt;span class="c1"&gt;// reversedInteger =&amp;gt; 4 * 10 (step 2)&lt;/span&gt;
&lt;span class="c1"&gt;//                 =&amp;gt; 40 + 3 (step 5)&lt;/span&gt;
&lt;span class="c1"&gt;//                 =&amp;gt; 43 [pushes the 3 onto reversedInteger]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;This is a pretty simple and neat trick in numeric manipulation and a much-improved approach to the &lt;em&gt;reverseInteger&lt;/em&gt; problem. I'm always looking for other creative ways to solve simple problems like this, so if you've got clever ones to share, drop them in the comments!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>todayilearned</category>
      <category>problemsolving</category>
    </item>
  </channel>
</rss>
