deno.land / std@0.224.0 / data_structures / red_black_tree_test.ts
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assertEquals, assertStrictEquals } from "../assert/mod.ts";import { RedBlackTree } from "./red_black_tree.ts";import { ascend, descend } from "./comparators.ts";import { type Container, MyMath } from "./_test_utils.ts";
Deno.test("RedBlackTree works as expected with default ascend comparator", () => { const trees = [ new RedBlackTree(), new RedBlackTree(), ] as const; const values: number[] = [-10, 9, -1, 100, 1, 0, -100, 10, -9];
const expectedMin: number[][] = [ [-10, -10, -10, -10, -10, -10, -100, -100, -100], [-9, -9, -100, -100, -100, -100, -100, -100, -100], ]; const expectedMax: number[][] = [ [-10, 9, 9, 100, 100, 100, 100, 100, 100], [-9, 10, 10, 10, 10, 100, 100, 100, 100], ]; for (const [i, tree] of trees.entries()) { assertEquals(tree.size, 0); assertEquals(tree.isEmpty(), true); for (const [j, value] of values.entries()) { assertEquals(tree.find(value), null); assertEquals(tree.insert(value), true); assertEquals(tree.find(value), value); assertEquals(tree.size, j + 1); assertEquals(tree.isEmpty(), false); assertEquals(tree.min(), expectedMin?.[i]?.[j]); assertEquals(tree.max(), expectedMax?.[i]?.[j]); } for (const value of values) { assertEquals(tree.insert(value), false); assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false); assertEquals(tree.min(), -100); assertEquals(tree.max(), 100); } values.reverse(); }
for (const tree of trees) { assertEquals( [...tree.lnrValues()], [-100, -10, -9, -1, 0, 1, 9, 10, 100], ); assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false);
assertEquals( [...tree], [-100, -10, -9, -1, 0, 1, 9, 10, 100], ); assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false);
assertEquals( [...tree.rnlValues()], [100, 10, 9, 1, 0, -1, -9, -10, -100], ); assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false); }
assertEquals( [...trees[0].nlrValues()], [-1, -10, -100, -9, 9, 1, 0, 100, 10], ); assertEquals( [...trees[1].nlrValues()], [-9, -100, -10, 1, 0, -1, 10, 9, 100], ); for (const tree of trees) { assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false); }
assertEquals( [...trees[0].lrnValues()], [-100, -9, -10, 0, 1, 10, 100, 9, -1], ); assertEquals( [...trees[1].lrnValues()], [-10, -100, -1, 0, 9, 100, 10, 1, -9], ); for (const tree of trees) { assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false); }
assertEquals( [...trees[0].lvlValues()], [-1, -10, 9, -100, -9, 1, 100, 0, 10], ); assertEquals( [...trees[1].lvlValues()], [-9, -100, 1, -10, 0, 10, -1, 9, 100], ); for (const tree of trees) { assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false); }
for (const tree of trees) { const expected: number[] = [-100, -10, -9, -1, 0, 1, 9, 10, 100]; for (const [j, value] of values.entries()) { assertEquals(tree.size, values.length - j); assertEquals(tree.isEmpty(), false); assertEquals(tree.find(value), value);
assertEquals(tree.remove(value), true); expected.splice(expected.indexOf(value), 1); assertEquals([...tree], expected); assertEquals(tree.find(value), null);
assertEquals(tree.remove(value), false); assertEquals([...tree], expected); assertEquals(tree.find(value), null); } assertEquals(tree.size, 0); assertEquals(tree.isEmpty(), true); }});
Deno.test("RedBlackTree works as exepcted with descend comparator", () => { const trees = [ new RedBlackTree(descend), new RedBlackTree(descend), ] as const; const values: number[] = [-10, 9, -1, 100, 1, 0, -100, 10, -9];
const expectedMin: number[][] = [ [-10, 9, 9, 100, 100, 100, 100, 100, 100], [-9, 10, 10, 10, 10, 100, 100, 100, 100, 100], ]; const expectedMax: number[][] = [ [-10, -10, -10, -10, -10, -10, -100, -100, -100], [-9, -9, -100, -100, -100, -100, -100, -100, -100], ]; for (const [i, tree] of trees.entries()) { assertEquals(tree.size, 0); assertEquals(tree.isEmpty(), true); for (const [j, value] of values.entries()) { assertEquals(tree.find(value), null); assertEquals(tree.insert(value), true); assertEquals(tree.find(value), value); assertEquals(tree.size, j + 1); assertEquals(tree.isEmpty(), false); assertEquals(tree.min(), expectedMin?.[i]?.[j]); assertEquals(tree.max(), expectedMax?.[i]?.[j]); } for (const value of values) { assertEquals(tree.insert(value), false); assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false); assertEquals(tree.min(), 100); assertEquals(tree.max(), -100); } values.reverse(); }
for (const tree of trees) { assertEquals( [...tree.lnrValues()], [100, 10, 9, 1, 0, -1, -9, -10, -100], ); assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false);
assertEquals( [...tree], [100, 10, 9, 1, 0, -1, -9, -10, -100], ); assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false);
assertEquals( [...tree.rnlValues()], [-100, -10, -9, -1, 0, 1, 9, 10, 100], ); assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false); }
assertEquals( [...trees[0].nlrValues()], [-1, 9, 100, 10, 1, 0, -10, -9, -100], ); assertEquals( [...trees[1].nlrValues()], [-9, 1, 10, 100, 9, 0, -1, -100, -10], ); for (const tree of trees) { assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false); }
assertEquals( [...trees[0].lrnValues()], [10, 100, 0, 1, 9, -9, -100, -10, -1], ); assertEquals( [...trees[1].lrnValues()], [100, 9, 10, -1, 0, 1, -10, -100, -9], ); for (const tree of trees) { assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false); }
assertEquals( [...trees[0].lvlValues()], [-1, 9, -10, 100, 1, -9, -100, 10, 0], ); assertEquals( [...trees[1].lvlValues()], [-9, 1, -100, 10, 0, -10, 100, 9, -1], ); for (const tree of trees) { assertEquals(tree.size, values.length); assertEquals(tree.isEmpty(), false); }
for (const tree of trees) { const expected: number[] = [100, 10, 9, 1, 0, -1, -9, -10, -100]; for (const [j, value] of values.entries()) { assertEquals(tree.size, values.length - j); assertEquals(tree.isEmpty(), false); assertEquals(tree.find(value), value);
assertEquals(tree.remove(value), true); expected.splice(expected.indexOf(value), 1); assertEquals([...tree], expected); assertEquals(tree.find(value), null);
assertEquals(tree.remove(value), false); assertEquals([...tree], expected); assertEquals(tree.find(value), null); } assertEquals(tree.size, 0); assertEquals(tree.isEmpty(), true); }});
Deno.test("RedBlackTree works with object items", () => { const tree: RedBlackTree<Container> = new RedBlackTree(( a: Container, b: Container, ) => ascend(a.id, b.id)); const ids: number[] = [-10, 9, -1, 100, 1, 0, -100, 10, -9];
for (const [i, id] of ids.entries()) { const newContainer: Container = { id, values: [] }; assertEquals(tree.find(newContainer), null); assertEquals(tree.insert(newContainer), true); newContainer.values.push(i - 1, i, i + 1); assertStrictEquals(tree.find({ id, values: [] }), newContainer); assertEquals(tree.size, i + 1); assertEquals(tree.isEmpty(), false); } for (const [i, id] of ids.entries()) { const newContainer: Container = { id, values: [] }; assertEquals( tree.find({ id } as Container), { id, values: [i - 1, i, i + 1] }, ); assertEquals(tree.insert(newContainer), false); assertEquals( tree.find({ id, values: [] }), { id, values: [i - 1, i, i + 1] }, ); assertEquals(tree.size, ids.length); assertEquals(tree.isEmpty(), false); }
assertEquals( [...tree].map((container) => container.id), [-100, -10, -9, -1, 0, 1, 9, 10, 100], ); assertEquals(tree.size, ids.length); assertEquals(tree.isEmpty(), false);
const expected: number[] = [-100, -10, -9, -1, 0, 1, 9, 10, 100]; for (const [i, id] of ids.entries()) { assertEquals(tree.size, ids.length - i); assertEquals(tree.isEmpty(), false); assertEquals( tree.find({ id, values: [] }), { id, values: [i - 1, i, i + 1] }, );
assertEquals(tree.remove({ id, values: [] }), true); expected.splice(expected.indexOf(id), 1); assertEquals([...tree].map((container) => container.id), expected); assertEquals(tree.find({ id, values: [] }), null);
assertEquals(tree.remove({ id, values: [] }), false); assertEquals([...tree].map((container) => container.id), expected); assertEquals(tree.find({ id, values: [] }), null); } assertEquals(tree.size, 0); assertEquals(tree.isEmpty(), true);});
Deno.test("RedBlackTree.from() handles Iterable", () => { const values: number[] = [-10, 9, -1, 100, 9, 1, 0, 9, -100, 10, -9]; const originalValues: number[] = Array.from(values); const expected: number[] = [-100, -10, -9, -1, 0, 1, 9, 10, 100]; let tree: RedBlackTree<number> = RedBlackTree.from(values); assertEquals(values, originalValues); assertEquals([...tree], expected); assertEquals([...tree.nlrValues()], [-1, -10, -100, -9, 9, 1, 0, 100, 10]); assertEquals([...tree.lvlValues()], [-1, -10, 9, -100, -9, 1, 100, 0, 10]);
tree = RedBlackTree.from(values, { compare: descend }); assertEquals(values, originalValues); assertEquals([...tree].reverse(), expected); assertEquals([...tree.nlrValues()], [-1, 9, 100, 10, 1, 0, -10, -9, -100]); assertEquals([...tree.lvlValues()], [-1, 9, -10, 100, 1, -9, -100, 10, 0]);
tree = RedBlackTree.from(values, { map: (v: number) => 2 * v, }); assertEquals([...tree], expected.map((v: number) => 2 * v)); assertEquals([...tree.nlrValues()], [-2, -20, -200, -18, 18, 2, 0, 200, 20]); assertEquals([...tree.lvlValues()], [-2, -20, 18, -200, -18, 2, 200, 0, 20]);
const math = new MyMath(); tree = RedBlackTree.from(values, { map: function (this: MyMath, v: number) { return this.multiply(3, v); }, thisArg: math, }); assertEquals(values, originalValues); assertEquals([...tree], expected.map((v: number) => 3 * v)); assertEquals([...tree.nlrValues()], [-3, -30, -300, -27, 27, 3, 0, 300, 30]); assertEquals([...tree.lvlValues()], [-3, -30, 27, -300, -27, 3, 300, 0, 30]);
tree = RedBlackTree.from(values, { compare: descend, map: (v: number) => 2 * v, }); assertEquals(values, originalValues); assertEquals([...tree].reverse(), expected.map((v: number) => 2 * v)); assertEquals([...tree.nlrValues()], [-2, 18, 200, 20, 2, 0, -20, -18, -200]); assertEquals([...tree.lvlValues()], [-2, 18, -20, 200, 2, -18, -200, 20, 0]);
tree = RedBlackTree.from(values, { compare: descend, map: function (this: MyMath, v: number) { return this.multiply(3, v); }, thisArg: math, }); assertEquals(values, originalValues); assertEquals([...tree].reverse(), expected.map((v: number) => 3 * v)); assertEquals([...tree.nlrValues()], [-3, 27, 300, 30, 3, 0, -30, -27, -300]); assertEquals([...tree.lvlValues()], [-3, 27, -30, 300, 3, -27, -300, 30, 0]);});
Deno.test("RedBlackTree.from() handles default ascend comparator", () => { const values: number[] = [-10, 9, -1, 100, 9, 1, 0, 9, -100, 10, -9]; const expected: number[] = [-100, -10, -9, -1, 0, 1, 9, 10, 100]; const originalTree: RedBlackTree<number> = new RedBlackTree(); for (const value of values) originalTree.insert(value); let tree: RedBlackTree<number> = RedBlackTree.from(originalTree); assertEquals([...originalTree], expected); assertEquals([...tree], expected); assertEquals([...tree.nlrValues()], [...originalTree.nlrValues()]); assertEquals([...tree.lvlValues()], [...originalTree.lvlValues()]);
tree = RedBlackTree.from(originalTree, { compare: descend }); assertEquals([...originalTree], expected); assertEquals([...tree].reverse(), expected); assertEquals([...tree.nlrValues()], [-1, 1, 10, 100, 9, 0, -10, -9, -100]); assertEquals([...tree.lvlValues()], [-1, 1, -10, 10, 0, -9, -100, 100, 9]);
tree = RedBlackTree.from(originalTree, { map: (v: number) => 2 * v, }); assertEquals([...originalTree], expected); assertEquals([...tree], expected.map((v: number) => 2 * v)); assertEquals([...tree.nlrValues()], [-2, -20, -200, -18, 2, 0, 20, 18, 200]); assertEquals([...tree.lvlValues()], [-2, -20, 2, -200, -18, 0, 20, 18, 200]);
const math = new MyMath(); tree = RedBlackTree.from(originalTree, { map: function (this: MyMath, v: number) { return this.multiply(3, v); }, thisArg: math, }); assertEquals([...originalTree], expected); assertEquals([...tree], expected.map((v: number) => 3 * v)); assertEquals([...tree.nlrValues()], [-3, -30, -300, -27, 3, 0, 30, 27, 300]); assertEquals([...tree.lvlValues()], [-3, -30, 3, -300, -27, 0, 30, 27, 300]);
tree = RedBlackTree.from(originalTree, { compare: descend, map: (v: number) => 2 * v, }); assertEquals([...originalTree], expected); assertEquals([...tree].reverse(), expected.map((v: number) => 2 * v)); assertEquals([...tree.nlrValues()], [-2, 2, 20, 200, 18, 0, -20, -18, -200]); assertEquals([...tree.lvlValues()], [-2, 2, -20, 20, 0, -18, -200, 200, 18]);
tree = RedBlackTree.from(originalTree, { compare: descend, map: function (this: MyMath, v: number) { return this.multiply(3, v); }, thisArg: math, }); assertEquals([...originalTree], expected); assertEquals([...tree].reverse(), expected.map((v: number) => 3 * v)); assertEquals([...tree.nlrValues()], [-3, 3, 30, 300, 27, 0, -30, -27, -300]); assertEquals([...tree.lvlValues()], [-3, 3, -30, 30, 0, -27, -300, 300, 27]);});
Deno.test("RedBlackTree.from() handles descend comparator", () => { const values: number[] = [-10, 9, -1, 100, 9, 1, 0, 9, -100, 10, -9]; const expected: number[] = [100, 10, 9, 1, 0, -1, -9, -10, -100]; const originalTree: RedBlackTree<number> = new RedBlackTree(descend); for (const value of values) originalTree.insert(value); let tree: RedBlackTree<number> = RedBlackTree.from(originalTree); assertEquals([...originalTree], expected); assertEquals([...tree], expected); assertEquals([...tree.nlrValues()], [...originalTree.nlrValues()]); assertEquals([...tree.lvlValues()], [...originalTree.lvlValues()]);
tree = RedBlackTree.from(originalTree, { compare: ascend }); assertEquals([...originalTree], expected); assertEquals([...tree].reverse(), expected); assertEquals([...tree.nlrValues()], [1, -1, -10, -100, -9, 0, 10, 9, 100]); assertEquals([...tree.lvlValues()], [1, -1, 10, -10, 0, 9, 100, -100, -9]);
tree = RedBlackTree.from(originalTree, { map: (v: number) => 2 * v, }); assertEquals([...originalTree], expected); assertEquals([...tree], expected.map((v: number) => 2 * v)); assertEquals([...tree.nlrValues()], [2, 20, 200, 18, -2, 0, -20, -18, -200]); assertEquals([...tree.lvlValues()], [2, 20, -2, 200, 18, 0, -20, -18, -200]);
const math = new MyMath(); tree = RedBlackTree.from(originalTree, { map: function (this: MyMath, v: number) { return this.multiply(3, v); }, thisArg: math, }); assertEquals([...originalTree], expected); assertEquals([...tree], expected.map((v: number) => 3 * v)); assertEquals([...tree.nlrValues()], [3, 30, 300, 27, -3, 0, -30, -27, -300]); assertEquals([...tree.lvlValues()], [3, 30, -3, 300, 27, 0, -30, -27, -300]);
tree = RedBlackTree.from(originalTree, { compare: ascend, map: (v: number) => 2 * v, }); assertEquals([...originalTree], expected); assertEquals([...tree].reverse(), expected.map((v: number) => 2 * v)); assertEquals([...tree.nlrValues()], [2, -2, -20, -200, -18, 0, 20, 18, 200]); assertEquals([...tree.lvlValues()], [2, -2, 20, -20, 0, 18, 200, -200, -18]);
tree = RedBlackTree.from(originalTree, { compare: ascend, map: function (this: MyMath, v: number) { return this.multiply(3, v); }, thisArg: math, }); assertEquals([...originalTree], expected); assertEquals([...tree].reverse(), expected.map((v: number) => 3 * v)); assertEquals([...tree.nlrValues()], [3, -3, -30, -300, -27, 0, 30, 27, 300]); assertEquals([...tree.lvlValues()], [3, -3, 30, -30, 0, 27, 300, -300, -27]);});
Deno.test("RedBlackTree() inserts rebalance left", () => { let values: number[] = [8, 4, 10, 0, 6, 11, -2, 2]; let tree: RedBlackTree<number> = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [8, 4, 0, -2, 2, 6, 10, 11]); assertEquals([...tree.lvlValues()], [8, 4, 10, 0, 6, 11, -2, 2]); assertEquals(tree.insert(-3), true); assertEquals([...tree.nlrValues()], [4, 0, -2, -3, 2, 8, 6, 10, 11]); assertEquals([...tree.lvlValues()], [4, 0, 8, -2, 2, 6, 10, -3, 11]);
tree = RedBlackTree.from(values); assertEquals(tree.insert(-1), true); assertEquals([...tree.nlrValues()], [4, 0, -2, -1, 2, 8, 6, 10, 11]); assertEquals([...tree.lvlValues()], [4, 0, 8, -2, 2, 6, 10, -1, 11]);
tree = RedBlackTree.from(values); assertEquals(tree.insert(1), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 1, 8, 6, 10, 11]); assertEquals([...tree.lvlValues()], [4, 0, 8, -2, 2, 6, 10, 1, 11]);
tree = RedBlackTree.from(values); assertEquals(tree.insert(3), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 3, 8, 6, 10, 11]); assertEquals([...tree.lvlValues()], [4, 0, 8, -2, 2, 6, 10, 3, 11]);
values = [4, -4, 6, -5, 0, 7, -2, 2]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [4, -4, -5, 0, -2, 2, 6, 7]); assertEquals([...tree.lvlValues()], [4, -4, 6, -5, 0, 7, -2, 2]); assertEquals(tree.insert(-3), true); assertEquals([...tree.nlrValues()], [0, -4, -5, -2, -3, 4, 2, 6, 7]); assertEquals([...tree.lvlValues()], [0, -4, 4, -5, -2, 2, 6, -3, 7]);
tree = RedBlackTree.from(values); assertEquals(tree.insert(-1), true); assertEquals([...tree.nlrValues()], [0, -4, -5, -2, -1, 4, 2, 6, 7]); assertEquals([...tree.lvlValues()], [0, -4, 4, -5, -2, 2, 6, -1, 7]);
tree = RedBlackTree.from(values); assertEquals(tree.insert(1), true); assertEquals([...tree.nlrValues()], [0, -4, -5, -2, 4, 2, 1, 6, 7]); assertEquals([...tree.lvlValues()], [0, -4, 4, -5, -2, 2, 6, 1, 7]);
tree = RedBlackTree.from(values); assertEquals(tree.insert(3), true); assertEquals([...tree.nlrValues()], [0, -4, -5, -2, 4, 2, 3, 6, 7]); assertEquals([...tree.lvlValues()], [0, -4, 4, -5, -2, 2, 6, 3, 7]);});
Deno.test("RedBlackTree() inserts rebalance right", () => { let values: number[] = [-4, -6, 4, 0, 6, -7, -2, 2]; let tree: RedBlackTree<number> = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [-4, -6, -7, 4, 0, -2, 2, 6]); assertEquals([...tree.lvlValues()], [-4, -6, 4, -7, 0, 6, -2, 2]); assertEquals(tree.insert(-3), true); assertEquals([...tree.nlrValues()], [0, -4, -6, -7, -2, -3, 4, 2, 6]); assertEquals([...tree.lvlValues()], [0, -4, 4, -6, -2, 2, 6, -7, -3]);
tree = RedBlackTree.from(values); assertEquals(tree.insert(-1), true); assertEquals([...tree.nlrValues()], [0, -4, -6, -7, -2, -1, 4, 2, 6]); assertEquals([...tree.lvlValues()], [0, -4, 4, -6, -2, 2, 6, -7, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.insert(1), true); assertEquals([...tree.nlrValues()], [0, -4, -6, -7, -2, 4, 2, 1, 6]); assertEquals([...tree.lvlValues()], [0, -4, 4, -6, -2, 2, 6, -7, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.insert(3), true); assertEquals([...tree.nlrValues()], [0, -4, -6, -7, -2, 4, 2, 3, 6]); assertEquals([...tree.lvlValues()], [0, -4, 4, -6, -2, 2, 6, -7, 3]);
values = [-8, -10, -4, -11, -6, 0, -2, 2]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [-8, -10, -11, -4, -6, 0, -2, 2]); assertEquals([...tree.lvlValues()], [-8, -10, -4, -11, -6, 0, -2, 2]); assertEquals(tree.insert(-3), true); assertEquals([...tree.nlrValues()], [-4, -8, -10, -11, -6, 0, -2, -3, 2]); assertEquals([...tree.lvlValues()], [-4, -8, 0, -10, -6, -2, 2, -11, -3]);
tree = RedBlackTree.from(values); assertEquals(tree.insert(-1), true); assertEquals([...tree.nlrValues()], [-4, -8, -10, -11, -6, 0, -2, -1, 2]); assertEquals([...tree.lvlValues()], [-4, -8, 0, -10, -6, -2, 2, -11, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.insert(1), true); assertEquals([...tree.nlrValues()], [-4, -8, -10, -11, -6, 0, -2, 2, 1]); assertEquals([...tree.lvlValues()], [-4, -8, 0, -10, -6, -2, 2, -11, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.insert(3), true); assertEquals([...tree.nlrValues()], [-4, -8, -10, -11, -6, 0, -2, 2, 3]); assertEquals([...tree.lvlValues()], [-4, -8, 0, -10, -6, -2, 2, -11, 3]);});
Deno.test("RedBlackTree removes rebalance root", () => { let values: number[] = [0]; let tree: RedBlackTree<number> = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [0]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], []);
values = [0, -1, 1]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [0, -1, 1]); assertEquals([...tree.lvlValues()], [0, -1, 1]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [1, -1]); assertEquals([...tree.lvlValues()], [1, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-1), true); assertEquals([...tree.nlrValues()], [0, 1]); assertEquals([...tree.lvlValues()], [0, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(1), true); assertEquals([...tree.nlrValues()], [0, -1]); assertEquals([...tree.lvlValues()], [0, -1]);
values = [0, -2, 2, -3, -1, 1, 3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [0, -2, -3, -1, 2, 1, 3]); assertEquals([...tree.lvlValues()], [0, -2, 2, -3, -1, 1, 3]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [1, -2, -3, -1, 2, 3]); assertEquals([...tree.lvlValues()], [1, -2, 2, -3, -1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [0, -1, -3, 2, 1, 3]); assertEquals([...tree.lvlValues()], [0, -1, 2, -3, 1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [0, -2, -3, -1, 3, 1]); assertEquals([...tree.lvlValues()], [0, -2, 3, -3, -1, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals([...tree.nlrValues()], [0, -2, -1, 2, 1, 3]); assertEquals([...tree.lvlValues()], [0, -2, 2, -1, 1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-1), true); assertEquals([...tree.nlrValues()], [0, -2, -3, 2, 1, 3]); assertEquals([...tree.lvlValues()], [0, -2, 2, -3, 1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(1), true); assertEquals([...tree.nlrValues()], [0, -2, -3, -1, 2, 3]); assertEquals([...tree.lvlValues()], [0, -2, 2, -3, -1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals([...tree.nlrValues()], [0, -2, -3, -1, 2, 1]); assertEquals([...tree.lvlValues()], [0, -2, 2, -3, -1, 1]);
values = [0, -2, 2, -3, -1]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [0, -2, -3, -1, 2]); assertEquals([...tree.lvlValues()], [0, -2, 2, -3, -1]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-2, -3, 2, -1]); assertEquals([...tree.lvlValues()], [-2, -3, 2, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [0, -1, -3, 2]); assertEquals([...tree.lvlValues()], [0, -1, 2, -3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [-2, -3, 0, -1]); assertEquals([...tree.lvlValues()], [-2, -3, 0, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals([...tree.nlrValues()], [0, -2, -1, 2]); assertEquals([...tree.lvlValues()], [0, -2, 2, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-1), true); assertEquals([...tree.nlrValues()], [0, -2, -3, 2]); assertEquals([...tree.lvlValues()], [0, -2, 2, -3]);
values = [0, -2, 2, 1, 3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [0, -2, 2, 1, 3]); assertEquals([...tree.lvlValues()], [0, -2, 2, 1, 3]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [1, -2, 2, 3]); assertEquals([...tree.lvlValues()], [1, -2, 2, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [2, 0, 1, 3]); assertEquals([...tree.lvlValues()], [2, 0, 3, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [0, -2, 3, 1]); assertEquals([...tree.lvlValues()], [0, -2, 3, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(1), true); assertEquals([...tree.nlrValues()], [0, -2, 2, 3]); assertEquals([...tree.lvlValues()], [0, -2, 2, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals([...tree.nlrValues()], [0, -2, 2, 1]); assertEquals([...tree.lvlValues()], [0, -2, 2, 1]);
values = [0, -2, 2, -3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [0, -2, -3, 2]); assertEquals([...tree.lvlValues()], [0, -2, 2, -3]); assertEquals(tree.remove(-3), true); assertEquals([...tree.nlrValues()], [0, -2, 2]); assertEquals([...tree.lvlValues()], [0, -2, 2]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [2, -2]); assertEquals([...tree.lvlValues()], [2, -2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [0, 2]); assertEquals([...tree.lvlValues()], [0, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [0, -2]); assertEquals([...tree.lvlValues()], [0, -2]);
values = [0, -2, 2, 3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [0, -2, 2, 3]); assertEquals([...tree.lvlValues()], [0, -2, 2, 3]); assertEquals(tree.remove(3), true); assertEquals([...tree.nlrValues()], [0, -2, 2]); assertEquals([...tree.lvlValues()], [0, -2, 2]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [2, -2]); assertEquals([...tree.lvlValues()], [2, -2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [0, 2]); assertEquals([...tree.lvlValues()], [0, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [0, -2]); assertEquals([...tree.lvlValues()], [0, -2]);
values = [0, -2, 2, -3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [0, -2, -3, 2]); assertEquals([...tree.lvlValues()], [0, -2, 2, -3]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-2, -3, 2]); assertEquals([...tree.lvlValues()], [-2, -3, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [0, -3, 2]); assertEquals([...tree.lvlValues()], [0, -3, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [-2, -3, 0]); assertEquals([...tree.lvlValues()], [-2, -3, 0]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals([...tree.nlrValues()], [0, -2, 2]); assertEquals([...tree.lvlValues()], [0, -2, 2]);
values = [0, -2, 2, -1]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [0, -2, -1, 2]); assertEquals([...tree.lvlValues()], [0, -2, 2, -1]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-1, -2, 2]); assertEquals([...tree.lvlValues()], [-1, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [0, -1, 2]); assertEquals([...tree.lvlValues()], [0, -1, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [-1, -2, 0]); assertEquals([...tree.lvlValues()], [-1, -2, 0]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-1), true); assertEquals([...tree.nlrValues()], [0, -2, 2]); assertEquals([...tree.lvlValues()], [0, -2, 2]);
values = [0, -2, 2, 1]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [0, -2, 2, 1]); assertEquals([...tree.lvlValues()], [0, -2, 2, 1]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [1, -2, 2]); assertEquals([...tree.lvlValues()], [1, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [1, 0, 2]); assertEquals([...tree.lvlValues()], [1, 0, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [0, -2, 1]); assertEquals([...tree.lvlValues()], [0, -2, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(1), true); assertEquals([...tree.nlrValues()], [0, -2, 2]); assertEquals([...tree.lvlValues()], [0, -2, 2]);
values = [0, -2, 2, 3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [0, -2, 2, 3]); assertEquals([...tree.lvlValues()], [0, -2, 2, 3]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [2, -2, 3]); assertEquals([...tree.lvlValues()], [2, -2, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [2, 0, 3]); assertEquals([...tree.lvlValues()], [2, 0, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [0, -2, 3]); assertEquals([...tree.lvlValues()], [0, -2, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals([...tree.nlrValues()], [0, -2, 2]); assertEquals([...tree.lvlValues()], [0, -2, 2]);});
Deno.test("RedBlackTree removes rebalance left", () => { let values = [4, 5, 0]; let tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [4, 0, 5]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [4, 5]);
values = [4, 5, 0, -1, 1]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [4, 0, -1, 1, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -1, 1]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [4, 1, -1, 5]); assertEquals([...tree.lvlValues()], [4, 1, 5, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-1), true); assertEquals([...tree.nlrValues()], [4, 0, 1, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(1), true); assertEquals([...tree.nlrValues()], [4, 0, -1, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -1]);
values = [4, 5, 0, -2, 2, -3, -1]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [4, 0, -2, -3, -1, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, -3, -1]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [4, -2, -3, 2, -1, 5]); assertEquals([...tree.lvlValues()], [4, -2, 5, -3, 2, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [4, 0, -1, -3, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -1, 2, -3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [4, -2, -3, 0, -1, 5]); assertEquals([...tree.lvlValues()], [4, -2, 5, -3, 0, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals([...tree.nlrValues()], [4, 0, -2, -1, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-1), true); assertEquals([...tree.nlrValues()], [4, 0, -2, -3, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, -3]);
values = [4, 5, 0, -2, 2, 1, 3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 1, 3, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, 1, 3]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [4, 1, -2, 2, 3, 5]); assertEquals([...tree.lvlValues()], [4, 1, 5, -2, 2, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [4, 2, 0, 1, 3, 5]); assertEquals([...tree.lvlValues()], [4, 2, 5, 0, 3, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 3, 1, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 3, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(1), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 3, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 1, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, 1]);
values = [4, 5, 0, -2, 2, -3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [4, 0, -2, -3, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, -3]); assertEquals(tree.remove(-3), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [4, 2, -2, 5]); assertEquals([...tree.lvlValues()], [4, 2, 5, -2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [4, 0, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals(tree.remove(4), true); assertEquals([...tree.nlrValues()], [0, -2, 5, 2]); assertEquals([...tree.lvlValues()], [0, -2, 5, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals(tree.remove(5), true); assertEquals([...tree.nlrValues()], [0, -2, 4, 2]); assertEquals([...tree.lvlValues()], [0, -2, 4, 2]);
values = [4, 5, 0, -2, 2, 3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 3, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, 3]); assertEquals(tree.remove(3), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [4, 2, -2, 5]); assertEquals([...tree.lvlValues()], [4, 2, 5, -2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [4, 0, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals(tree.remove(4), true); assertEquals([...tree.nlrValues()], [0, -2, 5, 2]); assertEquals([...tree.lvlValues()], [0, -2, 5, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals(tree.remove(5), true); assertEquals([...tree.nlrValues()], [0, -2, 4, 2]); assertEquals([...tree.lvlValues()], [0, -2, 4, 2]);
values = [4, 5, 0, -2, 2, -3, -1, 1, 3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [4, 0, -2, -3, -1, 2, 1, 3, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, -3, -1, 1, 3]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [4, 1, -2, -3, -1, 2, 3, 5]); assertEquals([...tree.lvlValues()], [4, 1, 5, -2, 2, -3, -1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [4, 0, -1, -3, 2, 1, 3, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -1, 2, -3, 1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [4, 0, -2, -3, -1, 3, 1, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 3, -3, -1, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals([...tree.nlrValues()], [4, 0, -2, -1, 2, 1, 3, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, -1, 1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-1), true); assertEquals([...tree.nlrValues()], [4, 0, -2, -3, 2, 1, 3, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, -3, 1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(1), true); assertEquals([...tree.nlrValues()], [4, 0, -2, -3, -1, 2, 3, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, -3, -1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals([...tree.nlrValues()], [4, 0, -2, -3, -1, 2, 1, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, -3, -1, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(4), true); assertEquals([...tree.nlrValues()], [0, -2, -3, -1, 2, 1, 5, 3]); assertEquals([...tree.lvlValues()], [0, -2, 2, -3, -1, 1, 5, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(5), true); assertEquals([...tree.nlrValues()], [0, -2, -3, -1, 2, 1, 4, 3]); assertEquals([...tree.lvlValues()], [0, -2, 2, -3, -1, 1, 4, 3]);
values = [4, 5, 0, -2, 2, -3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [4, 0, -2, -3, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, -3]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [4, -2, -3, 2, 5]); assertEquals([...tree.lvlValues()], [4, -2, 5, -3, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [4, 0, -3, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -3, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [4, -2, -3, 0, 5]); assertEquals([...tree.lvlValues()], [4, -2, 5, -3, 0]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(4), true); assertEquals([...tree.nlrValues()], [0, -2, -3, 5, 2]); assertEquals([...tree.lvlValues()], [0, -2, 5, -3, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(5), true); assertEquals([...tree.nlrValues()], [0, -2, -3, 4, 2]); assertEquals([...tree.lvlValues()], [0, -2, 4, -3, 2]);
values = [4, 5, 0, -2, 2, -1]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [4, 0, -2, -1, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, -1]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [4, -1, -2, 2, 5]); assertEquals([...tree.lvlValues()], [4, -1, 5, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [4, 0, -1, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -1, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [4, -1, -2, 0, 5]); assertEquals([...tree.lvlValues()], [4, -1, 5, -2, 0]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-1), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(4), true); assertEquals([...tree.nlrValues()], [0, -2, -1, 5, 2]); assertEquals([...tree.lvlValues()], [0, -2, 5, -1, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(5), true); assertEquals([...tree.nlrValues()], [0, -2, -1, 4, 2]); assertEquals([...tree.lvlValues()], [0, -2, 4, -1, 2]);
values = [4, 5, 0, -2, 2, 1]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 1, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, 1]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [4, 1, -2, 2, 5]); assertEquals([...tree.lvlValues()], [4, 1, 5, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [4, 1, 0, 2, 5]); assertEquals([...tree.lvlValues()], [4, 1, 5, 0, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 1, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(1), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(4), true); assertEquals([...tree.nlrValues()], [0, -2, 2, 1, 5]); assertEquals([...tree.lvlValues()], [0, -2, 2, 1, 5]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(5), true); assertEquals([...tree.nlrValues()], [0, -2, 2, 1, 4]); assertEquals([...tree.lvlValues()], [0, -2, 2, 1, 4]);
values = [4, 5, 0, -2, 2, 3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 3, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2, 3]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [4, 2, -2, 3, 5]); assertEquals([...tree.lvlValues()], [4, 2, 5, -2, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [4, 2, 0, 3, 5]); assertEquals([...tree.lvlValues()], [4, 2, 5, 0, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 3, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals([...tree.nlrValues()], [4, 0, -2, 2, 5]); assertEquals([...tree.lvlValues()], [4, 0, 5, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(4), true); assertEquals([...tree.nlrValues()], [0, -2, 3, 2, 5]); assertEquals([...tree.lvlValues()], [0, -2, 3, 2, 5]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(5), true); assertEquals([...tree.nlrValues()], [0, -2, 3, 2, 4]); assertEquals([...tree.lvlValues()], [0, -2, 3, 2, 4]);});
Deno.test("RedBlackTree removes rebalance right", () => { let values = [-4, -5, 0]; let tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [-4, -5, 0]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-4, -5]);
values = [-4, -5, 0, -1, 1]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [-4, -5, 0, -1, 1]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -1, 1]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-4, -5, 1, -1]); assertEquals([...tree.lvlValues()], [-4, -5, 1, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-1), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, 1]); assertEquals([...tree.lvlValues()], [-4, -5, 0, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(1), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -1]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -1]);
values = [-4, -5, 0, -2, 2, -3, -1]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, -3, -1, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, -3, -1]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-4, -5, -2, -3, 2, -1]); assertEquals([...tree.lvlValues()], [-4, -5, -2, -3, 2, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -1, -3, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -1, 2, -3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [-4, -5, -2, -3, 0, -1]); assertEquals([...tree.lvlValues()], [-4, -5, -2, -3, 0, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, -1, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, -1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-1), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, -3, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, -3]);
values = [-4, -5, 0, -2, 2, 1, 3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 2, 1, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, 1, 3]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-4, -5, 1, -2, 2, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 1, -2, 2, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [-4, -5, 2, 0, 1, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 2, 0, 3, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 3, 1]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 3, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(1), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 2, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 2, 1]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, 1]);
values = [-4, -5, 0, -2, 2, -3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, -3, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, -3]); assertEquals(tree.remove(-3), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-4, -5, 2, -2]); assertEquals([...tree.lvlValues()], [-4, -5, 2, -2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals(tree.remove(-4), true); assertEquals([...tree.nlrValues()], [-2, -5, 0, 2]); assertEquals([...tree.lvlValues()], [-2, -5, 0, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals(tree.remove(-5), true); assertEquals([...tree.nlrValues()], [0, -4, -2, 2]); assertEquals([...tree.lvlValues()], [0, -4, 2, -2]);
values = [-4, -5, 0, -2, 2, 3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 2, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, 3]); assertEquals(tree.remove(3), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-4, -5, 2, -2]); assertEquals([...tree.lvlValues()], [-4, -5, 2, -2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals(tree.remove(-4), true); assertEquals([...tree.nlrValues()], [-2, -5, 0, 2]); assertEquals([...tree.lvlValues()], [-2, -5, 0, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals(tree.remove(-5), true); assertEquals([...tree.nlrValues()], [0, -4, -2, 2]); assertEquals([...tree.lvlValues()], [0, -4, 2, -2]);
values = [-4, -5, 0, -2, 2, -3, -1, 1, 3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, -3, -1, 2, 1, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, -3, -1, 1, 3]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-4, -5, 1, -2, -3, -1, 2, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 1, -2, 2, -3, -1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -1, -3, 2, 1, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -1, 2, -3, 1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, -3, -1, 3, 1]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 3, -3, -1, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, -1, 2, 1, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, -1, 1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-1), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, -3, 2, 1, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, -3, 1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(1), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, -3, -1, 2, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, -3, -1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, -3, -1, 2, 1]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, -3, -1, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-4), true); assertEquals([...tree.nlrValues()], [-3, -5, 0, -2, -1, 2, 1, 3]); assertEquals([...tree.lvlValues()], [-3, -5, 0, -2, 2, -1, 1, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-5), true); assertEquals([...tree.nlrValues()], [0, -2, -4, -3, -1, 2, 1, 3]); assertEquals([...tree.lvlValues()], [0, -2, 2, -4, -1, 1, 3, -3]);
values = [-4, -5, 0, -2, 2, -3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, -3, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, -3]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-4, -5, -2, -3, 2]); assertEquals([...tree.lvlValues()], [-4, -5, -2, -3, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -3, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -3, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [-4, -5, -2, -3, 0]); assertEquals([...tree.lvlValues()], [-4, -5, -2, -3, 0]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-3), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-4), true); assertEquals([...tree.nlrValues()], [-3, -5, 0, -2, 2]); assertEquals([...tree.lvlValues()], [-3, -5, 0, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-5), true); assertEquals([...tree.nlrValues()], [0, -3, -4, -2, 2]); assertEquals([...tree.lvlValues()], [0, -3, 2, -4, -2]);
values = [-4, -5, 0, -2, 2, -1]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, -1, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, -1]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-4, -5, -1, -2, 2]); assertEquals([...tree.lvlValues()], [-4, -5, -1, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -1, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -1, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [-4, -5, -1, -2, 0]); assertEquals([...tree.lvlValues()], [-4, -5, -1, -2, 0]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-1), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-4), true); assertEquals([...tree.nlrValues()], [-2, -5, 0, -1, 2]); assertEquals([...tree.lvlValues()], [-2, -5, 0, -1, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-5), true); assertEquals([...tree.nlrValues()], [0, -2, -4, -1, 2]); assertEquals([...tree.lvlValues()], [0, -2, 2, -4, -1]);
values = [-4, -5, 0, -2, 2, 1]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 2, 1]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, 1]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-4, -5, 1, -2, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 1, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [-4, -5, 1, 0, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 1, 0, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 1]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 1]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(1), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-4), true); assertEquals([...tree.nlrValues()], [-2, -5, 1, 0, 2]); assertEquals([...tree.lvlValues()], [-2, -5, 1, 0, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-5), true); assertEquals([...tree.nlrValues()], [0, -4, -2, 2, 1]); assertEquals([...tree.lvlValues()], [0, -4, 2, -2, 1]);
values = [-4, -5, 0, -2, 2, 3]; tree = RedBlackTree.from(values); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 2, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2, 3]); assertEquals(tree.remove(0), true); assertEquals([...tree.nlrValues()], [-4, -5, 2, -2, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 2, -2, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-2), true); assertEquals([...tree.nlrValues()], [-4, -5, 2, 0, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 2, 0, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(2), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 3]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(3), true); assertEquals([...tree.nlrValues()], [-4, -5, 0, -2, 2]); assertEquals([...tree.lvlValues()], [-4, -5, 0, -2, 2]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-4), true); assertEquals([...tree.nlrValues()], [-2, -5, 2, 0, 3]); assertEquals([...tree.lvlValues()], [-2, -5, 2, 0, 3]);
tree = RedBlackTree.from(values); assertEquals(tree.remove(-5), true); assertEquals([...tree.nlrValues()], [0, -4, -2, 2, 3]); assertEquals([...tree.lvlValues()], [0, -4, 2, -2, 3]);});
Deno.test("RedBlackTree works with README example", () => { const values = [3, 10, 13, 4, 6, 7, 1, 14]; const tree = new RedBlackTree<number>(); values.forEach((value) => tree.insert(value)); assertEquals([...tree], [1, 3, 4, 6, 7, 10, 13, 14]); assertEquals(tree.min(), 1); assertEquals(tree.max(), 14); assertEquals(tree.find(42), null); assertEquals(tree.find(7), 7); assertEquals(tree.remove(42), false); assertEquals(tree.remove(7), true); assertEquals([...tree], [1, 3, 4, 6, 10, 13, 14]);
const invertedTree = new RedBlackTree<number>(descend); values.forEach((value) => invertedTree.insert(value)); assertEquals([...invertedTree], [14, 13, 10, 7, 6, 4, 3, 1]); assertEquals(invertedTree.min(), 14); assertEquals(invertedTree.max(), 1); assertEquals(invertedTree.find(42), null); assertEquals(invertedTree.find(7), 7); assertEquals(invertedTree.remove(42), false); assertEquals(invertedTree.remove(7), true); assertEquals([...invertedTree], [14, 13, 10, 6, 4, 3, 1]);
const words = new RedBlackTree<string>((a, b) => ascend(a.length, b.length) || ascend(a, b) ); ["truck", "car", "helicopter", "tank", "train", "suv", "semi", "van"] .forEach((value) => words.insert(value)); assertEquals([...words], [ "car", "suv", "van", "semi", "tank", "train", "truck", "helicopter", ]); assertEquals(words.min(), "car"); assertEquals(words.max(), "helicopter"); assertEquals(words.find("scooter"), null); assertEquals(words.find("tank"), "tank"); assertEquals(words.remove("scooter"), false); assertEquals(words.remove("tank"), true); assertEquals([...words], [ "car", "suv", "van", "semi", "train", "truck", "helicopter", ]);});
Version Info