From e09342a3da591bf40bc426edadb297449a8cdb14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Sat, 18 Oct 2014 15:49:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B1=82=E5=BA=8F=E9=81=8D?= =?UTF-8?q?=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/BinarySearchTree.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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;