添加层序遍历
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
#include "Stack.h"
|
#include "Stack.h"
|
||||||
#include "String.h"
|
#include "String.h"
|
||||||
|
|
||||||
|
#include <deque>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
namespace TinySTL{
|
namespace TinySTL{
|
||||||
@@ -60,6 +61,7 @@ namespace TinySTL{
|
|||||||
void print_preorder(const string& delim = " ", std::ostream& os = std::cout)const;
|
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_inorder(const string& delim = " ", std::ostream& os = std::cout)const;
|
||||||
void print_postorder(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:
|
private:
|
||||||
void deallocateAllNodes(node *ptr){
|
void deallocateAllNodes(node *ptr){
|
||||||
if (ptr){
|
if (ptr){
|
||||||
@@ -137,6 +139,21 @@ namespace TinySTL{
|
|||||||
insert(*first);
|
insert(*first);
|
||||||
}
|
}
|
||||||
template<class T>
|
template<class T>
|
||||||
|
void binary_search_tree<T>::print_levelorder(const string& delim, std::ostream& os)const{
|
||||||
|
auto temp = root_;
|
||||||
|
if (temp != 0){
|
||||||
|
std::deque<node *> 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<class T>
|
||||||
void binary_search_tree<T>::print_preorder_aux(const string& delim, std::ostream& os, const node *ptr)const{
|
void binary_search_tree<T>::print_preorder_aux(const string& delim, std::ostream& os, const node *ptr)const{
|
||||||
if (ptr){
|
if (ptr){
|
||||||
os << ptr->data_ << delim;
|
os << ptr->data_ << delim;
|
||||||
|
|||||||
Reference in New Issue
Block a user