-
Notifications
You must be signed in to change notification settings - Fork 657
feat: add callback functionality for binary search tree #6730
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
base: main
Are you sure you want to change the base?
Conversation
|
Signed-off-by: Abhinav Tamaskar <[email protected]>
data_structures/_test_utils.ts
Outdated
@@ -8,4 +8,5 @@ export class MyMath { | |||
export interface Container { | |||
id: number; | |||
values: number[]; | |||
st_size: number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use camelCase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
@@ -104,12 +105,18 @@ export class BinarySearchTree<T> implements Iterable<T> { | |||
* @param compare A custom comparison function to sort the values in the tree. | |||
* By default, the values are sorted in ascending order. | |||
*/ | |||
constructor(compare: (a: T, b: T) => number = ascend) { | |||
constructor(compare: (a: T, b: T) => number = ascend, callback?: (node: BinarySearchNode<T>) => void) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to document callback
param in jsdoc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the param.
This change doesn't seem a good idea for adding that capability to this class. Currently For small k's, it seems we can use |
The tree can be large, like ~5K. And k can be as large as O(n), so iterating over values with lnr/rnl isn't really a good idea.
I don't think it's the worst thing to make the BinarySearchNode a public class, as we have already made it a "binary" tree, so having left/right/parent elements is expected of it. A different thing to do would be to expose the BinarySearchNode class, but not give access directly to member objects but rather have setChild/getChild or set/get Left/Right/Parent kind of functions. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6730 +/- ##
==========================================
- Coverage 94.65% 93.70% -0.95%
==========================================
Files 576 559 -17
Lines 47065 46838 -227
Branches 6610 6472 -138
==========================================
- Hits 44549 43890 -659
- Misses 2473 2902 +429
- Partials 43 46 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Then maybe it's better to define a new interface which only has Also run |
That sounds like a good idea. I've added a new class BSTNode and refactored the setup to use that.
Thank you for this. I've got it passing locally. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now looks much better to me. Left some comments
data_structures/bst_node.ts
Outdated
* | ||
* @typeparam T The type of the values stored in the binary tree. | ||
*/ | ||
export class BSTNode<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually avoid using this type of acronym in API names. BinarySearchTree used to be BSTree but we renamed it later #2400 I think this should be named BinarySearchTreeNode
.
Also I think this should be typescript interface
instead of class
. We can avoid unnecessary runtime overhead by making this interface
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, renamed to BinarySearchTreeNode and made into an interface.
data_structures/bst_node.ts
Outdated
// This module is browser compatible. | ||
|
||
/** | ||
* A generic Binary Search Tree (BST) node class. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nicely documented 👍
I need the functionality to find the k'th node in the tree, which isn't possible with current setup
This augmentation allows people to define whatever functions that they want, in this particular case, it allows me to store the subtree size in a parameter
st_size
and which I can use to find the k'th node in order.First javascript project PR so this is gonna have some issues, happy to solve them.