Binary Search Tree Data Structure — Overview

Left and Right

As the name implies “binary”, we will interact with YES or NO, 1 or 0, and TRUE and FALSE. In the binary search tree, we will interact with LEFT and RIGHT. If the inserted value is greater than the root, the value will be placed on the right side of the root. If smaller than the root, the value will be stored on the left side.

  • 40 is the root.
  • 30 is smaller than the root (40), place it on the left side.
  • 50 is greater than the root (40), place it on the right side.
  • 25 is smaller than the root (40), and there is a child (30) on the left side. 25 is smaller than 30, place it on the left side of 30.
  • 35 is smaller than the root (40), and there is a child (30) on the left side. 35 is greater than 30, place it on the right side of 30.
  • 45 is greater than the root (40), and there is a child (50) on the right side. 45 is smaller than 50, place it on the left side of 50.
  • 60 is greater than the root (40), and there is a child (50) on the right side. 60 is greater than 50, place it on the right side of 50.

Let’s code!

Given an array [30, 20, 40], we will change it to Binary Search Tree. Remember the logic! You need to focus on ROOT, LEFT, and RIGHT.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store