c++ BST继承自二叉树
1 #include <iostream> 2 #include<binaryNode.hpp> 3 #include<cassert> 4 #include<queue> 5 #include<vector> 6 7 using namespace std; 8 9 template<class T>binaryNode<T>* getNode(T num) 10 { 11 return new binaryNode<T>(num); 12 13 }; 14 15 template<class T>class binaryTree { 16 protected: 17 queue<binaryNode<T>*>que; 18 public: 19 20 binaryNode<T>* top = new binaryNode<T>(); 21 22 binaryTree() = default; 23 24 binaryTree(T data) 25 { 26 sz = 1; 27 top->data = data; 28 que.push(top); 29 vec.push_back(0); 30 vec.push_back(top); 31 } 32 33 binaryNode<T>* operator[](size_t num) { 34 if (num == 1)return top; 35 else 36 { 37 return vec[num]; 38 } 39 40 41 } 42 virtual ~binaryTree() { 43 delete top; 44 } 45 virtual size_t size() const { return sz; } 46 47 virtual size_t height()const { 48 return hi; 49 } 50 51 virtual void insertls(int num, T data) { insertls(num, new binaryNode<T>(data)); } 52 53 virtual void insertrs(int num, T data) { insertrs(num, new binaryNode<T>(data)); } 54 55 virtual void insertls(int num, binaryNode<T>* node) { 56 binaryNode<T>* get = this->operator[](num); 57 assert(get != nullptr); 58 if (get->rson == NULL)hi++; 59 sz += node->size(); 60 get->lson = node; 61 vec.push_back(get->lson); 62 } 63 64 virtual void insertrs(int num, binaryNode<T>* node) { 65 binaryNode<T>* get = this->operator[](num); 66 assert(get != nullptr); 67 if (get->lson == NULL)hi++; 68 sz += node->size(); 69 get->rson = node; 70 vec.push_back(get->rson); 71 } 72 73 74 virtual void insertaslstree(int num, const binaryTree<T>& tree) { 75 sz += tree.size() - tree.top->size(); 76 insertls(num, tree.top); 77 hi--; 78 hi += tree.height(); 79 } 80 81 virtual void insertasrstree(int num, const binaryTree<T>& tree) { 82 sz += tree.size() - tree.top->size(); 83 84 insertrs(num, tree.top); 85 hi--; 86 hi += tree.height(); 87 } 88 89 void preorder_traverse(binaryNode<T>* node) { 90 if (!node)return; 91 std::cout << node->data << " "; 92 preorder_traverse(node->lson); 93 preorder_traverse(node->rson); 94 } 95 96 void inorder_traverse(binaryNode<T>* node) { 97 if (!node)return; 98 inorder_traverse(node->lson); 99 std::cout << node->data << " "; 100 inorder_traverse(node->rson); 101 } 102 103 void postorder_traverse(binaryNode<T>* node) { 104 if (!node)return; 105 postorder_traverse(node->lson); 106 postorder_traverse(node->rson); 107 std::cout << node->data << " "; 108 } 109 110 void remove(int num) { 111 binaryNode<T>* get = this->operator[](num); 112 binaryNode<T>* father = this->operator[](num / 2); 113 if (get->lson || get->rson) 114 { 115 std::cout << "不允许remove" << endl; 116 } 117 else { 118 if (father->lson && father->rson); 119 else hi--; 120 sz--; 121 get->remove(); 122 } 123 } 124 125 bool empty() { return top == nullptr ? true : false; } 126 127 virtual void traverse_level() { 128 if (!que.empty()) { 129 binaryNode<T>* node = que.front(); 130 cout << que.front()->data << " "; 131 que.pop(); 132 if (!node) 133 return; 134 if (node->lson) 135 que.push(node->lson); 136 if (node->rson) 137 que.push(node->rson); 138 traverse_level(); 139 } 140 } 141 142 143 private: 144 size_t sz = 0; 145 size_t hi = top->height(); 146 vector<binaryNode<T>*>vec; 147 148 }; 149 template <class T>class BST :public binaryTree<T> { 150 public: 151 152 BST() = default; 153 154 size_t size() { return sz; } 155 156 size_t height() { return hi; } 157 158 auto changehi(binaryNode<T>* node) { 159 if (node == NULL) 160 return 0; 161 else 162 { 163 return max(changehi(node->lson), changehi(node->rson)) + 1; 164 165 } 166 } 167 BST(T data) 168 { 169 this->que.push(this->top); 170 sz = 1; 171 this->top->data = data; 172 173 } 174 void swap(binaryNode<T>* left, binaryNode<T>* right) { 175 T t = left->data; 176 left->data = right->data; 177 right->data = t; 178 } 179 binaryNode<T>* find(binaryNode<T>* node) { 180 binaryNode<T>* cp = this->top; 181 for (size_t i = 0; i < height(); ++i) { 182 if (cp->data == node->data) 183 return cp; 184 else if (node->data < cp->data) 185 { 186 if (cp->lson) 187 cp = cp->lson; 188 } 189 else 190 { 191 if (cp->rson) 192 cp = cp->rson; 193 } 194 } 195 return NULL; 196 } 197 binaryNode<T>* search(binaryNode<T>* node) { 198 binaryNode<T>* cp = this->top; 199 while (is_exist(cp)) { 200 if (node->data <= cp->data) 201 { 202 if (cp->lson) 203 cp = cp->lson; 204 else 205 break; 206 } 207 else 208 { 209 if (cp->rson) 210 cp = cp->rson; 211 else 212 break; 213 } 214 } 215 return cp; 216 } 217 218 virtual void insert(binaryNode<T>* node) { 219 binaryNode<T>* tmp = search(node); 220 sz++; 221 if (tmp->data > node->data) 222 { 223 tmp->lson = node; 224 tmp->lson->father = tmp; 225 } 226 else 227 { 228 tmp->rson = node; 229 tmp->rson->father = tmp; 230 } 231 hi = changehi(this->top); 232 } 233 void remove(binaryNode<T>* node) { 234 { 235 traverse(); 236 binaryNode<T>* tmp = find(node); 237 assert(tmp); 238 if (tmp->father) { 239 if (tmp->data > tmp->father->data) { 240 if (tmp->lson && !tmp->rson) 241 tmp->father->rson = tmp->lson; 242 else if (!tmp->lson && tmp->rson) 243 tmp->father->rson = tmp->rson; 244 else if (tmp->lson && tmp->rson) { 245 for (size_t i = 0; i < vec.size(); ++i) 246 { 247 if (vec[i] == tmp) 248 { 249 binaryNode<T>* n = vec[i + 1]; 250 remove(vec[i + 1]); 251 swap(vec[i], n); 252 253 } 254 } 255 } 256 else 257 tmp->father->rson = NULL; 258 } 259 else { 260 if (tmp->lson && !tmp->rson) 261 tmp->father->lson = tmp->lson; 262 else if (!tmp->lson && tmp->rson) 263 tmp->father->lson = tmp->rson; 264 else if (tmp->lson && tmp->rson) { 265 for (size_t i = 0; i < vec.size(); ++i) 266 { 267 if (vec[i] == tmp) 268 { 269 binaryNode<T>* n = vec[i + 1]; 270 remove(vec[i + 1]); 271 swap(vec[i], n); 272 } 273 } 274 } 275 else tmp->father->lson = NULL; 276 } 277 278 279 } 280 else { 281 for (size_t i = 0; i < vec.size(); ++i) 282 { 283 if (vec[i] == this->top) 284 { 285 binaryNode<T>* n = vec[i + 1]; 286 remove(vec[i + 1]); 287 swap(vec[i], n); 288 } 289 } 290 } 291 } 292 hi = changehi(this->top); 293 } 294 295 bool is_exist(binaryNode<T>* node) { 296 if (node->lson || node->rson) 297 return true; 298 else 299 return false; 300 } 301 302 void inorder_traverse(binaryNode<T>* node) { 303 if (!node)return; 304 inorder_traverse(node->lson); 305 { 306 cout << node->data << " "; 307 vec.push_back(node); 308 } 309 inorder_traverse(node->rson); 310 } 311 312 void traverse() { 313 for (auto i : vec) 314 cout << i->data << endl; 315 } 316 317 private: 318 size_t sz; 319 size_t hi = 1; 320 vector<binaryNode<T>*>vec; 321 }; 322 323 324 int main() { 325 326 /*binaryTree<char>s('i'); 327 s.insertls(1, getNode('d')); 328 s.insertls(2, getNode('c')); 329 s.insertls(3, getNode('a')); 330 s.insertrs(4, getNode('b')); 331 s.insertrs(2, getNode('h')); 332 s.insertls(6, getNode('f')); 333 s.insertls(7, getNode('e')); 334 s.insertrs(7, getNode('g')); 335 s.insertrs(1, getNode('l')); 336 s.insertls(10, getNode('k')); 337 s.insertrs(10, getNode('n')); 338 s.insertls(11, getNode('j')); 339 s.insertls(12, getNode('m')); 340 s.insertrs(12, getNode('p')); 341 s.insertrs(15, getNode('o'));*/ 342 /*s.insertls(4, getNode('a')); 343 s.insertrs(8, getNode('b')); 344 s.insertls(5, getNode('f')); 345 s.insertls(10, getNode('e')); 346 s.insertrs(10, getNode('g')); 347 s.insertls(3, getNode('k')); 348 s.insertls(6, getNode('j')); 349 s.insertrs(3, getNode('n')); 350 s.insertrs(7, getNode('p')); 351 s.insertls(7, getNode('m')); 352 s.insertls(15, getNode('o'));*/ 353 //cout << s.size(); 354 //cout << s[18]->data; 355 //cout << "size:" << s.size(); 356 //s.traverse_level(); 357 BST<int>s(4); 358 s.insert(getNode(2)); 359 s.insert(getNode(9)); 360 s.insert(getNode(3)); 361 s.insert(getNode(1)); 362 s.insert(getNode(7)); 363 s.insert(getNode(6)); 364 //cout<<s.search(getNode(9))->data; 365 s.insert(getNode(10)); 366 s.inorder_traverse(s.top); 367 s.remove(getNode(4)); 368 //s.traverse(); 369 //s.remove(getNode(4)); 370 cout << endl; 371 s.traverse_level(); 372 //s.inorder_traverse(s.top); 373 /*s.insert(getNode(8)); 374 375 s.insert(getNode(5));*/ 376 //s.inorder_traverse(s.top); 377 //cout << s.changehi(s.top); 378 // s.insertls(1,getNode('a')); 379 380 381 }
1 #pragma once 2 template<class T>struct binaryNode { 3 binaryNode() = default; 4 binaryNode(T data, binaryNode<T>* father = NULL, binaryNode<T>* lson = NULL, binaryNode<T>* rson = NULL) : 5 father(father), lson(lson), rson(rson), data(data) {} 6 binaryNode<T>* father; 7 binaryNode<T>* lson; 8 binaryNode<T>* rson; 9 T data; 10 size_t height() { 11 size_t height = 1; 12 if (lson == NULL && rson == NULL); 13 else 14 height++; 15 return height; 16 } 17 size_t size() { 18 size_t sz = 1; 19 lson == NULL ? sz : sz++; 20 rson == NULL ? sz : sz++; 21 return sz; 22 }; 23 auto insertls(T data) { 24 return lson = new binaryNode<T>(data, this); 25 } 26 auto insertrs(T data) { 27 return rson = new binaryNode<T>(data, this); 28 } 29 auto remove() { 30 delete rson; 31 delete lson; 32 delete this; 33 } 34 bool operator<(const binaryNode<T>* node) { return node->data < data ? true : false; } 35 bool operator>(const binaryNode<T>* node) { return !(node < this); } 36 bool operator==(const binaryNode<T>* node) { return data == node->data; } 37 38 };
所望隔山海