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.
|
- Write an algorithm to print the nodes in the tree one tier at a time, that is, in a breadth-first manner.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
void breadth_first_traverse(Node* root)
|
// the breadth-first traversal function using std::queue
|
||||||
{
|
void breadth_first_traverse(Node* root) {
|
||||||
int level=0;
|
if (root == NULL) {
|
||||||
std::vector<Node*> current_level;
|
return;
|
||||||
std::vector<Node*> next_level;
|
}
|
||||||
if(root==NULL){return;}
|
|
||||||
current_level.push_back(root);
|
std::queue<Node*> node_queue; // queue to store nodes for BFS traversal
|
||||||
while(current_level.size()!=0)
|
node_queue.push(root); // start by pushing the root node
|
||||||
{
|
|
||||||
std::cout<<"level"<<level<<":";
|
int level = 0;
|
||||||
for (unsigned i=0; i<current_level.size();i++)
|
|
||||||
{
|
while (!node_queue.empty()) {
|
||||||
if(current_level[i]->left != NULL)
|
int level_size = node_queue.size(); // number of nodes at the current level
|
||||||
next_level.push_back(current_level[i]->left);
|
std::cout << "level " << level << ": ";
|
||||||
if(current_level[i]->right != NULL)
|
|
||||||
next_level.push_back(current_level[i]->right);
|
for (int i = 0; i < level_size; i++) {
|
||||||
std::cout<<" "<<current_level[i]->value;
|
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;
|
// after we finish the for loop, the only pointers in the queue, are the pointers pointing to nodes of the next level.
|
||||||
level++;
|
|
||||||
next_level.clear();
|
std::cout << std::endl;
|
||||||
std::cout<<std.endl;
|
level++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
- What is the best/average/worst-case running time of this algorithm? What is the best/average/worst-case
|
- 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.
|
memory usage of this algorithm? Give a specific example tree that illustrates each case.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <queue>
|
||||||
|
|
||||||
class Node {
|
class Node {
|
||||||
public:
|
public:
|
||||||
@@ -11,33 +11,39 @@ public:
|
|||||||
Node(int val) : value(val), left(NULL), right(NULL) {}
|
Node(int val) : value(val), left(NULL), right(NULL) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
// The breadth-first traversal function you provided
|
// the breadth-first traversal function using std::queue
|
||||||
void breadth_first_traverse(Node* root) {
|
void breadth_first_traverse(Node* root) {
|
||||||
int level = 0;
|
|
||||||
std::vector<Node*> current_level;
|
|
||||||
std::vector<Node*> next_level;
|
|
||||||
|
|
||||||
if (root == NULL) {
|
if (root == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
current_level.push_back(root);
|
std::queue<Node*> node_queue; // queue to store nodes for BFS traversal
|
||||||
|
node_queue.push(root); // start by pushing the root node
|
||||||
|
|
||||||
while (current_level.size() != 0) {
|
int level = 0;
|
||||||
std::cout << "level " << level << ":";
|
|
||||||
for (unsigned i = 0; i < current_level.size(); i++) {
|
while (!node_queue.empty()) {
|
||||||
if (current_level[i]->left != NULL) {
|
int level_size = node_queue.size(); // number of nodes at the current level
|
||||||
next_level.push_back(current_level[i]->left);
|
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_level[i]->right != NULL) {
|
if (current_node->right != NULL) {
|
||||||
next_level.push_back(current_level[i]->right);
|
node_queue.push(current_node->right);
|
||||||
}
|
}
|
||||||
std::cout << " " << current_level[i]->value;
|
|
||||||
}
|
}
|
||||||
current_level = next_level;
|
// after we finish the for loop, the only pointers in the queue, are the pointers pointing to nodes of the next level.
|
||||||
level++;
|
|
||||||
next_level.clear();
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
level++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
72
lectures/20_trees_III/bfs_main_vector.cpp
Normal file
72
lectures/20_trees_III/bfs_main_vector.cpp
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class Node {
|
||||||
|
public:
|
||||||
|
int value;
|
||||||
|
Node* left;
|
||||||
|
Node* right;
|
||||||
|
|
||||||
|
// constructor to create a new node
|
||||||
|
Node(int val) : value(val), left(NULL), right(NULL) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// bfs using vectors
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
current_level = next_level;
|
||||||
|
level++;
|
||||||
|
next_level.clear();
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// creating a simple binary tree
|
||||||
|
// 1
|
||||||
|
// / \
|
||||||
|
// 2 3
|
||||||
|
// / \ / \
|
||||||
|
//4 5 6 7
|
||||||
|
Node* root = new Node(1);
|
||||||
|
root->left = new Node(2);
|
||||||
|
root->right = new Node(3);
|
||||||
|
root->left->left = new Node(4);
|
||||||
|
root->left->right = new Node(5);
|
||||||
|
root->right->left = new Node(6);
|
||||||
|
root->right->right = new Node(7);
|
||||||
|
|
||||||
|
// calling the breadth-first traversal function
|
||||||
|
breadth_first_traverse(root);
|
||||||
|
|
||||||
|
// cleaning up dynamically allocated memory
|
||||||
|
delete root->left->left;
|
||||||
|
delete root->left->right;
|
||||||
|
delete root->right->left;
|
||||||
|
delete root->right->right;
|
||||||
|
delete root->left;
|
||||||
|
delete root->right;
|
||||||
|
delete root;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user