-
Notifications
You must be signed in to change notification settings - Fork 78
Migration towards nnbd. #38
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
Conversation
Move trees and tests to nnbd.
| /// | ||
| /// In [AvlTree] it is the primary focus to actively balance out all | ||
| /// imbalanced [node]s following addition or deletion. | ||
| int balanceFactor = 0; |
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.
How about using a Map here? Combines the good of both enum and current definition
'rightHeavy' -> -1
'balanced' -> 0
'leftHeavy' -> 1
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.
Actually, I like this implementation
enum BalanceState { rightHeavy, balanced, leftHeavy }
class Node {
BalanceState balanceState = BalanceState.balanced;
int get balanceFactor {
switch (balanceState) {
case BalanceState.rightHeavy:
return -1;
case BalanceState.balanced:
return 0;
case BalanceState.leftHeavy:
return 1;
}
}
}
void main() {
var node = Node();
print(node.balanceState);
print(node.balanceFactor);
}This prints
BalanceState.balanced
0
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.
It just occured to me, if a node is leftHeavy, it's balance factor can be any natural number. Similarly rightHeavy node's balance factor can be any negative integer. So by definition, it cannot be hardcoded, like I did.
lib/trees/avl_tree.dart
Outdated
| // [node] was balanced before deletion. (Both left and right subtree had | ||
| // the same height.) | ||
| // N | ||
| // / \ | ||
| // ** ** | ||
| case 0: | ||
| // node is right heavy now. | ||
| // [node] is right heavy now. | ||
| // N | ||
| // / \ | ||
| // * ** | ||
| node.balanceFactor = -1; | ||
| _isShorter = false; | ||
| break; | ||
|
|
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.
@code-shoily Do you think the added comment helps or it's just cluttering?
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.
No it does help.
I'm working on AvlTree, I've migrated it to nnbd but I'm updating comments.
#35