Skip to content

Instantly share code, notes, and snippets.

@nns2009
nns2009 / BinaryTreeSort.js
Last active June 13, 2024 09:36
Binary Tree Sort in 3 lines of code in JavaScript
// Watch https://youtu.be/KvYzGsz5vHA to see how I coded this with some commentary
let add = (n, v) => !n ? { v } : { ...n, [v < n.v]: add(n[v < n.v], v) }
let flat = n => !n ? [] : [...flat(n[!0]), n.v, ...flat(n[!1])]
let bsort = vs => flat(vs.reduce(add, null))
// 182 characters with nice formatting
let add=(n,v)=>!n?{v}:{...n,[v<n.v]:add(n[v<n.v],v)}
let flat=n=>!n?[]:[...flat(n[!0]),n.v,...flat(n[!1])]
let bsort=vs=>flat(vs.reduce(add,null))