-
Notifications
You must be signed in to change notification settings - Fork 13
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
Optimization to improve the time and space complexity #18
base: master
Are you sure you want to change the base?
Conversation
@@ -34,6 +34,25 @@ | |||
return leafs; | |||
} | |||
|
|||
/*Using recursion to optimize the method by | |||
* reducing the number of object created*/ | |||
public static int getLeafsSize(Node root) { |
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.
Just a curiosity. It's clear that this method is better then getLeafs(node).size()
. Another possible implementation is similar with getLeafs(node)
but with a variable leafsSize
instead of leafs
:
public static List<Node> getLeafs(Node root) {
List<Node> leafs = new ArrayList<Node>();
Iterator<Node> it = new TreeIterator(root);
while (it.hasNext()) {
Node node = it.next();
if (node.isLeaf()) {
leafs.add(node);
}
}
return leafs;
}
public static List<Node> getLeafsSize(Node root) {
int leafsSize = 0;
Iterator<Node> it = new TreeIterator(root);
while (it.hasNext()) {
Node node = it.next();
if (node.isLeaf()) {
leafsSize++;
}
}
return leafsSize;
}
In this mode I eliminate the recursive call.
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.
i agree,this will work as well
Thanks! I love to see code that optimize something but I must admit that are rare the moments when something submit code that resolve a performance problem. |
Did you do any benchmarks? How much improvement is gained with this patch? |
change1:-
Using HashSet (look-up is O(1)) for pathRenderedCache which is optimized compared ArrayList
change 2:-
Calculation of leaf span
Using recursion to optimize the method by reducing the number of object created