Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(bst): on duplicates values the same node is returned#100

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
amejiarosario merged 1 commit intomasterfromfix/nadhem-findings
May 24, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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
11 changes: 6 additions & 5 deletionssrc/data-structures/trees/binary-search-tree.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,23 +19,24 @@ class BinarySearchTree {
*@returns {BinaryTreeNode} newly added node
*/
add(value){
constnewNode=newBinaryTreeNode(value);
letnode=newBinaryTreeNode(value);

if(this.root){
const{ found, parent}=this.findNodeAndParent(value);// <1>
if(found){// duplicated: value already exist on the tree
found.meta.multiplicity=(found.meta.multiplicity||1)+1;// <2>
node=found;
}elseif(value<parent.value){
parent.setLeftAndUpdateParent(newNode);
parent.setLeftAndUpdateParent(node);
}else{
parent.setRightAndUpdateParent(newNode);
parent.setRightAndUpdateParent(node);
}
}else{
this.root=newNode;
this.root=node;
}

this.size+=1;
returnnewNode;
returnnode;
}
// end::add[]

Expand Down
4 changes: 2 additions & 2 deletionssrc/data-structures/trees/binary-search-tree.spec.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -66,7 +66,7 @@ describe('Binary Search Tree', () => {
it('should deal with duplicates', () => {
const root = bst.add(1);
expect(root.meta.multiplicity).toBe(undefined);
bst.add(1);
expect(bst.add(1)).toBe(root); // should return existing
expect(bst.size).toBe(2);
expect(root.toValues()).toMatchObject({
value: 1, parent: null, left: null, right: null,
Expand DownExpand Up@@ -262,7 +262,7 @@ describe('Binary Search Tree', () => {
});

it('should remove duplicates', () => {
bst.add(40); // add duplicate
expect(bst.add(40)).toBe(n40); // add duplicate
expect(n40.meta.multiplicity).toBe(2);

expect(bst.remove(40)).toBe(true);
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp