diff --git a/lectures/20_trees_III/README.md b/lectures/20_trees_III/README.md index bee2d2b..3e000ae 100644 --- a/lectures/20_trees_III/README.md +++ b/lectures/20_trees_III/README.md @@ -62,8 +62,6 @@ 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 - BFS code discussed in class - void breadth_first_traverse(Node* root) { int level=0; @@ -93,6 +91,10 @@ We can also implement operator++ for the ds_set iterator without using the paren - 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. +- Run [this bfs_main.cpp program](bfs_main.cpp) to test the above function. + +- Play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/trees/level_order/index.html) to understand how this works. + ## 20.5 Height and Height Calculation Algorithm - The height of a node in a tree is the length of the longest path down the tree from that node to a leaf node. The height of a tree with only one node (the root node) is 1. The height of an empty tree (a tree with no nodes) is 0. @@ -189,7 +191,7 @@ Draw picture of each case! - Then we recursively apply erase to remove that node — which is guaranteed to have at most one child. -play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/trees/delete_node/index.html) to understand how this works. +Play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/trees/delete_node/index.html) to understand how this works. Exercise: Write a recursive version of erase.   diff --git a/lectures/20_trees_III/bfs_main.cpp b/lectures/20_trees_III/bfs_main.cpp new file mode 100644 index 0000000..c1c3b3c --- /dev/null +++ b/lectures/20_trees_III/bfs_main.cpp @@ -0,0 +1,72 @@ +#include +#include + +class Node { +public: + int value; + Node* left; + Node* right; + + // constructor to create a new node + Node(int val) : value(val), left(NULL), right(NULL) {} +}; + +// The breadth-first traversal function you provided +void breadth_first_traverse(Node* root) { + int level = 0; + std::vector current_level; + std::vector 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; +}