查找树
作者:互联网
1 #ifndef BSTREE_H 2 #define BSTREE_H 3 4 #include <iostream> 5 6 template <typename T> 7 class BSTreeNode { 8 public: 9 T key; 10 BSTreeNode *left; 11 BSTreeNode *right; 12 BSTreeNode *parent; 13 BSTreeNode(T value, BSTreeNode *p, BSTreeNode *l, BSTreeNode *r) 14 : key(value), parent(p), left(l), right(r) {} 15 16 T operator*() const { return key; } 17 }; 18 19 template <typename T> 20 class BSTree 21 { 22 public: 23 typedef BSTreeNode<T> Node; 24 typedef BSTreeNode<T>* Node_pointer; 25 26 public: 27 BSTree(); 28 ~BSTree(); 29 30 //前序遍历 31 void preOrder() { preOrder(rootTree); } 32 //中序遍历 33 void inOrder() { inOrder(rootTree); } 34 //后序遍历 35 void postOrder() { postOrder(rootTree); } 36 //最大值 37 T max(); 38 //最小值 39 T min(); 40 // 找结点(x)的后继结点。即,查找"二叉树中数据值大于该结点"的"最小结点"。 41 Node* successor(Node *x); 42 // 找结点(x)的前驱结点。即,查找"二叉树中数据值小于该结点"的"最大结点"。 43 Node* predecessor(Node *x); 44 // (递归实现)查找"二叉树"中键值为key的节点 45 Node* search(T key); 46 //插入 47 void insert(T key); 48 //删除 49 void remove(T key); 50 51 //清除 52 void clear(); 53 private: 54 void insert(Node_pointer &tree, Node *z); 55 Node* remove(Node_pointer &tree, Node *z); 56 57 void preOrder(Node *tree); 58 void inOrder(Node *tree); 59 void postOrder(Node *tree); 60 Node* max(Node *tree); 61 Node* min(Node *tree); 62 Node* search(Node* tree, T key); 63 void destroy(Node_pointer &tree); 64 private: 65 Node *rootTree; 66 }; 67 68 template <typename T> 69 BSTree<T>::BSTree() : rootTree(nullptr) 70 { 71 } 72 73 template <typename T> 74 BSTree<T>::~BSTree() 75 { 76 clear(); 77 } 78 79 template <typename T> 80 void BSTree<T>::insert(T key) 81 { 82 Node* z = new Node(key, nullptr, nullptr, nullptr); 83 insert(rootTree, z); 84 } 85 86 template <typename T> 87 void BSTree<T>::insert(Node_pointer &tree, Node *z) 88 { 89 Node* y = nullptr; 90 Node* x = tree; 91 92 while (x != nullptr) { 93 y = x; 94 if (z->key < x->key) { 95 x = x->left; 96 } 97 else { 98 x = x->right; 99 } 100 } 101 102 z->parent = y; 103 if (y == nullptr) { 104 tree = z; 105 } 106 else if (z->key < y->key) { 107 y->left = z; 108 } 109 else { 110 y->right = z; 111 } 112 } 113 114 115 template <typename T> 116 void BSTree<T>::preOrder(Node *tree) 117 { 118 if (tree == nullptr) return; 119 std::cout << tree->key << " "; 120 preOrder(tree->left); 121 preOrder(tree->right); 122 } 123 124 template <typename T> 125 void BSTree<T>::inOrder(Node *tree) 126 { 127 if (!tree) return; 128 inOrder(tree->left); 129 std::cout << tree->key << " "; 130 inOrder(tree->right); 131 } 132 133 template <typename T> 134 void BSTree<T>::postOrder(Node *tree) 135 { 136 if (!tree) return; 137 postOrder(tree->left); 138 postOrder(tree->right); 139 std::cout << tree->key << " "; 140 } 141 142 template <typename T> 143 void BSTree<T>::clear() 144 { 145 std::cout << "===destroy: "; 146 destroy(rootTree); 147 std::cout << std::endl; 148 } 149 150 template <typename T> 151 void BSTree<T>::destroy(Node_pointer &tree) 152 { 153 if (tree == nullptr) return; 154 155 if (tree->left) { 156 destroy(tree->left); 157 } 158 if (tree->right) { 159 destroy(tree->right); 160 } 161 162 std::cout << tree->key << " "; 163 delete tree; 164 tree = nullptr; 165 } 166 167 template <typename T> 168 typename BSTree<T>::Node* BSTree<T>::max(Node *tree) 169 { 170 if (!tree) return nullptr; 171 172 Node* x = tree; 173 while (x->right) { 174 x = x->right; 175 } 176 return x; 177 } 178 179 template <typename T> 180 T BSTree<T>::max() 181 { 182 Node* x = max(rootTree); 183 184 if (x) { 185 return x->key; 186 } else { 187 return (T)nullptr; 188 } 189 190 } 191 192 template <typename T> 193 typename BSTree<T>::Node* BSTree<T>::min(Node *tree) 194 { 195 if (!tree) return nullptr; 196 197 Node* x = tree; 198 while (x->left) { 199 x = x->left; 200 } 201 return x; 202 } 203 204 template <typename T> 205 T BSTree<T>::min() 206 { 207 Node* x = min(rootTree); 208 209 if (x) { 210 return x->key; 211 } else { 212 return (T)nullptr; 213 } 214 } 215 216 // 找结点(x)的后继结点。即,查找"二叉树中数据值大于该结点"的"最小结点"。 217 template <typename T> 218 typename BSTree<T>::Node* BSTree<T>::successor(Node *x) 219 { 220 if (!x) return nullptr; 221 222 if (x->right) { 223 return min(x->right); 224 } 225 226 //1. x为一个左节点,父节点为后驱 227 //2. x为一个右节点,寻找父节点的父节点。。。,直到找到一个自己作为左孩子的父节点。 228 Node* y = x->parent; 229 while ((y!=nullptr) && (x==y->right)) { 230 x = y; 231 y = x->parent; 232 } 233 234 return y; 235 } 236 237 // 找结点(x)的前驱结点。即,查找"二叉树中数据值小于该结点"的"最大结点"。 238 template <typename T> 239 typename BSTree<T>::Node* BSTree<T>::predecessor(Node *x) 240 { 241 if (!x) return nullptr; 242 243 if (x->left) { 244 return max(x->left); 245 } 246 247 //1. x为一个右节点,父节点为后驱 248 //2. x为一个左节点,寻找父节点的父节点。。。,直到找到一个自己作为右孩子的父节点。 249 Node* y = x->parent; 250 while ((y != nullptr) && (x == y->left)) { 251 x = y; 252 y = x->parent; 253 } 254 255 return y; 256 257 } 258 259 template <typename T> 260 typename BSTree<T>::Node* BSTree<T>::remove(Node_pointer &tree, Node *z) 261 { 262 Node* x = nullptr; 263 Node* y = nullptr; //删除的节点 264 265 //1. 假如z只含有一个子节点或者无子节点,删除z节点 266 //2. 假如z含有两个子节点,则删除后驱节点,然后z节点值替换 267 //y最多一个孩子节点 268 if ((z->left == nullptr) || (z->right == nullptr)) { 269 y = z; 270 } else { 271 y = successor(z); 272 } 273 274 //查找子节点 275 if (y->left != nullptr) { 276 x = y->left; 277 } else { 278 x = y->right; 279 } 280 281 if (x != nullptr) { 282 x->parent = y->parent; 283 } 284 285 if (y->parent == nullptr) { 286 tree = x; 287 } 288 else if (y == y->parent->left) { 289 y->parent->left = x; 290 } 291 else { 292 y->parent->right = x; 293 } 294 295 return y; 296 } 297 298 //删除 299 template <typename T> 300 void BSTree<T>::remove(T key) 301 { 302 Node* x = search(key); 303 if (!x) return; 304 305 Node* y = remove(rootTree, x); 306 if (y) { 307 delete y; 308 } 309 } 310 311 template <typename T> 312 typename BSTree<T>::Node* BSTree<T>::search(Node* tree, T key) 313 { 314 315 if (tree==nullptr || tree->key == key) { 316 return tree; 317 } 318 319 if (key < tree->key) { 320 return search(tree->left, key); 321 } else { 322 return search(tree->right, key); 323 } 324 } 325 326 template <typename T> 327 typename BSTree<T>::Node* BSTree<T>::search(T key) 328 { 329 return search(rootTree, key); 330 } 331 332 #endif
标签:Node,return,nullptr,tree,查找,key,BSTree 来源: https://www.cnblogs.com/vczf/p/12104051.html