Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2012
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. void binTree::levelorderIterative()
  2. {
  3. std::string slash;
  4.  
  5. if(!root)
  6. throw emptyTree();
  7.  
  8. linkedBTNQueue current, next;
  9. binNode * curr;
  10.  
  11. // Push the root onto the current queue
  12. current.push(root);
  13.  
  14. // While the current queue is not empty, loop
  15. while(!current.isEmpty()){
  16. // Set the current node to the front element of the current queue
  17. // Pop the front element of the current queue
  18. curr = current.front();
  19. try{
  20. current.pop();
  21. }
  22. catch(linkedBTNQueue::outOfQueueBounds){
  23. throw linkedBTNQueue::outOfQueueBounds();
  24. }
  25. // If curr is not null, print out the data of the current node
  26. if(curr){
  27. std::cout << curr->data << ' ';
  28.  
  29. // If the left branch of the current node is not null, push the
  30. // left branch of the node onto the "next" queue
  31. if(curr->left != NULL){
  32. next.push(curr->left);
  33. slash.push_back('/');
  34.  
  35. }
  36. // If the right branch of the current node is not null, push the
  37. // right branch of the node onto the "next" queue
  38. if(curr->right != NULL){
  39. next.push(curr->right);
  40. slash.push_back('\\');
  41. }
  42. }
  43. // Transfer the elements of the "next" queue to the current queue
  44. if(current.isEmpty()){
  45. std::cout << std::endl;
  46. std::cout << slash << std::endl;
  47. slash.clear();
  48. while(!next.isEmpty()){
  49. try{
  50. current.push(next.pop());
  51. }
  52. catch(linkedBTNQueue::outOfQueueBounds){
  53. throw linkedBTNQueue::outOfQueueBounds();
  54. }
  55. }
  56. }
  57. else{
  58. slash.push_back(' ');
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement