Hello
I know of only one method of visiting the elements on one level of a tree. This method is not very efficient. This is the way I do it(PHP implementation):
I travese all the nodes of the tree in depth(inorder) and I check if teh level is equal to my desired level and if so I visit that node. I also keep track on the number of nodes but that is not relevant to the question.Code:private function NextNode(&$node,$curlevel,$desiredlevel,&$nodesnr=null) { if($node == null) return; $curlevel++; if($curlevel == $desiredlevel) { $this->Visit($node); if($nodesnr!==null) $nodesnr++; } $this->NextNode($node->left,$curlevel,$desiredlevel,$nodesnr); $this->NextNode($node->right,$curlevel,$desiredlevel,$nodesnr); }
This is inefficient because it goes throgh the entire tree.
Does anyone know of a better(more efficient) sollution?
Thank you in advance




Reply With Quote
