📅  最后修改于: 2023-12-03 15:29:40.604000             🧑  作者: Mango
Binary search tree (BST) is a commonly used data structure in computer science. It is a tree-like data structure where each node has at most two children, and the left child is always less than or equal to the parent node, while the right child is always greater than the parent node. In this article, we will explore the implementation of BST in C++, along with various operations that can be performed on it.
struct Node{
int data;
Node *left;
Node *right;
};
class BST{
private:
Node *root;
Node* insertHelper(Node *, int);
public:
BST();
Node* getNode(int);
void insert(int);
bool search(int);
Node* deleteNode(Node *, int);
Node* getMinValueNode(Node *);
void inorderTraversal(Node *);
Node* getRoot();
};
The above code defines a simple Node structure that is used to represent a node in the BST. It consists of three fields: int data
for storing integer values, and Node *left
and Node *right
for pointing to the left and right child nodes, respectively.
The BST class consists of a private pointer Node *root
, which points to the root of the tree. It also contains various member functions that can be used to perform operations on the tree.
BST::BST(){
root = nullptr;
}
The constructor initializes the root pointer to nullptr
.
Node* BST::insertHelper(Node *node, int data){
if(node == nullptr){
node = getNode(data);
}
else if(data < node->data){
node->left = insertHelper(node->left, data);
}
else if(data > node->data){
node->right = insertHelper(node->right, data);
}
return node;
}
void BST::insert(int data){
root = insertHelper(root, data);
}
Node* BST::getNode(int data){
Node *temp = new Node;
temp->data = data;
temp->left = nullptr;
temp->right = nullptr;
return temp;
}
The insert
function inserts a node with the given data value into the BST. This function takes the data value as an argument and calls the insertHelper
function to perform the actual insertion.
The insertHelper
function takes two arguments: a pointer to a node (node
) and the data value to be inserted (data
). It checks if node
is null, in which case it creates a new node with the data value and returns it. If node
is not null, it checks if the data value is less than the data value stored in the node. If it is, it recursively calls insertHelper
on the left child of the node. If it is greater, it calls insertHelper
on the right child of the node. The function returns the node after insertion.
The getNode
function creates a new node with the given data value and returns a pointer to it.
bool BST::search(int data){
Node *temp = root;
while(temp != nullptr){
if(temp->data == data){
return true;
}
else if(data < temp->data){
temp = temp->left;
}
else if(data > temp->data){
temp = temp->right;
}
}
return false;
}
The search
function checks if a node with a given data value exists in the BST. This function takes the data value as an argument and returns true if it is found, and false otherwise.
The function starts with a pointer temp
pointing to the root node of the tree. It then iteratively traverses the tree, starting from the root, until it finds a node with the given data value, or until it reaches a leaf node (temp
becomes null). If the data value is less than the data value of the current node, it moves to the left child of the node. If it is greater, it moves to the right child of the node.
Node* BST::getMinValueNode(Node *node){
Node *current = node;
while(current && current->left != nullptr){
current = current->left;
}
return current;
}
Node* BST::deleteNode(Node *node, int data){
if(node == nullptr){
return node;
}
if(data < node->data){
node->left = deleteNode(node->left, data);
}
else if(data > node->data){
node->right = deleteNode(node->right, data);
}
else{
if(node->left == nullptr){
Node *temp = node->right;
delete node;
return temp;
}
else if(node->right == nullptr){
Node *temp = node->left;
delete node;
return temp;
}
Node *temp = getMinValueNode(node->right);
node->data = temp->data;
node->right = deleteNode(node->right, temp->data);
}
return node;
}
The deleteNode
function deletes a node with a given data value from the BST. This function takes the root of the tree (node
) and the data value to be deleted (data
) as arguments.
The function first checks if the current node is null. If it is, it simply returns null. If the data value to be deleted is less than the data value of the current node, it recursively calls deleteNode
on the left child of the node. If it is greater, it calls deleteNode
on the right child of the node. If the data value is equal to the data value of the current node, it checks if the node has zero, one, or two children.
If the node has no left child, it deletes the node and returns its right child. If it has no right child, it deletes the node and returns its left child. If it has two children, it finds the minimum value node in its right subtree (which is the leftmost node in the subtree) and swaps its data with the data of the node to be deleted. It recursively calls deleteNode
on the right child of the node to delete the duplicate value node.
The getMinValueNode
function finds the minimum value node in a subtree, given a node pointer as an argument. It continues traversing the left subtree of the current node until it reaches a leaf node.
void BST::inorderTraversal(Node *node){
if(node == nullptr){
return;
}
inorderTraversal(node->left);
std::cout << node->data << " ";
inorderTraversal(node->right);
}
The inorderTraversal
function performs an inorder traversal of the BST. An inorder traversal of a binary tree visits the left subtree, then the root node, and then the right subtree in that order.
The function recursively calls itself on the left and right subtrees of the current node when they are not null. It then prints the data value of the current node.
Node* BST::getRoot(){
return root;
}
The getRoot
function returns a pointer to the root node of the BST.
In this article, we explored the implementation of BST in C++. We also looked at various operations that can be performed on the BST, such as insertion, deletion, searching, and traversal. BST is a very useful data structure, and its implementation in C++ can be useful in many programming applications.