From 590652f0ab37b9e303fc3efefa5abed6d35a4c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Wed, 14 Jan 2015 15:48:40 +0800 Subject: [PATCH] bug fix --- TinySTL/BinarySearchTree.h | 2 +- TinySTL/Detail/BinarySearchTree.impl.h | 28 +++++---------- TinySTL/Test/BinarySearchTreeTest.cpp | 2 +- TinySTL/Test/BinarySearchTreeTest.h | 4 +++ TinySTL/TinySTL.vcxproj | 16 +++++++-- TinySTL/TinySTL.vcxproj.filters | 47 +++++++++++++++++++++++--- 6 files changed, 72 insertions(+), 27 deletions(-) diff --git a/TinySTL/BinarySearchTree.h b/TinySTL/BinarySearchTree.h index 9326c33..e4ba6ea 100644 --- a/TinySTL/BinarySearchTree.h +++ b/TinySTL/BinarySearchTree.h @@ -93,7 +93,7 @@ namespace TinySTL{ const T *ptr_; cntrPtr container_; stack parent_;//±£´æ´Órootµ½ptr_µÄ¸¸½ÚµãµÄ·¾¶ - std::set rvisited_;//µ±Ç°½ÚµãµÄÓÒ×ÓÊ÷ÊÇ·ñ±»·ÃÎʹý + //std::set rvisited_;//µ±Ç°½ÚµãµÄÓÒ×ÓÊ÷ÊÇ·ñ±»·ÃÎʹý std::set visited_;//µ±Ç°½ÚµãÊÇ·ñ±»·ÃÎʹý£¨´Ënode±»·ÃÎÊ˵Ã÷Æä×ó×ÓÊ÷Òѱ»·ÃÎÊÁË£© public: bst_iter(const T *ptr, cntrPtr container); diff --git a/TinySTL/Detail/BinarySearchTree.impl.h b/TinySTL/Detail/BinarySearchTree.impl.h index dbbfb06..a08b00c 100644 --- a/TinySTL/Detail/BinarySearchTree.impl.h +++ b/TinySTL/Detail/BinarySearchTree.impl.h @@ -12,6 +12,8 @@ namespace TinySTL{ while (temp && temp != ptr_ && temp->data_ != ptr_->data_){ parent_.push(temp); if (temp->data_ < ptr_->data_){ + //tempÏòÓÒ×ß˵Ã÷temoÖ¸ÏòµÄ¸¸½Úµã²»ÐèÒªÔٴηÃÎÊÁË + visited_.insert(temp);//add 2015.01.14 temp = temp->right_; } else if (temp->data_ > ptr_->data_){ @@ -23,37 +25,25 @@ namespace TinySTL{ bst_iter& bst_iter::operator ++(){ visited_.insert(ptr_);//´Ënode±»·ÃÎÊ if (ptr_->right_){//´Ënode»¹ÓÐÓÒ×ÓÊ÷ - rvisited_.insert(ptr_); + //rvisited_.insert(ptr_); parent_.push(ptr_); ptr_ = ptr_->right_; while (ptr_ && ptr_->left_){ parent_.push(ptr_); ptr_ = ptr_->left_; } - } - else{//nodeÎÞÓÒ×ÓÊ÷ÔòÖ»ÄÜÏò¸¸½Úµã·¾¶Òƶ¯ - + }else{//nodeÎÞÓÒ×ÓÊ÷ÔòÖ»ÄÜÏò¸¸½Úµã·¾¶Òƶ¯ + ptr_ = 0;//add 2015.01.14 while (!parent_.empty()){ ptr_ = parent_.top(); parent_.pop(); - if (visited_.count(ptr_) == 0){//¸¸½ÚµãÉÐδ·ÃÎÊ + if (visited_.count(ptr_) == 0){//¸¸½ÚµãÉÐδ·ÃÎÊ,´Ëʱptr_Ö¸Ïò´Ë½Úµã visited_.insert(ptr_); break; } - else if (rvisited_.count(ptr_) == 0){//¸¸½ÚµãµÄÓÒ×ÓÊ÷ÉÐδ±»·ÃÎÊ - rvisited_.insert(ptr_); - if (ptr_->right_){ - parent_.push(ptr_); - ptr_ = ptr_->right_; - while (ptr_ && ptr_->left_){ - parent_.push(ptr_); - ptr_ = ptr_->left_; - } - } - } - ptr_ = 0; - } - } + ptr_ = 0;//ÉèΪÉÚ±ø + }//end of while + }//end of if return *this; } template diff --git a/TinySTL/Test/BinarySearchTreeTest.cpp b/TinySTL/Test/BinarySearchTreeTest.cpp index 50e0cf1..e65ea9b 100644 --- a/TinySTL/Test/BinarySearchTreeTest.cpp +++ b/TinySTL/Test/BinarySearchTreeTest.cpp @@ -36,7 +36,7 @@ namespace TinySTL{ std::vector v; std::random_device rd; - for (auto i = 0; i != 10; ++i){ + for (auto i = 0; i != 100; ++i){ auto r = rd() % 65536; bst.insert(r); v.push_back(r); diff --git a/TinySTL/Test/BinarySearchTreeTest.h b/TinySTL/Test/BinarySearchTreeTest.h index 0cd4023..1bb2e88 100644 --- a/TinySTL/Test/BinarySearchTreeTest.h +++ b/TinySTL/Test/BinarySearchTreeTest.h @@ -16,6 +16,10 @@ namespace TinySTL{ template using tsBst = TinySTL::binary_search_tree < T > ; + void testCase1(); + void testCase2(); + void testCase3(); + void testCase4(); void testAllCases(); } diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index 3c08a13..361ec6a 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -79,13 +79,16 @@ - + + - + + + @@ -104,6 +107,12 @@ + + + + + + @@ -114,8 +123,11 @@ + + + diff --git a/TinySTL/TinySTL.vcxproj.filters b/TinySTL/TinySTL.vcxproj.filters index 508fc94..87faa39 100644 --- a/TinySTL/TinySTL.vcxproj.filters +++ b/TinySTL/TinySTL.vcxproj.filters @@ -22,6 +22,9 @@ {f20d9032-1c1d-424a-a8aa-f39c87789d3b} + + {7dd5c5f0-33b6-44c6-b2b9-d4c0ec1d4c13} + @@ -60,11 +63,20 @@ Test - - 头文件 + + Test - - 头文件 + + Test + + + Detail + + + Detail + + + Test @@ -167,6 +179,33 @@ Test + + Test + + + Test + + + Detail + + + Detail + + + Detail + + + Detail + + + Detail + + + Test + + + Detail +