声明:
该代码基于之前的二叉搜索树代码修改来的,可以点击这里 查看相关笔记;
上一条提到的笔记里面的代码有 bug,可以选择不用看了,bug 如下:
节点使用 location
字段记录是左子还是右子,其实没有必要,整个代码里面使用到的地方并不多;
在删除节点时没有对父子关系进行维护,会存在隐藏问题,比如死循环;
对于新代码主要修改有以下:
修改:
删除 location
字段以及相关操作;
将 add 函数和 delete 函数修改为递归函数;
对可能出现空指针异常的地方添加判断(这个是这次改动中出现的最多的问题)
添加:
添加节点左旋转与右旋转操作,用于实现平衡二叉树;
添加判断是否平衡和是否为搜索树的函数;
再唠一句
这次编写平衡二叉树再次让我体会到了 C++ 空指针有多么恶心,这玩意完全不按照套路来,或者说我也没有摸清楚 C++ 操作指针的套路,可能是因为 Java 和 Python 写多了,对于指针的理解没有那么深刻,算是得到了一个教训吧,只能说慢慢练,还不知道后面写图的代码会遇到多恶心的东西,我这里甚至还没用上模板,要是用上了,估计会更恶心。(恶心!恶心呐!恶心!tui~真恶心)
啰嗦一下左右旋转
这里面涉及到的左旋转和右旋转操作并不复杂,这里大致画一下就是这 4 种情况:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 ================================================ ================================================ ================================================
类定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 #ifndef DATA_STRUCT_NEW_BINARY_TREE_H #define DATA_STRUCT_NEW_BINARY_TREE_H #endif #include <iostream> class BinarySearchTreeNode {private : int value; int height; BinarySearchTreeNode *parent; BinarySearchTreeNode *left; BinarySearchTreeNode *right; public : explicit BinarySearchTreeNode (int value, BinarySearchTreeNode *p) ; BinarySearchTreeNode (); ~BinarySearchTreeNode (); void setValue (int v) ; int getValue () const ; void setHeight (int h) ; int getHeight () const ; void setParent (BinarySearchTreeNode *p) ; BinarySearchTreeNode *getParent () ; void setLeft (BinarySearchTreeNode *l) ; BinarySearchTreeNode *getLeft () ; void setRight (BinarySearchTreeNode *r) ; BinarySearchTreeNode *getRight () ; BinarySearchTreeNode (BinarySearchTreeNode *pNode); }; class BinarySearchTree {private : BinarySearchTreeNode *root; int count; void preShow (BinarySearchTreeNode *node) ; void midShow (BinarySearchTreeNode *node) ; void postShow (BinarySearchTreeNode *node) ; void showTree (BinarySearchTreeNode *arr, int num) ; void destroyTree (BinarySearchTreeNode *node) ; BinarySearchTreeNode *deleteNode (BinarySearchTreeNode *node, int value) ; BinarySearchTreeNode *addNode (BinarySearchTreeNode *node, BinarySearchTreeNode *parentNode, int value) ; static int getNodeHeight (BinarySearchTreeNode *node) ; int getBalanceFactor (BinarySearchTreeNode *node) ; bool isBST (BinarySearchTreeNode *node) ; bool isBalanced (BinarySearchTreeNode *node) ; BinarySearchTreeNode *turnRight (BinarySearchTreeNode *node) ; BinarySearchTreeNode *turnLeft (BinarySearchTreeNode *node) ; public : BinarySearchTree (); ~BinarySearchTree (); BinarySearchTreeNode *getRoot () ; int getCount () const ; void addNode (int value) ; BinarySearchTreeNode *minimum () ; BinarySearchTreeNode *minimum (BinarySearchTreeNode *node) ; BinarySearchTreeNode *maximum () ; BinarySearchTreeNode *maximum (BinarySearchTreeNode *node) ; BinarySearchTreeNode *findNode (int v) ; bool isBST () ; bool isBalanced () ; int deleteNode (int v) ; void preShow () ; void midShow () ; void postShow () ; void showTree () ; };
节点类实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 #include "../header/binary_tree_define.h" using namespace std;BinarySearchTreeNode::BinarySearchTreeNode () { this ->value = -65535 ; this ->height = 1 ; this ->parent = nullptr ; this ->left = nullptr ; this ->right = nullptr ; } BinarySearchTreeNode::BinarySearchTreeNode (int value, BinarySearchTreeNode *p) { this ->value = value; this ->height = 1 ; this ->parent = p; this ->left = nullptr ; this ->right = nullptr ; } BinarySearchTreeNode::~BinarySearchTreeNode () { cout << "释放二分搜索树 " << this ->value << " 结点内存" << endl; } void BinarySearchTreeNode::setValue (const int v) { this ->value = v; } int BinarySearchTreeNode::getValue () const { return this ->value; } void BinarySearchTreeNode::setHeight (int h) { this ->height = h; } int BinarySearchTreeNode::getHeight () const { return this ->height; } void BinarySearchTreeNode::setParent (BinarySearchTreeNode *p) { this ->parent = p; } BinarySearchTreeNode *BinarySearchTreeNode::getParent () { return this ->parent; } void BinarySearchTreeNode::setLeft (BinarySearchTreeNode *l) { this ->left = l; } BinarySearchTreeNode *BinarySearchTreeNode::getLeft () { return this ->left; } void BinarySearchTreeNode::setRight (BinarySearchTreeNode *r) { this ->right = r; } BinarySearchTreeNode *BinarySearchTreeNode::getRight () { return this ->right; } BinarySearchTreeNode::BinarySearchTreeNode (BinarySearchTreeNode *pNode) { this ->value = pNode->value; this ->height = pNode->height; this ->parent = pNode->parent; this ->left = pNode->left; this ->right = pNode->right; }
树代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 #include <cmath> #include "../header/binary_tree_define.h" using namespace std;BinarySearchTree::BinarySearchTree () { this ->root = nullptr ; this ->count = 0 ; } BinarySearchTree::~BinarySearchTree () { destroyTree (this ->root); } void BinarySearchTree::destroyTree (BinarySearchTreeNode *node) { if (node == nullptr ) { return ; } destroyTree (node->getLeft ()); destroyTree (node->getRight ()); delete (node); } BinarySearchTreeNode *BinarySearchTree::getRoot () { return this ->root; } int BinarySearchTree::getCount () const { return this ->count; } int BinarySearchTree::getNodeHeight (BinarySearchTreeNode *node) { if (node == nullptr ) { return 0 ; } return node->getHeight (); } int BinarySearchTree::getBalanceFactor (BinarySearchTreeNode *node) { if (node == nullptr ) { return 0 ; } return this ->getNodeHeight (node->getLeft ()) - this ->getNodeHeight (node->getRight ()); } void BinarySearchTree::addNode (int value) { if (this ->count == 0 && this ->root == nullptr ) { this ->root = new BinarySearchTreeNode (value, nullptr ); this ->count++; return ; } this ->root = this ->addNode (this ->root, nullptr , value); this ->count++; } BinarySearchTreeNode *BinarySearchTree::addNode (BinarySearchTreeNode *node, BinarySearchTreeNode *parentNode, int value) { if (node == nullptr ) { return new BinarySearchTreeNode (value, parentNode); } if (value < node->getValue ()) { node->setLeft (addNode (node->getLeft (), node, value)); } else if (value > node->getValue ()) { node->setRight (addNode (node->getRight (), node, value)); } node->setHeight (1 + max (this ->getNodeHeight (node->getLeft ()), this ->getNodeHeight (node->getRight ()))); int balanceFactor = this ->getBalanceFactor (node); if (balanceFactor > 1 && getBalanceFactor (node->getLeft ()) >= 0 ) { node = this ->turnRight (node); } if (balanceFactor < -1 && getBalanceFactor (node->getRight ()) <= 0 ) { node = this ->turnLeft (node); } if (balanceFactor > 1 && getBalanceFactor (node->getLeft ()) < 0 ) { node->setLeft (this ->turnLeft (node->getLeft ())); node = this ->turnRight (node); } if (balanceFactor < -1 && getBalanceFactor (node->getRight ()) > 0 ) { node->setRight (this ->turnRight (node->getRight ())); node = this ->turnLeft (node); } return node; } BinarySearchTreeNode *BinarySearchTree::findNode (int v) { auto *node = this ->root; while (node != nullptr ) { if (v > node->getValue ()) { node = node->getRight (); } else if (v < node->getValue ()) { node = node->getLeft (); } else { return node; } } return nullptr ; } bool BinarySearchTree::isBST () { return this ->isBST (this ->root); } bool BinarySearchTree::isBST (BinarySearchTreeNode *node) { if (node == nullptr ) { return true ; } if (node->getLeft () == nullptr && node->getRight () == nullptr ) { return true ; } bool leftFlag = this ->isBST (node->getLeft ()); if (node->getRight () == nullptr ) { return leftFlag & (node->getLeft ()->getValue () < node->getValue ()); } bool rightFlag = this ->isBST (node->getRight ()); return leftFlag & rightFlag; } bool BinarySearchTree::isBalanced () { return this ->isBalanced (this ->root); } bool BinarySearchTree::isBalanced (BinarySearchTreeNode *node) { if (node == nullptr ) { return true ; } int balanceFactor = this ->getBalanceFactor (node); if (abs (balanceFactor) > 1 ) { return false ; } return isBalanced (node->getLeft ()) && isBalanced (node->getRight ()); } BinarySearchTreeNode *BinarySearchTree::turnRight (BinarySearchTreeNode *node) { auto *y = node; auto *x = node->getLeft (); if (x->getRight () != nullptr ) { x->getRight ()->setParent (y); y->setLeft (x->getRight ()); } else { y->setLeft (nullptr ); } x->setRight (y); x->setParent (y->getParent ()); y->setParent (x); y->setHeight (max (getNodeHeight (y->getLeft ()), getNodeHeight (y->getRight ())) + 1 ); x->setHeight (max (getNodeHeight (x->getLeft ()), getNodeHeight (x->getRight ())) + 1 ); return x; } BinarySearchTreeNode *BinarySearchTree::turnLeft (BinarySearchTreeNode *node) { auto *y = node; auto *x = node->getRight (); if (x->getLeft () != nullptr ) { x->getLeft ()->setParent (y); y->setRight (x->getLeft ()); } else { y->setRight (nullptr ); } x->setLeft (y); ; x->setParent (y->getParent ()); y->setParent (x); y->setHeight (max (getNodeHeight (y->getLeft ()), getNodeHeight (y->getRight ())) + 1 ); x->setHeight (max (getNodeHeight (x->getLeft ()), getNodeHeight (x->getRight ())) + 1 ); return x; } int BinarySearchTree::deleteNode (int v) { auto *findNode = this ->findNode (v); if (findNode == nullptr ) { cout << "需要删除的节点不存在" << endl; return -1 ; } auto * retNode =this ->deleteNode (this ->root, v); assert (retNode != nullptr ); if (retNode != nullptr ) { this ->root = retNode; delete (findNode); } return v; } BinarySearchTreeNode *BinarySearchTree::deleteNode (BinarySearchTreeNode *node, int value) { BinarySearchTreeNode *retNode; if (value < node->getValue ()) { auto * newNode = this ->deleteNode (node->getLeft (), value); node->setLeft (newNode); if (newNode != nullptr ) { newNode->setParent (node); } retNode = node; } else if (value > node->getValue ()) { auto * newNode = this ->deleteNode (node->getRight (), value); node->setRight (newNode); if (newNode != nullptr ) { newNode->setParent (node); } retNode = node; } else { if (node->getLeft () == nullptr ) { auto * rightNode = node->getRight (); node->setRight (nullptr ); this ->count --; return rightNode; } if (node->getRight () == nullptr ) { auto * leftNode = node->getLeft (); node->setLeft (nullptr ); this ->count--; return leftNode; } else { auto * successor = this ->minimum (node->getRight ()); auto * sucRightChild = this ->deleteNode (node->getRight (), successor->getValue ()); successor->setRight (sucRightChild); if (sucRightChild != nullptr ) { sucRightChild->setParent (successor); } successor->setLeft (node->getLeft ()); successor->getLeft ()->setParent (successor); node->setLeft (nullptr ); node->setRight (nullptr ); this ->count--; retNode = successor; } } retNode->setHeight (1 + max (this ->getNodeHeight (retNode->getLeft ()), this ->getNodeHeight (retNode->getRight ()))); int balanceFactor = this ->getBalanceFactor (retNode); if (balanceFactor > 1 && getBalanceFactor (retNode->getLeft ()) >= 0 ) { retNode = this ->turnRight (retNode); } if (balanceFactor < -1 && getBalanceFactor (retNode->getRight ()) <= 0 ) { retNode = this ->turnLeft (retNode); } if (balanceFactor > 1 && getBalanceFactor (retNode->getLeft ()) < 0 ) { retNode->setLeft (this ->turnLeft (retNode->getLeft ())); retNode = this ->turnRight (retNode); } if (balanceFactor < -1 && getBalanceFactor (retNode->getRight ()) > 0 ) { retNode->setRight (this ->turnRight (retNode->getRight ())); retNode = this ->turnLeft (retNode); } return retNode; } void BinarySearchTree::preShow () { cout << "前序遍历" << endl; preShow (this ->root); cout << endl; cout << "================" << endl; } void BinarySearchTree::preShow (BinarySearchTreeNode *node) { if (node == nullptr ) { return ; } cout << node->getValue () << " - " ; preShow (node->getLeft ()); preShow (node->getRight ()); } void BinarySearchTree::midShow () { cout << "中序遍历" << endl; midShow (this ->root); cout << endl; cout << "================" << endl; } void BinarySearchTree::midShow (BinarySearchTreeNode *node) { if (node == nullptr ) { return ; } midShow (node->getLeft ()); cout << node->getValue () << " - " ; midShow (node->getRight ()); } void BinarySearchTree::postShow () { cout << "后序遍历" << endl; postShow (this ->root); cout << endl; cout << "================" << endl; } void BinarySearchTree::postShow (BinarySearchTreeNode *node) { if (node == nullptr ) { return ; } postShow (node->getLeft ()); postShow (node->getRight ()); cout << node->getValue () << " - " ; } void BinarySearchTree::showTree () { this ->showTree (this ->root, 1 ); } void BinarySearchTree::showTree (BinarySearchTreeNode *arr, int num) { if (num == 0 ) { return ; } int len = 0 ; for (int i = 0 ; i < num; i++) { for (int j = 0 ; j < arr[i].getHeight (); ++j) { cout << " " ; } cout << arr[i].getValue (); } cout << endl; for (int i = 0 ; i < num; i++) { for (int j = 0 ; j < arr[i].getHeight (); ++j) { cout << " " ; } if (arr[i].getLeft () != nullptr ) { cout << "/" ; len++; } if (arr[i].getRight () != nullptr ) { cout << "\\" ; len++; } } cout << endl; auto *newArr = new BinarySearchTreeNode[len]; for (int i = 0 , j = 0 ; i < num; i++) { if (arr[i].getLeft () != nullptr ) { newArr[j] = arr[i].getLeft (); j++; } if (arr[i].getRight () != nullptr ) { newArr[j] = arr[i].getRight (); j++; } } showTree (newArr, len); } BinarySearchTreeNode *BinarySearchTree::minimum () { auto *arr = new BinarySearchTreeNode[1 ]; arr[0 ] = this ->root; return this ->minimum (this ->root); } BinarySearchTreeNode *BinarySearchTree::minimum (BinarySearchTreeNode *node) { if (node == nullptr ) { cout << "该结点不存在" << endl; return nullptr ; } if (node->getLeft () == nullptr ) { return node; } auto *tempNode = node; while (tempNode->getLeft () != nullptr ) { tempNode = tempNode->getLeft (); } return tempNode; } BinarySearchTreeNode *BinarySearchTree::maximum () { return this ->maximum (this ->root); } BinarySearchTreeNode *BinarySearchTree::maximum (BinarySearchTreeNode *node) { if (node == nullptr ) { cout << "该结点不存在" << endl; return nullptr ; } if (node->getRight () != nullptr ) { return node; } auto *tempNode = node; while (tempNode->getRight () == nullptr ) { tempNode = tempNode->getRight (); } return tempNode; }
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include "header/binary_tree_define.h" using namespace std;int main () { auto *tree = new BinarySearchTree (); tree->addNode (9 ); tree->addNode (5 ); tree->addNode (2 ); tree->addNode (4 ); tree->addNode (1 ); tree->addNode (3 ); tree->addNode (7 ); tree->addNode (6 ); tree->addNode (8 ); tree->addNode (15 ); tree->addNode (10 ); tree->addNode (13 ); tree->addNode (14 ); tree->addNode (11 ); tree->addNode (12 ); tree->addNode (16 ); cout << "count is " << tree->getCount () << endl; cout << "是否是二分搜索树 : " << tree->isBST () << endl; cout << "是否是平衡二分搜索树 : " << tree->isBalanced () << endl; tree->midShow (); tree->deleteNode (9 ); tree->deleteNode (7 ); tree->deleteNode (13 ); tree->deleteNode (15 ); tree->deleteNode (14 ); tree->deleteNode (16 ); tree->deleteNode (4 ); tree->midShow (); cout << "count is " << tree->getCount () << endl; cout << "是否是二分搜索树 : " << tree->isBST () << endl; cout << "是否是平衡二分搜索树 : " << tree->isBalanced () << endl; delete (tree); return 0 ; }