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

C++ version of BinaryTree::bfTraverse() is quirky/broken #58

Open
ScottDDexter opened this issue Oct 19, 2017 · 1 comment
Open

C++ version of BinaryTree::bfTraverse() is quirky/broken #58

ScottDDexter opened this issue Oct 19, 2017 · 1 comment

Comments

@ScottDDexter
Copy link
Contributor

The book text, as well as the Java and Python implementations, all use Queues to accumulate nodes to visit, but in the C++ code, the supporting data structure is ArrayDeque<Node*>. Elements are added at q.size() but instead of being removed at the other end, they're removed at q.size()-1. (See full implementation pasted in below.)

This strikes me as odd; I'm happy to submit a PR to fix this, but I wanted to check that I'm not overlooking something.

void BinaryTree<Node>::bfTraverse() {
	ArrayDeque<Node*> q;
	if (r != nil) q.add(q.size(),r);
	while (q.size() > 0) {
		Node *u = q.remove(q.size()-1);
		if (u->left != nil) q.add(q.size(),u->left);
		if (u->right != nil) q.add(q.size(),u->right);
	}
}
@david-topham
Copy link

I agree. I think change remove to use 0 index

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

No branches or pull requests

2 participants