BST 二叉搜索树(binary search tree)
二叉搜索树只能为空树,或者是具有下列性质的二叉树
- 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
- 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
- 它的左、右子树也分别为二叉排序树
树节点
1 2 3 4 5 6 7 8 9 10
| #pragma once struct TreeNode { int val; TreeNode* left = nullptr; TreeNode* right=nullptr; TreeNode* parent = nullptr; TreeNode(int v) :val(v) { }; };
|
BST树
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
| #pragma once #include"Tree_Node.h" #include<vector> #include<iostream> class BST { private: TreeNode* _root=nullptr;
void _createBST( std::vector<int>vec); TreeNode* _find(int k); void _insertnode(int a); void _removenode( int a); void _inorderprint(TreeNode * node); void _preorderprint(TreeNode * node); void _postorderprint(TreeNode * node); TreeNode* Predecessor(int a); TreeNode* Successor(int a); public: BST() {}; BST(std::vector<int>vec) { _createBST( vec); }; void inorderprint() { _inorderprint(_root); }; void preorderprint() { _preorderprint(_root); }; void postorderprint() { _postorderprint(_root); }; void insert(int a) { _insertnode(a); }; void remove(int a) { _removenode( a); }; };
void BST::_createBST(std::vector<int>vec) { for (int val : vec) _insertnode(val); }
TreeNode* BST::_find(int key) { TreeNode* cur=_root; while (cur->val!=key && cur!=nullptr) { if (key > cur->val) cur = cur->right; else cur = cur->left; } if(cur!=nullptr) return cur; else { std::cout << "BST do not have this node!\n "; return nullptr; } }
void BST::_insertnode(int a) { TreeNode* node = new TreeNode(a); if (_root == nullptr) { _root = node; return; } else { TreeNode* cur = _root; while (true) { if (node->val > cur->val) { if(cur->right!=nullptr) cur = cur->right; else { cur->right = node; node->parent = cur; return; } } else { if (cur->left!= nullptr) cur = cur->left; else { cur->left = node; node->parent = cur; return; } } } } }
TreeNode* BST::Predecessor(int a) { TreeNode* cur = _find(a); if (cur->left != nullptr) { cur = cur->left; while (cur->right != nullptr) cur = cur->right; return cur; } while (cur->parent != nullptr && cur->parent->left == cur) cur = cur->parent; return cur->parent; }
TreeNode* BST::Successor(int a) { TreeNode* cur = _find(a); if (cur->right != nullptr) { cur = cur->right; while (cur->left != nullptr) cur = cur->left; return cur; } while (cur->parent != nullptr && cur->parent->right != cur) cur = cur->parent; return cur->parent; }
void BST::_removenode(int a) { if (_root == nullptr) throw("error : it's an empty BSTtree!\n"); TreeNode* dnode = _find(a); TreeNode* temp; if (dnode->left == nullptr && dnode->right == nullptr) temp = nullptr; else if (dnode->left == nullptr || dnode->right == nullptr) { temp = (dnode->left == nullptr) ? dnode->right : dnode->left; temp->parent = dnode->parent; } else { temp = Successor(a); TreeNode* temp_right; if (temp->right == nullptr) temp_right = nullptr; else { temp_right = temp->right; temp_right->parent = temp->parent; }
if (temp->parent->right == temp) temp->parent->right = temp_right; else temp->parent->left = temp_right;
if (dnode->parent == nullptr) { temp->right = dnode->right; temp->left = dnode->left; _root = temp; delete dnode; } else if (dnode->parent->right == dnode) { dnode->parent->right = temp; temp->parent = dnode->parent; temp->right = dnode->right; temp->left = dnode->left; delete dnode; } else { dnode->parent->left = temp; temp->parent = dnode->parent; temp->right = dnode->right; temp->left = dnode->left; delete dnode; }
return; } if (dnode->parent != nullptr) { if (dnode->parent->right == dnode) dnode->parent->right = temp; else dnode->parent->left = temp; } else _root = temp; delete dnode; return; }
void BST::_inorderprint(TreeNode * node) { if (node == nullptr) return; std:: cout << node->val << ' '; _inorderprint(node->left); _inorderprint(node->right); } void BST::_preorderprint(TreeNode * node) { if (node == nullptr) return; _preorderprint(node->left); std::cout << node->val << ' '; _preorderprint(node->right); } void BST::_postorderprint(TreeNode * node) { if (node == nullptr) return; _postorderprint(node->left); _postorderprint(node->right); std::cout << node->val<<' '; }
|
测试运行结果
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
| #include"Binary_Search_Tree.h" #include<iostream> using namespace std; int main() { vector<int> vec{8,3,10,1,6,4,7,14,13 }; BST bstree(vec); bstree.inorderprint(); cout<<"\n"; bstree.preorderprint(); cout << "\n"; bstree.postorderprint(); cout << "\ndelete node key = 6\n"; bstree.remove(6); bstree.preorderprint(); cout << "\ndelete node key = 8\n"; bstree.remove(8); bstree.preorderprint(); cout << "\ndelete node key = 13\n"; bstree.remove(13); bstree.preorderprint(); cout << "\n"; system("pause"); return 0; }
|
代码:
https://github.com/ChristmasError/Data_Structure/tree/master/%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%20BST-Binary%20Search%20Tree