📅  最后修改于: 2023-12-03 14:39:56.408000             🧑  作者: Mango
二叉搜索树(Binary Search Tree,简称BST)是一种常用的数据结构,它具有高效的插入、查找和删除操作。在C++中,我们可以利用类来实现二叉搜索树的完整功能。本文将介绍如何使用C++实现二叉搜索树,并提供完整的代码示例。
以下是C++代码详细实现:
// 定义二叉搜索树节点结构
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 二叉搜索树类
class BST {
public:
// 构造函数
BST() : root(nullptr) {}
// 插入节点方法
void insert(int val) {
TreeNode* newNode = new TreeNode(val);
if (!root) {
root = newNode;
} else {
TreeNode* curr = root;
while (true) {
if (val < curr->val) {
if (curr->left) {
curr = curr->left;
} else {
curr->left = newNode;
break;
}
} else {
if (curr->right) {
curr = curr->right;
} else {
curr->right = newNode;
break;
}
}
}
}
}
// 删除节点方法
TreeNode* remove(TreeNode* curr, int val) {
if (curr == nullptr) {
return curr;
}
if (val < curr->val) {
curr->left = remove(curr->left, val);
} else if (val > curr->val) {
curr->right = remove(curr->right, val);
} else {
if (curr->left == nullptr && curr->right == nullptr) {
delete curr;
curr = nullptr;
} else if (curr->left == nullptr) {
TreeNode* temp = curr;
curr = curr->right;
delete temp;
} else if (curr->right == nullptr) {
TreeNode* temp = curr;
curr = curr->left;
delete temp;
} else {
TreeNode* minRightNode = findMinNode(curr->right);
curr->val = minRightNode->val;
curr->right = remove(curr->right, minRightNode->val);
}
}
return curr;
}
// 查找节点方法
bool search(int val) {
TreeNode* curr = root;
while (curr) {
if (val == curr->val) {
return true;
} else if (val < curr->val) {
curr = curr->left;
} else {
curr = curr->right;
}
}
return false;
}
// 前序遍历方法
void preorderTraversal(TreeNode* curr) {
if (curr) {
cout << curr->val << " ";
preorderTraversal(curr->left);
preorderTraversal(curr->right);
}
}
// 中序遍历方法
void inorderTraversal(TreeNode* curr) {
if (curr) {
inorderTraversal(curr->left);
cout << curr->val << " ";
inorderTraversal(curr->right);
}
}
// 后序遍历方法
void postorderTraversal(TreeNode* curr) {
if (curr) {
postorderTraversal(curr->left);
postorderTraversal(curr->right);
cout << curr->val << " ";
}
}
// 释放内存的方法
void releaseMemory(TreeNode* curr) {
if (curr) {
releaseMemory(curr->left);
releaseMemory(curr->right);
delete curr;
}
}
private:
TreeNode* root;
// 查找最小节点的方法
TreeNode* findMinNode(TreeNode* curr) {
while (curr->left) {
curr = curr->left;
}
return curr;
}
};
// 主函数
int main() {
BST bst;
// 插入节点
bst.insert(5);
bst.insert(3);
bst.insert(7);
bst.insert(2);
bst.insert(4);
bst.insert(6);
bst.insert(8);
// 中序遍历二叉树
cout << "Inorder Traversal: ";
bst.inorderTraversal(bst.getRoot());
cout << endl;
// 查找节点
int target = 6;
if (bst.search(target)) {
cout << "Node " << target << " found in the BST." << endl;
} else {
cout << "Node " << target << " not found in the BST." << endl;
}
// 删除节点
int valToDelete = 7;
bst.remove(valToDelete);
// 中序遍历二叉树
cout << "Inorder Traversal after deleting " << valToDelete << ": ";
bst.inorderTraversal(bst.getRoot());
cout << endl;
// 释放内存
bst.releaseMemory(bst.getRoot());
return 0;
}
以上是一个完整的C++实现的二叉搜索树。可以根据需要进行插入、删除、查找节点,并对二叉树进行中序遍历。同时也提供了方法来释放二叉树占用的内存。
希望这段代码能帮助你理解和实现二叉搜索树。如果有任何问题,请随时提问。