Binary data structures: an intro to trees and heaps in JavaScript

Yung L. Leung
Mar 20 · 4 min read
Photo by Keith Wickramasekara on Unsplash

Trees

source
A root branches off into two parents of leaves. Leaves (green) are nodes without children.
Insertion of the node with a value of 6
Finding the node with a value of 6
Breadth First Search returns [3, 1, 5, 0, 2, 4, 6]
Depth First Search Pre — Order returns [3, 1, 0, 2, 5, 4, 6]
n = 3

Heaps

Photo by Nick Tong on Unsplash
Max Binary Heap
Insertion of a node with a value of 6 & bubbling it upward.
Extraction of the max node with the value of 6 & sinking down node with the value of 3.
Bubbling up of the new node with value 6
Sinking down of node with value 1

Reference:

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead

Yung L. Leung

Written by

Developer with a passion for clean code & good UI/UX design. Inspired by the life of Jobs & Wozniak & realized through solving puzzles & building apps.

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead