diff --git a/TinySTL/BinarySearchTree.h b/TinySTL/BinarySearchTree.h index d650f31..f3d7266 100644 --- a/TinySTL/BinarySearchTree.h +++ b/TinySTL/BinarySearchTree.h @@ -5,6 +5,7 @@ #include "Stack.h" #include "String.h" +#include #include namespace TinySTL{ @@ -60,6 +61,7 @@ namespace TinySTL{ void print_preorder(const string& delim = " ", std::ostream& os = std::cout)const; void print_inorder(const string& delim = " ", std::ostream& os = std::cout)const; void print_postorder(const string& delim = " ", std::ostream& os = std::cout)const; + void print_levelorder(const string& delim = " ", std::ostream& os = std::cout)const; private: void deallocateAllNodes(node *ptr){ if (ptr){ @@ -137,6 +139,21 @@ namespace TinySTL{ insert(*first); } template + void binary_search_tree::print_levelorder(const string& delim, std::ostream& os)const{ + auto temp = root_; + if (temp != 0){ + std::deque q; + q.push_back(temp); + while (!q.empty()){ + temp = q.front(); + q.pop_front(); + if (temp->left_ != 0) q.push_back(temp->left_); + if (temp->right_ != 0) q.push_back(temp->right_); + os << temp->data_ << delim; + } + } + } + template void binary_search_tree::print_preorder_aux(const string& delim, std::ostream& os, const node *ptr)const{ if (ptr){ os << ptr->data_ << delim;