diff --git a/data_structures/binary_search_tree.ts b/data_structures/binary_search_tree.ts index a3fedffa8bf6..dccf0e39f546 100644 --- a/data_structures/binary_search_tree.ts +++ b/data_structures/binary_search_tree.ts @@ -295,7 +295,7 @@ export class BinarySearchTree implements Iterable { } const values: Iterable = options?.map ? Array.from(unmappedValues, options.map, options.thisArg) - : unmappedValues as U[]; + : Array.from(unmappedValues) as unknown as U[]; for (const value of values) result.insert(value); return result; } diff --git a/data_structures/binary_search_tree_test.ts b/data_structures/binary_search_tree_test.ts index 887fda5c32f7..ce7b78beaf44 100644 --- a/data_structures/binary_search_tree_test.ts +++ b/data_structures/binary_search_tree_test.ts @@ -317,6 +317,26 @@ Deno.test("BinarySearchTree contains objects", () => { assertEquals(tree.isEmpty(), true); }); +Deno.test("BinarySearchTree.from() handles empty array-like objects", () => { + const tree = BinarySearchTree.from({ length: 0 }); + assertEquals([...tree], []); + assertEquals(tree.size, 0); +}); + +Deno.test("BinarySearchTree.from() handles array-like objects", () => { + const values = { 0: 3, 1: 1, 2: 3, 3: 2, length: 4 }; + const tree = BinarySearchTree.from(values); + assertEquals([...tree], [1, 2, 3]); + assertEquals(tree.size, 3); +}); + +Deno.test("BinarySearchTree.from() handles array-like objects with a custom comparator", () => { + const values = { 0: 3, 1: 1, 2: 3, 3: 2, length: 4 }; + const tree = BinarySearchTree.from(values, { compare: descend }); + assertEquals([...tree], [3, 2, 1]); + assertEquals(tree.size, 3); +}); + Deno.test("BinarySearchTree.from() handles iterable", () => { const values: number[] = [-10, 9, -1, 100, 9, 1, 0, 9, -100, 10, -9]; const originalValues: number[] = Array.from(values); diff --git a/data_structures/red_black_tree.ts b/data_structures/red_black_tree.ts index c853d52399d3..e8a971290915 100644 --- a/data_structures/red_black_tree.ts +++ b/data_structures/red_black_tree.ts @@ -267,7 +267,7 @@ export class RedBlackTree extends BinarySearchTree { } const values: Iterable = options?.map ? Array.from(unmappedValues, options.map, options.thisArg) - : unmappedValues as U[]; + : Array.from(unmappedValues) as unknown as U[]; for (const value of values) result.insert(value); return result; } diff --git a/data_structures/red_black_tree_test.ts b/data_structures/red_black_tree_test.ts index 0e07f2c4f14b..00ebb9b7dcde 100644 --- a/data_structures/red_black_tree_test.ts +++ b/data_structures/red_black_tree_test.ts @@ -312,6 +312,26 @@ Deno.test("RedBlackTree works with object items", () => { assertEquals(tree.isEmpty(), true); }); +Deno.test("RedBlackTree.from() handles empty array-like objects", () => { + const tree = RedBlackTree.from({ length: 0 }); + assertEquals([...tree], []); + assertEquals(tree.size, 0); +}); + +Deno.test("RedBlackTree.from() handles array-like objects", () => { + const values = { 0: 3, 1: 1, 2: 3, 3: 2, length: 4 }; + const tree = RedBlackTree.from(values); + assertEquals([...tree], [1, 2, 3]); + assertEquals(tree.size, 3); +}); + +Deno.test("RedBlackTree.from() handles array-like objects with a custom comparator", () => { + const values = { 0: 3, 1: 1, 2: 3, 3: 2, length: 4 }; + const tree = RedBlackTree.from(values, { compare: descend }); + assertEquals([...tree], [3, 2, 1]); + assertEquals(tree.size, 3); +}); + 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);