完成迭代器--操作
This commit is contained in:
@@ -222,14 +222,14 @@ namespace TinySTL{
|
||||
private:
|
||||
const T *ptr_;
|
||||
cntrPtr container_;
|
||||
stack<const T *> stack_;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>root<6F><74>ptr_<72>ĸ<EFBFBD><C4B8>ڵ<EFBFBD><DAB5><EFBFBD>·<EFBFBD><C2B7>
|
||||
stack<const T *> parent_;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>root<6F><74>ptr_<72>ĸ<EFBFBD><C4B8>ڵ<EFBFBD><DAB5><EFBFBD>·<EFBFBD><C2B7>
|
||||
std::set<const T *> visited_;
|
||||
public:
|
||||
bst_iter(const T *ptr, cntrPtr container)
|
||||
:ptr_(ptr), container_(container){
|
||||
auto temp = container_->root_;
|
||||
while (temp &&ptr_ && temp != ptr_ && temp->data_ != ptr_->data_){
|
||||
stack_.push(temp);
|
||||
parent_.push(temp);
|
||||
if (temp->data_ < ptr_->data_)
|
||||
temp = temp->right_;
|
||||
else if (temp->data_ > ptr_->data_)
|
||||
@@ -253,18 +253,18 @@ namespace TinySTL{
|
||||
};//end of bst_iter
|
||||
template<class T>
|
||||
bst_iter<T>& bst_iter<T>::operator ++(){
|
||||
visited_.insert(ptr_);
|
||||
visited_.insert(ptr_);//<2F><>Ϊ<EFBFBD>ѷ<EFBFBD><D1B7><EFBFBD>
|
||||
if (ptr_->right_ != 0){
|
||||
stack_.push(ptr_);
|
||||
parent_.push(ptr_);
|
||||
ptr_ = ptr_->right_;
|
||||
while (ptr_ != 0 && ptr_->left_ != 0){
|
||||
stack_.push(ptr_);
|
||||
parent_.push(ptr_);
|
||||
ptr_ = ptr_->left_;
|
||||
}
|
||||
}else{
|
||||
if (!stack_.empty() && visited_.count(stack_.top()) == 0){
|
||||
ptr_ = stack_.top();
|
||||
stack_.pop();
|
||||
if (!parent_.empty() && visited_.count(parent_.top()) == 0){//<2F><><EFBFBD>ڵ<EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD>
|
||||
ptr_ = parent_.top();
|
||||
parent_.pop();
|
||||
}else{
|
||||
ptr_ = 0;
|
||||
}
|
||||
@@ -278,6 +278,30 @@ namespace TinySTL{
|
||||
return res;
|
||||
}
|
||||
template<class T>
|
||||
bst_iter<T>& bst_iter<T>::operator --(){
|
||||
visited_.erase(ptr_);//<2F><>Ϊδ<CEAA><CEB4><EFBFBD><EFBFBD>
|
||||
if (ptr_->left_ != 0){
|
||||
parent_.push(ptr_);
|
||||
ptr_ = ptr_->left_;
|
||||
while (ptr_ && ptr_->right_){
|
||||
parent_.push(ptr_);
|
||||
ptr_ = ptr_->right_;
|
||||
}
|
||||
}else{
|
||||
if (!parent_.empty() && visited_.count(parent_.top()) == 1){//<2F><><EFBFBD>ڵ<EFBFBD><DAB5>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
ptr_ = parent_.top();
|
||||
parent_.pop();
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
template<class T>
|
||||
bst_iter<T> bst_iter<T>::operator --(int){
|
||||
auto res = *this;
|
||||
--*this;
|
||||
return res;
|
||||
}
|
||||
template<class T>
|
||||
bool operator ==(const bst_iter<T>& it1, const bst_iter<T>& it2){
|
||||
return it1.ptr_ == it2.ptr_;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user