Implementing a BST in JavaScript

Introduction

So far you have reasoned about binary search trees on paper — drawing nodes, following edges, and tracing where a value belongs. This topic turns that reasoning into working code. You will define a Node class that stores a value plus left and right references, wrap it in a BinarySearchTree class that tracks the root, and then add methods one at a time: insert, search (lookup), the three traversals, and finally delete with its three cases.

This matters because the code forces you to be precise in ways a diagram does not. Where does a new node actually get attached? What does your method return when the tree is empty, or when the key is not found? How do you reconnect a subtree after removing a node with two children? Writing the implementation yourself is the fastest way to discover which parts of the concept you only half understood.

The videos below are from a Java-based DSA series, and that is deliberate: the class structure, the recursive insert, and the recursive search translate almost line for line into JavaScript. While watching, focus on the shape of the logic rather than the syntax — the base case of each recursion, the comparison that chooses left versus right, and the fact that each recursive call returns a subtree reference that the parent must reassign. Then rewrite each method in JavaScript yourself, replacing class fields and types with plain JavaScript properties, and test it on a small tree you build by hand.

Video 1: How to represent Binary Search Tree in Java? | Animation

Dinesh Varyani · 10:07 · 17,329 views · 5y ago · captions — Watch on YouTube

About this video

An animated walkthrough of how a binary search tree is represented in code: the node object holding a value and left/right child references, and the tree class that holds a reference to the root. Expect visual diagrams showing how individual node objects link together to form the overall tree structure in memory.

Why this video

This maps directly onto the first two subtopics — defining the Node class and setting up the BinarySearchTree class — and the animation makes the reference-based structure concrete before any method logic is introduced.

Video 2: How to insert a value in a Binary Search Tree (Recursive) | Implementation

Dinesh Varyani · 7:23 · 16,176 views · 5y ago · captions — Watch on YouTube

About this video

Covers the recursive implementation of inserting a value into a BST: comparing the new key against the current node, descending left or right accordingly, and creating the node when an empty position is reached. The implementation is shown step by step alongside an animated view of the tree growing.

Why this video

Insertion is the method every other operation depends on, and this video shows the recursive pattern — including returning the subtree back up to the caller — that you will reuse when writing insert and delete in JavaScript.

Video 3: How to search a given key in a Binary Search Tree (Recursive) | Implementation

Dinesh Varyani · 6:40 · 8,451 views · 5y ago · captions — Watch on YouTube

About this video

Walks through recursively searching a BST for a given key: checking the current node, returning when a match or an empty reference is found, and otherwise recursing into the left or right subtree based on the comparison. The animation illustrates the single downward path the search follows.

Why this video

It covers the search/lookup subtopic with the same recursive structure as the insert video, reinforcing the pattern and making clear why a BST search touches only one path from root to leaf.

How These Videos Support the Learning Outcomes

These three videos directly support MO5 by modeling the exact building blocks of a BST implementation — node and tree class definitions, a recursive insert, and a recursive search — in a form you can translate into JavaScript and validate with your own test cases. They also reinforce MO1, since representing a node with value plus left and right references makes the structural vocabulary of roots, children, and leaves concrete, and MO3, by demonstrating insertion and search logic that preserves the ordering property. Because the videos stop short of deletion and traversal, you should pair them with the module's course materials and your own coding practice to complete the delete cases (MO3), the in-order, pre-order, and post-order methods (MO2), and the complexity analysis that ties tree height to performance (MO4).

About These Videos

These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.