fueanta

Function for BFS.

Dec 19th, 2016
125
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Function for Breadth-First Search in CPP
  2. // nodeptr = TreeNode*
  3.  
  4. void bfs(nodeptr first) {
  5.     queue <nodeptr> i;
  6.     if (first != NULL)
  7.         i.push(first);
  8.     while (!i.empty()) {
  9.         if (i.front()->left != NULL)
  10.             i.push(i.front()->left);
  11.         if (i.front()->right != NULL)
  12.             i.push(i.front()->right);
  13.         cout << i.front()->key << " ";
  14.         i.pop();
  15.     }
  16.     cout << endl;
  17. }
RAW Paste Data