now bfs using queues
This commit is contained in:
@@ -61,32 +61,43 @@ We can also implement operator++ for the ds_set iterator without using the paren
|
||||
|
||||
- Write an algorithm to print the nodes in the tree one tier at a time, that is, in a breadth-first manner.
|
||||
|
||||
```cpp
|
||||
void breadth_first_traverse(Node* root)
|
||||
{
|
||||
int level=0;
|
||||
std::vector<Node*> current_level;
|
||||
std::vector<Node*> next_level;
|
||||
if(root==NULL){return;}
|
||||
current_level.push_back(root);
|
||||
while(current_level.size()!=0)
|
||||
{
|
||||
std::cout<<"level"<<level<<":";
|
||||
for (unsigned i=0; i<current_level.size();i++)
|
||||
{
|
||||
if(current_level[i]->left != NULL)
|
||||
next_level.push_back(current_level[i]->left);
|
||||
if(current_level[i]->right != NULL)
|
||||
next_level.push_back(current_level[i]->right);
|
||||
std::cout<<" "<<current_level[i]->value;
|
||||
```cpp
|
||||
// the breadth-first traversal function using std::queue
|
||||
void breadth_first_traverse(Node* root) {
|
||||
if (root == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::queue<Node*> node_queue; // queue to store nodes for BFS traversal
|
||||
node_queue.push(root); // start by pushing the root node
|
||||
|
||||
int level = 0;
|
||||
|
||||
while (!node_queue.empty()) {
|
||||
int level_size = node_queue.size(); // number of nodes at the current level
|
||||
std::cout << "level " << level << ": ";
|
||||
|
||||
for (int i = 0; i < level_size; i++) {
|
||||
Node* current_node = node_queue.front(); // get the front node
|
||||
node_queue.pop(); // remove the node from the queue
|
||||
|
||||
std::cout << current_node->value << " "; // print the value of the node
|
||||
|
||||
// push the children of the current node to the queue (if they exist)
|
||||
if (current_node->left != NULL) {
|
||||
node_queue.push(current_node->left);
|
||||
}
|
||||
if (current_node->right != NULL) {
|
||||
node_queue.push(current_node->right);
|
||||
}
|
||||
}
|
||||
current_level = next_level;
|
||||
level++;
|
||||
next_level.clear();
|
||||
std::cout<<std.endl;
|
||||
}
|
||||
}
|
||||
```
|
||||
// after we finish the for loop, the only pointers in the queue, are the pointers pointing to nodes of the next level.
|
||||
|
||||
std::cout << std::endl;
|
||||
level++;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- What is the best/average/worst-case running time of this algorithm? What is the best/average/worst-case
|
||||
memory usage of this algorithm? Give a specific example tree that illustrates each case.
|
||||
|
||||
Reference in New Issue
Block a user