Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(data-structures): ensure size consistency in RedBlackTree.from #6307

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data_structures/_binary_search_tree_internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ export const internals: {
tree: BinarySearchTree<T>,
node: BinarySearchNode<T>,
): BinarySearchNode<T> | null;
setSize<T>(tree: BinarySearchTree<T>, size: number): void;
} = {} as typeof internals;
2 changes: 2 additions & 0 deletions data_structures/binary_search_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ export class BinarySearchTree<T> implements Iterable<T> {
tree: BinarySearchTree<T>,
node: BinarySearchNode<T>,
): BinarySearchNode<T> | null => tree.#removeNode(node);
internals.setSize = <T>(tree: BinarySearchTree<T>, size: number) =>
tree.#size = size;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions data_structures/red_black_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
rotateNode,
insertNode,
removeNode,
setSize,
} = internals;

/**
Expand Down Expand Up @@ -251,6 +252,7 @@ export class RedBlackTree<T> extends BinarySearchTree<T> {
nodes.push(right);
}
}
setSize(result, collection.size);
}
} else {
result = (options?.compare
Expand Down
2 changes: 2 additions & 0 deletions data_structures/red_black_tree_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ Deno.test("RedBlackTree.from() handles default ascend comparator", () => {
const originalTree: RedBlackTree<number> = new RedBlackTree();
for (const value of values) originalTree.insert(value);
let tree: RedBlackTree<number> = RedBlackTree.from(originalTree);
assertEquals(originalTree.size, tree.size);
assertEquals([...originalTree], expected);
assertEquals([...tree], expected);
assertEquals([...tree.nlrValues()], [...originalTree.nlrValues()]);
Expand Down Expand Up @@ -434,6 +435,7 @@ Deno.test("RedBlackTree.from() handles descend comparator", () => {
const originalTree: RedBlackTree<number> = new RedBlackTree(descend);
for (const value of values) originalTree.insert(value);
let tree: RedBlackTree<number> = RedBlackTree.from(originalTree);
assertEquals(originalTree.size, tree.size);
assertEquals([...originalTree], expected);
assertEquals([...tree], expected);
assertEquals([...tree.nlrValues()], [...originalTree.nlrValues()]);
Expand Down
Loading