171 lines
4.4 KiB
C++
171 lines
4.4 KiB
C++
|
/**
|
|||
|
* File: binary_search_tree.cpp
|
|||
|
* Created Time: 2022-11-25
|
|||
|
* Author: krahets (krahets@163.com)
|
|||
|
*/
|
|||
|
|
|||
|
#include "../utils/common.hpp"
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
class BinarySearchTree {
|
|||
|
private:
|
|||
|
TreeNode *root;
|
|||
|
|
|||
|
public:
|
|||
|
/* <20><><EFBFBD>췽<EFBFBD><ECB7BD> */
|
|||
|
BinarySearchTree() {
|
|||
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
root = nullptr;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
~BinarySearchTree() {
|
|||
|
freeMemoryTree(root);
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> */
|
|||
|
TreeNode *getRoot() {
|
|||
|
return root;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>ҽڵ<D2BD> */
|
|||
|
TreeNode *search(int num) {
|
|||
|
TreeNode *cur = root;
|
|||
|
// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD>ң<EFBFBD>Խ<EFBFBD><D4BD>Ҷ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
while (cur != nullptr) {
|
|||
|
// Ŀ<><C4BF><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD> cur <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (cur->val < num)
|
|||
|
cur = cur->right;
|
|||
|
// Ŀ<><C4BF><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD> cur <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
else if (cur->val > num)
|
|||
|
cur = cur->left;
|
|||
|
// <20>ҵ<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>ڵ㣬<DAB5><E3A3AC><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>
|
|||
|
else
|
|||
|
break;
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>ڵ<EFBFBD>
|
|||
|
return cur;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> */
|
|||
|
void insert(int num) {
|
|||
|
// <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD>գ<EFBFBD><D5A3><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>
|
|||
|
if (root == nullptr) {
|
|||
|
root = new TreeNode(num);
|
|||
|
return;
|
|||
|
}
|
|||
|
TreeNode *cur = root, *pre = nullptr;
|
|||
|
// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD>ң<EFBFBD>Խ<EFBFBD><D4BD>Ҷ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
while (cur != nullptr) {
|
|||
|
// <20>ҵ<EFBFBD><D2B5>ظ<EFBFBD><D8B8>ڵ㣬ֱ<E3A3AC>ӷ<EFBFBD><D3B7><EFBFBD>
|
|||
|
if (cur->val == num)
|
|||
|
return;
|
|||
|
pre = cur;
|
|||
|
// <20><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD> cur <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (cur->val < num)
|
|||
|
cur = cur->right;
|
|||
|
// <20><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD> cur <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
else
|
|||
|
cur = cur->left;
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>
|
|||
|
TreeNode *node = new TreeNode(num);
|
|||
|
if (pre->val < num)
|
|||
|
pre->right = node;
|
|||
|
else
|
|||
|
pre->left = node;
|
|||
|
}
|
|||
|
|
|||
|
/* ɾ<><C9BE><EFBFBD>ڵ<EFBFBD> */
|
|||
|
void remove(int num) {
|
|||
|
// <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD>գ<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
|
|||
|
if (root == nullptr)
|
|||
|
return;
|
|||
|
TreeNode *cur = root, *pre = nullptr;
|
|||
|
// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD>ң<EFBFBD>Խ<EFBFBD><D4BD>Ҷ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
while (cur != nullptr) {
|
|||
|
// <20>ҵ<EFBFBD><D2B5><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>ڵ㣬<DAB5><E3A3AC><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>
|
|||
|
if (cur->val == num)
|
|||
|
break;
|
|||
|
pre = cur;
|
|||
|
// <20><>ɾ<EFBFBD><C9BE><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD> cur <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (cur->val < num)
|
|||
|
cur = cur->right;
|
|||
|
// <20><>ɾ<EFBFBD><C9BE><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD> cur <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
else
|
|||
|
cur = cur->left;
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>ڵ㣬<DAB5><E3A3AC>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>
|
|||
|
if (cur == nullptr)
|
|||
|
return;
|
|||
|
// <20>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD> = 0 or 1
|
|||
|
if (cur->left == nullptr || cur->right == nullptr) {
|
|||
|
// <20><><EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD> = 0 / 1 ʱ<><CAB1> child = nullptr / <20><><EFBFBD>ӽڵ<D3BD>
|
|||
|
TreeNode *child = cur->left != nullptr ? cur->left : cur->right;
|
|||
|
// ɾ<><C9BE><EFBFBD>ڵ<EFBFBD> cur
|
|||
|
if (cur != root) {
|
|||
|
if (pre->left == cur)
|
|||
|
pre->left = child;
|
|||
|
else
|
|||
|
pre->right = child;
|
|||
|
} else {
|
|||
|
// <20><>ɾ<EFBFBD><C9BE><EFBFBD>ڵ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ڵ㣬<DAB5><E3A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>
|
|||
|
root = child;
|
|||
|
}
|
|||
|
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
|||
|
delete cur;
|
|||
|
}
|
|||
|
// <20>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD> = 2
|
|||
|
else {
|
|||
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> cur <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ<EFBFBD>
|
|||
|
TreeNode *tmp = cur->right;
|
|||
|
while (tmp->left != nullptr) {
|
|||
|
tmp = tmp->left;
|
|||
|
}
|
|||
|
int tmpVal = tmp->val;
|
|||
|
// <20>ݹ<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>ڵ<EFBFBD> tmp
|
|||
|
remove(tmp->val);
|
|||
|
// <20><> tmp <20><><EFBFBD><EFBFBD> cur
|
|||
|
cur->val = tmpVal;
|
|||
|
}
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/* Driver Code */
|
|||
|
int main() {
|
|||
|
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
BinarySearchTree *bst = new BinarySearchTree();
|
|||
|
// <20><>ע<EFBFBD>⣬<EFBFBD><E2A3AC>ͬ<EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɲ<EFBFBD>ͬ<EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><D0BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
vector<int> nums = {8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15};
|
|||
|
for (int num : nums) {
|
|||
|
bst->insert(num);
|
|||
|
}
|
|||
|
cout << endl << "<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ\n" << endl;
|
|||
|
printTree(bst->getRoot());
|
|||
|
|
|||
|
/* <20><><EFBFBD>ҽڵ<D2BD> */
|
|||
|
TreeNode *node = bst->search(7);
|
|||
|
cout << endl << "<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD>Ľڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << node << "<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ֵ = " << node->val << endl;
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> */
|
|||
|
bst->insert(16);
|
|||
|
cout << endl << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> 16 <20><EFBFBD><F3A3ACB6><EFBFBD><EFBFBD><EFBFBD>Ϊ\n" << endl;
|
|||
|
printTree(bst->getRoot());
|
|||
|
|
|||
|
/* ɾ<><C9BE><EFBFBD>ڵ<EFBFBD> */
|
|||
|
bst->remove(1);
|
|||
|
cout << endl << "ɾ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> 1 <20><EFBFBD><F3A3ACB6><EFBFBD><EFBFBD><EFBFBD>Ϊ\n" << endl;
|
|||
|
printTree(bst->getRoot());
|
|||
|
bst->remove(2);
|
|||
|
cout << endl << "ɾ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> 2 <20><EFBFBD><F3A3ACB6><EFBFBD><EFBFBD><EFBFBD>Ϊ\n" << endl;
|
|||
|
printTree(bst->getRoot());
|
|||
|
bst->remove(4);
|
|||
|
cout << endl << "ɾ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> 4 <20><EFBFBD><F3A3ACB6><EFBFBD><EFBFBD><EFBFBD>Ϊ\n" << endl;
|
|||
|
printTree(bst->getRoot());
|
|||
|
|
|||
|
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
|||
|
delete bst;
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|