1 #include 2 #include<string> 3 #include 4 using namespace std; 5 template 6 class BST { 7 public: 8 BST() { sz = hi = 0; top = NULL; }//初始化 9 void insert(const T& data); 10 void insert(const initializer_list& data_list) { for (auto i : data_list)insert(i); } 11 void remove(const T& data); 12 void remove(binaryNode*node); 13 constexpr binaryNode* root() const { return top; } 14 void inorder_traverse (const binaryNode* node/*=root()*/) //中序遍历了解结构,这里不能用默认参数root() 15 { 16 if (!node)return; 17 inorder_traverse(node->lson); 18 cout << node->data << " "; 19 inorder_traverse(node->rson); 20 } 21 /* 22 问:一个类中,为什么不能将数据成员做为成员函数的默认参数? 23 答:标准规定这么一个限制条件是有其理由的, 24 非静态成员如果在成员函数被调用前没有被初始化, 25 此时编译器无法确认函数参数的默认值是多少。 26 而标准这样做就把错误的发现提前到编译期。 27 */ 28 binaryNode* floor(const T& data);//小于等于data的元素 29 binaryNode* ceil (const T& data);//大于等于data的元素 30 binaryNode* max(binaryNode*node) const{ while (node->rson)node = node->rson; return node; }//一个节点当做个子树看,最大的元素 31 binaryNode* min(binaryNode*node) const{ while (node->lson)node = node->lson; return node; }//一个节点当做个子树看,最小的元素 32 constexpr int size() const{ return sz; } 33 constexpr int height() const { return hi; } 34 binaryNode* find(const T&data) 35 { 36 if (auto node = find_it(data))return node; else { cout << "we can't find it!"; abort(); } 37 } 38 //通过find_it(data)获得准确的节点在哪,如果为空,程序截止 39 ~BST() 40 { 41 traverse(top); 42 }//遍历删除元素 43 private: 44 void traverse(binaryNode* node) { if (node) { traverse(node->lson); traverse(node->rson); delete node; } } 45 binaryNode* find_it(const T& data); 46 binaryNode* search(const T& data); 47 void swap(binaryNode* left, binaryNode* right) { auto t = left->data; left->data = right->data; right->data = t; }//只交换元素不交换指针 48 constexpr bool is_exist(const binaryNode* node) const { return node->lson || node->rson; }//判断双节点是否存在不存在返回false; 49 int checksz(const binaryNode*node) const 50 { 51 if (!node)return 0; 52 return checksz(node->lson) + checksz(node->rson) + 1; 53 }//遍历检查各个子树的size 54 int checkhi(const binaryNode* node) const 55 { 56 if(!node)return 0; 57 return std::max(checkhi(node->lson),checkhi(node->rson)) + 1; 58 }//遍历检查各个子树的height 59 60 void remove_leaf(binaryNode* node) 61 { 62 if (node != top) { 63 if (node->father->lson == node) 64 node->father->lson = NULL; 65 else 66 node->father->rson = NULL; 67 } 68 else top = NULL; 69 delete node; 70 }//删除叶子节点,如果是top,则置为空树。 71 void remove_one(binaryNode* node) 72 { 73 if (node != top) { 74 if (!node->lson) 75 { 76 if (node->father->lson == node) 77 { 78 node->father->lson = node->rson; 79 node->rson->father = node->father; 80 } 81 else 82 { 83 node->father->rson = node->rson; 84 node->rson->father = node->father; 85 } 86 } 87 else 88 { 89 if (node->father->lson == node) 90 { 91 node->father->lson = node->lson; 92 node->lson->father = node->father; 93 } 94 else 95 { 96 node->father->rson = node->lson; 97 node->lson->father = node->father; 98 } 99 }100 }101 else {102 if (!node->lson) top = node->rson;103 else top = node->lson;104 105 }106 delete node;107 }//删除一个节点,该节点只有一个子树,该节点的父亲指向节点之子108 void remove_both(binaryNode* node)109 {110 binaryNode* temp = min(node->rson);111 BST::swap(temp, node);112 remove(temp);113 }//不写交换而要考虑很多情况,所以为了方便。只转移数据114 115 int sz;116 int hi;117 binaryNode* top;118 };119 templatebinaryNode* BST::find_it(const T& data)120 {121 binaryNode* temp = root();122 while (is_exist(temp)||temp->data==data)123 {124 if (temp->data == data)return temp;125 else if (data > temp->data) {126 if (temp->rson)127 temp = temp->rson;128 else break;129 }130 else {131 if (temp->lson)132 temp = temp->lson;133 else break;134 }135 }136 return nullptr;137 }138 139 140 templatebinaryNode* BST::floor(const T& data)141 {142 binaryNode* close = top;143 while (is_exist(close))144 {145 if (data == close->data)return close;146 if (data > close->data)147 {148 if (close->rson)149 close = close->rson;150 else break;151 }152 else153 {154 if (close->lson)155 close = close->lson;156 else break;157 }158 }159 while (datadata)160 { binaryNode* temp = close;161 close = close->father;162 if (temp != close->lson)163 return close;164 }165 return close;166 }167 168 169 templatebinaryNode* BST::ceil(const T& data)170 {171 binaryNode* close = top;172 while (is_exist(close))173 {174 if (data == close->data)return close;175 if (data > close->data)176 {177 if (close->rson)178 close = close->rson;179 else break;180 }181 else182 {183 if (close->lson)184 close = close->lson;185 else break;186 }187 }188 while (data > close->data)189 {190 binaryNode* temp = close;191 close = close->father;192 if (temp != close->rson)193 return close;194 }195 return close;196 }197 198 templatevoid BST::insert(const T& data) {//利用BST的性质找到该插入的地方插入199 if (!top)top = new binaryNode(data);200 else201 { binaryNode* temp = search(data);202 if(!temp||temp->data==data) 203 {204 cout << "you have inserted same data in BST!" << endl; abort();205 }206 else{207 if (data > temp->data) 208 {209 temp->rson = new binaryNode(data);210 temp->rson->father = temp;211 }212 else213 {214 temp->lson = new binaryNode(data);215 temp->lson->father = temp;216 }217 218 }219 220 }221 sz = checksz(top);222 hi = checkhi(top);223 }224 225 templatevoid BST::remove(const T& data) {226 if (!top) { cout << "top is not exist!" << endl; abort(); }227 binaryNode* temp = find_it(data);228 if (!temp) { cout << "we can't find it that you want to remove!" << endl; abort(); }229 remove(temp);230 sz = checksz(top);231 hi = checkhi(top);232 }233 templatevoid BST::remove(binaryNode*node) {234 if (is_exist(node))235 {236 if (node->lson && node->rson)237 remove_both(node);//最棘手的两个238 else239 remove_one(node);//处理单个子树节点240 }241 else242 remove_leaf(node);//表示是个孤单的叶子结点243 }244 245 templatebinaryNode* BST::search(const T& data) {246 if (top->data == data)return top;247 binaryNode* temp = root();248 while (is_exist(temp)) {249 if (temp->data == data)250 return nullptr;251 if (data > temp->data)252 if (temp->rson)253 temp = temp->rson;254 else break;255 else if (data < temp->data)256 if (temp->lson)257 temp = temp->lson;258 else break;259 }260 return temp;261 }262 263 int main()264 {265 BST<char>test;266 test.insert({ 's','a','f','k','d','p','c','g'});267 test.insert('z');268 cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;269 test.inorder_traverse(test.root());270 cout << endl;271 test.remove('k');272 cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;273 test.inorder_traverse(test.root());274 cout << endl;275 test.remove('a');276 cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;277 test.inorder_traverse(test.root());278 cout << endl;279 test.remove('d');280 cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;281 test.inorder_traverse(test.root());282 cout << endl;283 test.remove('p');284 cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;285 test.inorder_traverse(test.root());286 cout << endl;287 test.remove('s');288 cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;289 test.inorder_traverse(test.root());290 cout << endl;291 test.remove('f');292 cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;293 test.inorder_traverse(test.root());294 cout << endl;295 test.remove('g');296 cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;297 test.inorder_traverse(test.root());298 cout << endl;299 test.remove('c');300 cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;301 test.inorder_traverse(test.root());302 cout << endl;303 304 }
binaryNode.hpp
1 #pragma once 2 #pragma once 3 template<class T>struct binaryNode { 4 binaryNode(T data, binaryNode* father = NULL, binaryNode* lson = NULL, binaryNode* rson = NULL) : 5 father(father), lson(lson), rson(rson), data(data) {} 6 binaryNode* father; 7 binaryNode* lson; 8 binaryNode* rson; 9 T data;10 //auto insertls(T data) {11 // return lson = new binaryNode(data, this);12 //}13 //auto insertrs(T data) {14 // return rson = new binaryNode(data, this);15 //}16 //auto remove() {17 // delete rson;18 // delete lson;19 // delete this;20 //}21 /*bool operator<(const binaryNode* node) { return node->data < data; }22 bool operator>(const binaryNode* node) { return !(node < this); }23 bool operator==(const binaryNode* node) { return data == node->data; }*/24 25 };