Algorithm question: print all the elements on a single given level of a binary tree -


I have to print nodes at one level of a binary tree (must travel) I do not know how to do it But then I am not usually very skilled with algorithms. I know that you use the queue in the fourth-first traversal and you start by inserting the root node in the queue, then you compute it and enqueue it to the children and then you confuse the first enclosed child and It enjoins children and does so ... and by my understanding it becomes absolutely impossible when one level is finished and the second begins until you start with each node Do not assign programmed, when it is built binary tree that level is and when the fourth-intersection.

Something like this (the code is in PHP but this is not a php related question. This is a common algorithm related question - it is a part of the function that adds a node to a binary tree which is accumulating the level When each node is added to the node):

  if ($ this-> root == zero) {$ this-> Root = $ node; $ This- & gt; Root- & gt; Level = 1; Return; } $ Nextnode = $ this- & gt; Root; $ Level = 1; While {right} {if ($ node-> gt; & gt; value> $ next node-> value) {if ($ nextnode- & gt; right! = Null) {$ nextnode = $ nextnode- & gt; right; $ Level ++; } And {$ next anode-> gt; Correct = $ node; $ Next anode- & gt; Right-> level = ++ $ level; Return; }} And if ($ node-> Standard  Value) {if ($ nextnode-> left! = Null) {$ nextnode = $ nextnode-> Left; $ Level ++; } And {$ next anode-> Left = $ node; $ Next anode- & gt; Left-> Level = ++ $ level; Return; }} And if ($ node-> value == $ next node-> value) returns; }  

So my question is:

Is this the only way to print nodes at one level of binary tree? Is there any other way? What is the other way without making the tree accumulating the level?

Thank you in advance

Do you want a recursive solution suite? I wrote this in C, I hope this is not a problem for you.

  void traverse_tree_rec (treenode * ptn, int current_level, int target_level, (void *) (pvisit) (trillement *)) {return (ptn == faucet); Else if (current_level == targeted_level) pVisit (ptn-> entry); Else {traverse_tree_rec (ptn-> left, current_level + 1, target_level, pVisit); Traverse_tree_rec (ptn-> true, current_level + 1, target_level, PVITIT); } {Traverse_level_rec (pTree-> Gt; root, 0, level, pFunction);  

}


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -