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

Optimization to improve the time and space complexity #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

tyagi85prashant
Copy link

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

@@ -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) {
Copy link
Owner

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.

Copy link
Author

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

@decebals
Copy link
Owner

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.

@rototor
Copy link
Collaborator

rototor commented Dec 15, 2016

Did you do any benchmarks? How much improvement is gained with this patch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants