完成find
This commit is contained in:
@@ -17,6 +17,7 @@ namespace TinySTL{
|
|||||||
friend class bst_iter;
|
friend class bst_iter;
|
||||||
private:
|
private:
|
||||||
struct node{
|
struct node{
|
||||||
|
typedef T value_type;
|
||||||
T data_;
|
T data_;
|
||||||
node *left_;
|
node *left_;
|
||||||
node *right_;
|
node *right_;
|
||||||
@@ -37,6 +38,14 @@ namespace TinySTL{
|
|||||||
}
|
}
|
||||||
|
|
||||||
void insert(const T& val);
|
void insert(const T& val);
|
||||||
|
template<class Iterator>
|
||||||
|
void insert(Iterator first, Iterator last);
|
||||||
|
|
||||||
|
bool empty()const{ return root_ == 0; }
|
||||||
|
|
||||||
|
iterator find_min();
|
||||||
|
iterator find_max();
|
||||||
|
iterator find(const T& val);
|
||||||
|
|
||||||
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;
|
||||||
@@ -50,12 +59,15 @@ namespace TinySTL{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
void insert_elem(const T& val, node *&ptr);
|
void insert_elem(const T& val, node *&ptr);
|
||||||
|
iterator find_min_aux(node *ptr);
|
||||||
|
iterator find_max_aux(node *ptr);
|
||||||
|
iterator find_aux(const T& val, node *ptr);
|
||||||
void print_preorder_aux(const string& delim, std::ostream& os, const node *ptr)const;
|
void print_preorder_aux(const string& delim, std::ostream& os, const node *ptr)const;
|
||||||
void print_inorder_aux(const string& delim, std::ostream& os, const node *ptr)const;
|
void print_inorder_aux(const string& delim, std::ostream& os, const node *ptr)const;
|
||||||
void print_postorder_aux(const string& delim, std::ostream& os, const node *ptr)const;
|
void print_postorder_aux(const string& delim, std::ostream& os, const node *ptr)const;
|
||||||
};//end of bst class
|
};//end of bst class
|
||||||
template<class T>
|
template<class T>
|
||||||
void binary_search_tree<T>::insert_elem(const T& val, node *&ptr){
|
void binary_search_tree<T>::insert_elem(const T& val, node *&ptr){//<2F>ظ<EFBFBD><D8B8><EFBFBD>Ԫ<EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD>
|
||||||
if (ptr == 0){
|
if (ptr == 0){
|
||||||
ptr = nodeAllocator::allocate();
|
ptr = nodeAllocator::allocate();
|
||||||
ptr->data_ = val;
|
ptr->data_ = val;
|
||||||
@@ -75,6 +87,12 @@ namespace TinySTL{
|
|||||||
insert_elem(val, root_);
|
insert_elem(val, root_);
|
||||||
}
|
}
|
||||||
template<class T>
|
template<class T>
|
||||||
|
template<class Iterator>
|
||||||
|
void binary_search_tree<T>::insert(Iterator first, Iterator last){
|
||||||
|
for (; first != last; ++first)
|
||||||
|
insert(*first);
|
||||||
|
}
|
||||||
|
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;
|
||||||
@@ -110,11 +128,62 @@ namespace TinySTL{
|
|||||||
void binary_search_tree<T>::print_postorder(const string& delim, std::ostream& os)const{
|
void binary_search_tree<T>::print_postorder(const string& delim, std::ostream& os)const{
|
||||||
print_postorder_aux(delim, os, root_);
|
print_postorder_aux(delim, os, root_);
|
||||||
}
|
}
|
||||||
|
template<class T>
|
||||||
|
typename binary_search_tree<T>::iterator binary_search_tree<T>::find_min_aux(node *ptr){
|
||||||
|
while (ptr && ptr->left_ != 0){
|
||||||
|
ptr = ptr->left_;
|
||||||
|
}
|
||||||
|
return iterator(ptr);
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
typename binary_search_tree<T>::iterator binary_search_tree<T>::find_min(){
|
||||||
|
return find_min_aux(root_);
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
typename binary_search_tree<T>::iterator binary_search_tree<T>::find_max_aux(node *ptr){
|
||||||
|
while (ptr && ptr->right_ != 0){
|
||||||
|
ptr = ptr->right_;
|
||||||
|
}
|
||||||
|
return iterator(ptr);
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
typename binary_search_tree<T>::iterator binary_search_tree<T>::find_max(){
|
||||||
|
return find_max_aux(root_);
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
typename binary_search_tree<T>::iterator binary_search_tree<T>::find_aux(const T& val, node *ptr){
|
||||||
|
while (ptr){
|
||||||
|
if (val == ptr->data_)
|
||||||
|
break;
|
||||||
|
else if (val < ptr->data_)
|
||||||
|
ptr = ptr->left_;
|
||||||
|
else
|
||||||
|
ptr = ptr->right_;
|
||||||
|
}
|
||||||
|
return iterator(ptr);
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
typename binary_search_tree<T>::iterator binary_search_tree<T>::find(const T& val){
|
||||||
|
return find_aux(val, root_);
|
||||||
|
}
|
||||||
|
|
||||||
namespace{
|
namespace{
|
||||||
//class of bst iterator
|
//class of bst iterator
|
||||||
template<class T>
|
template<class T>//T = node
|
||||||
class bst_iter{};
|
class bst_iter{
|
||||||
|
private:
|
||||||
|
typedef typename binary_search_tree<typename T::value_type>::vaule_type value_type;
|
||||||
|
typedef typename binary_search_tree<typename T::value_type>::reference reference;
|
||||||
|
typedef typename T::value_type *pointer;
|
||||||
|
private:
|
||||||
|
T *ptr_;
|
||||||
|
public:
|
||||||
|
bst_iter(T *ptr) :ptr_(ptr){}
|
||||||
|
|
||||||
|
operator T*(){ return ptr_; }
|
||||||
|
reference operator*(){ return ptr_->data_; }
|
||||||
|
pointer operator ->(){ return &(operator*()); }
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user