注意:该笔记的代码有 bug,已经在平衡二叉树的笔记中修正,点击这里跳转笔记;
先说明一下为什么这个代码不支持重复数据,主要是删改查这三个操作没有对重复数据进行操作,所以不支持(主要是懒的实现了)。
这个代码可能和其他博客里面的实现方式有些许不同,主要在以下几个方面:
- 树节点记录了父节点以及该结点是左子还是右子,需要多维护几个属性;
- 左右操作针对结点对象操作,不对结点里面的值操作,比如删除操作,并不是将替换节点直接 replace,而是将整个替换节点的地址覆盖删除节点的地址;
- 应该没了
下面就是代码实现,至于二叉搜索树的概念和前面一样,自己去查,大佬说的比我更好。
类定义
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
|
#ifndef DATA_STRUCT_NEW_BINARY_TREE_H #define DATA_STRUCT_NEW_BINARY_TREE_H
#endif
#include <iostream>
class BinarySearchTreeNode { private: int value; int location; BinarySearchTreeNode *parent; BinarySearchTreeNode *left; BinarySearchTreeNode *right; public: explicit BinarySearchTreeNode(int value, int location, BinarySearchTreeNode *p);
BinarySearchTreeNode();
~BinarySearchTreeNode();
void setValue(int v);
[[nodiscard]] int getValue() const;
void setLocation(int v);
int getLocation() const;
void setParent(BinarySearchTreeNode *p);
BinarySearchTreeNode *getParent();
void setLeft(BinarySearchTreeNode *l);
BinarySearchTreeNode *getLeft();
void setRight(BinarySearchTreeNode *r);
BinarySearchTreeNode *getRight(); };
class BinarySearchTree { private: BinarySearchTreeNode *root;
int count;
void preShow(BinarySearchTreeNode *node);
void midShow(BinarySearchTreeNode *node);
void postShow(BinarySearchTreeNode *node);
void destroyTree(BinarySearchTreeNode *node);
BinarySearchTreeNode *deleteNode(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);
int deleteNode(int v);
void preShow();
void midShow();
void postShow(); };
|
节点方法实现
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
|
#include "../header/binary_tree_define.h"
using namespace std;
BinarySearchTreeNode::BinarySearchTreeNode() { this->value = -65535; this->location = -1; this->parent = nullptr; this->left = nullptr; this->right = nullptr; }
BinarySearchTreeNode::BinarySearchTreeNode(int value, int location, BinarySearchTreeNode *p) { this->value = value; this->location = location; 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::setLocation(int v) { this->location = v; }
int BinarySearchTreeNode::getLocation() const { return this->location; }
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; }
|
树方法实现
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
|
#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; }
void BinarySearchTree::addNode(int value) { if (this->count == 0 && this->root == nullptr) { this->root = new BinarySearchTreeNode(value, -1, nullptr); this->count++; return; }
auto *node = this->root;
while (node != nullptr) { if (value > node->getValue()) { if (node->getRight() == nullptr) { node->setRight(new BinarySearchTreeNode(value, 1, node)); this->count++; return; } else { node = node->getRight(); } } else { if (node->getLeft() == nullptr) { node->setLeft(new BinarySearchTreeNode(value, 0, node)); this->count++; return; } else { node = node->getLeft(); } } } }
[[maybe_unused]] 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; }
BinarySearchTreeNode *BinarySearchTree::deleteNode(BinarySearchTreeNode *node) {
cout << "处理 " << node->getValue() << " 节点" << endl;
if (node->getRight() == nullptr && node->getLeft() == nullptr) { if (node->getLocation() == 0) { node->getParent()->setLeft(nullptr); } else if(node->getLocation() == 1) { node->getParent()->setRight(nullptr); }
return node; }
BinarySearchTreeNode *replaceNode = nullptr;
auto *parentNode = node->getParent();
if (node->getRight() != nullptr) { replaceNode = this->minimum(node->getRight()); cout << "替换当前 " << node->getValue() << " 节点的是 " << replaceNode->getValue() << " 节点" << endl; deleteNode(replaceNode);
replaceNode->setParent(parentNode); replaceNode->setLocation(node->getLocation()); replaceNode->setLeft(node->getLeft()); replaceNode->setRight(node->getRight()); } else { replaceNode = node->getLeft();
replaceNode->setParent(parentNode); replaceNode->setLocation(node->getLocation()); }
int location = node->getLocation(); if (location == 0) { parentNode->setLeft(replaceNode); } else if (location == 1) { parentNode->setRight(replaceNode); } else { this->root = replaceNode; }
return node; }
int BinarySearchTree::deleteNode(int v) {
auto *findNode = this->findNode(v); if (findNode == nullptr) { cout << "需要删除的节点不存在" << endl; return -1; }
auto *deleteNode = this->deleteNode(findNode); delete (deleteNode); this->count--;
return v; }
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() << " - "; }
BinarySearchTreeNode *BinarySearchTree::minimum() { 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
| #include "header/binary_tree_define.h"
using namespace std;
int main() { auto *tree = new BinarySearchTree(); tree->addNode(9); tree->addNode(4); tree->addNode(2); tree->addNode(5); 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);
tree->midShow();
tree->deleteNode(5);
tree->midShow();
delete (tree);
return 0; }
|
可能会用到的 CMake 文件
1 2 3 4 5 6 7 8 9
| cmake_minimum_required(VERSION 3.20) project(data_struct_new)
set(CMAKE_CXX_STANDARD 14)
# Add header file header directories include_directories(header)
add_executable(data_struct_new main.cpp src/binary_tree_node.cpp src/binary_tree.cpp)
|