����λ�ã���ҳ > �����̳� > �̳� > BST ���������� BinarySearchTree C++ʵ��(�ݹ�/�ǵݹ�)
Ŀ¼������������������ý�����;���������������ܷ��������������IJ������Ҳ���ɾ������ʵ��BSTree.hpptest.cc ���������� �������� ����������(BST,Binary Search Tree) �����������ֳƶ�������������������һ�ÿ����������Ǿ����������ʵĶ�����: ��������������Ϊ��
����������(BST,Binary Search Tree)
�����������ֳƶ�������������������һ�ÿ����������Ǿ����������ʵĶ�����:
int a[] = {8, 3, 1, 10, 6, 4, 7, 14, 13};
����������/���������Ҳ�ƶ���������,��Ϊ����������������������������
������������������һ��С�ڸ�,������һ�����ڸ�, ��϶���ݹ����� ���Եõ�
�����������ҽڵ��������������ڵ�,�����������ҽڵ��������������ڵ�.
������������ڵ�������������С�ڵ�,������������ڵ�������������С�ڵ�.
��������������С�ڵ���������������ڵ�,���ڵ��������������ҽڵ�
ʵ���������ֱ��ʹ������������,���Ǹ��������������ĸ�Ч��������,��������Ϊʵ�õĸ߽����ݽṹ,����ƽ�����������(AVL��,�����)��...
������:�Ž�ϵͳ,����ϵͳ��...
������:ͨѶ¼
�����ɾ�������������Ȳ��ң�����Ч�ʴ����˶����������и������������ܡ�
����n�����Ķ�������������ÿ��Ԫ�ز��ҵĸ�����ȣ������������ƽ�����ҳ����ǽ���ڶ�������������ȵĺ����������Խ���Ƚϴ���Խ�ࡣ
������ͬһ���ؼ��뼯�ϣ�������ؼ������Ĵ���ͬ�����ܵõ���ͬ�ṹ�Ķ�����������
��������£�����������Ϊ��ȫ������(���߽ӽ���ȫ������)����ƽ���Ƚϴ���Ϊ��$log_2 N$ (
$log_2 N$
)
�������£������������˻�Ϊ��֧��(�������Ƶ�֧)����ƽ���Ƚϴ���Ϊ��$\frac{N}{2}$ (
$\frac{N}{2}$
)
�ر��
ͬ��һ������,����˳��ͬ,�õ��Ķ�����Ҳ��ͬ
�������ֵ�Ѵ���ʱ,����ʧ��(������multi)
���Ȳ���Ԫ���Ƿ��ڶ����������У���������ڣ��򷵻�.
����,�������Ľṹ����,���Եõ�3�����
�������д�ɾ���ڵ���4�������ʵ�����:
Ҫɾ���Ľ���޺��ӽ��ʱ,ֱ��ɾ��
Ҫɾ���Ľ��ֻ�����ӻ��Һ���ʱ,�����ӻ��Һ��Ӹ�����
Ҫɾ���Ľ������Ǹ��׵����ӻ������Һ���,��2*2�����(Ҫɾ���Ľ���Ǹ��׵����ӻ��Һ���)
���Һ��Ӷ��ǿ�ʱ,Ҳ�������,��˿��Ժϲ��޺��ӽ�����
��1��ǰ����,ǡ���Ǹ��ڵ�,Ҳ��һ�����(������һ��������������)
Ҫɾ���Ľ�������Һ���(����)ʱ,��Ҫ��һ����Ҫ����������ҲҪ��������С�Ľڵ�������.
���� �ݹ鶨�� ��֪,ֻ�����ӵ����ҽ����Һ��ӵ��������������,��ѡһ����
��ѡ��ʹ���Һ��ӵ�������ʱ,�������������(���Dz��Ǹ��޹�)
Ҫɾ���Ľ�������������С���ǡ����Ҫɾ�������Һ���.
Ҫɾ���Ľ�������������С���û���Һ���.
Ҫɾ���Ľ�������������С������Һ���
(��ͼ��������)
template
struct BSTreeNode {
BSTreeNode* _left;
BSTreeNode* _right;
K _key;
BSTreeNode(K key)
:_key(key),_left(nullptr),_right(nullptr)
{}
};
template
class BSTree {
public:
using Node = BSTreeNode;
BSTree() = default;
BSTree(const BSTree& bst) {
_root = Copy(bst._root);
}
BSTree& operator=(BSTree bst) { //��������
swap(_root,bst.root);
return *this;
}
~BSTree() {
Destroy(_root);
}
public:
bool Insert(const K& key) {
if (_root == nullptr) {
_root = new Node(key);
_root->_key = key;
return true;
}
BSTreeNode* cur = _root;
BSTreeNode* parent = _root;
while (cur) {
if (key < cur->_key) {
parent = cur;
cur = cur->_left;
}
else if (key > cur->_key) {
parent = cur;
cur = cur->_right;
}
else {
return false;
}
}
//�߳�Ñ��,˵�����в����ڸýڵ�, ���Բ���
cur = new BSTreeNode(key);
if (key < parent->_key) {
parent->_left = cur;
}
else {
parent->_right = cur;
}
return true;
}
bool Find(const K& key) {
if (_root == nullptr) return false;
Node* cur = _root;
while (cur) {
if (key < cur->_key) {
cur = cur->_left;
}
else if (key > cur->_key) {
cur = cur->_right;
}
else {
return true;
}
}
// ��Ñ������,˵��û����
return false;
}
bool Erase(const K& key) {
if (_root == nullptr) return false;
Node* cur = _root;
Node* parent = _root;
while (cur) {
if (key < cur->_key) {
parent = cur;
cur = cur->_left;
}
else if (key > cur->_key) {
parent = cur;
cur = cur->_right;
}
else {
//û������
if (cur->_left == nullptr) {
if (cur == _root) {
_root = cur->_right;
}
else if (parent->_left == cur) {
parent->_left = cur->_right;
}
else {
parent->_right = cur->_right;
}
delete cur;
return true;
}
//û���Һ���
else if (cur->_right == nullptr) {
if (cur == _root) {
_root = cur->_left;
}
if (parent->_left == cur) {
parent->_left = cur->_left;
}
else {
parent->_right = cur->_left;
}
delete cur;
return true;
}
//�����Һ���
else {
//���Һ���(����)����С���/������
Node* rightMin = cur->_right; //��ȷ��Ϊ��
Node* rightMinParent = cur;
while (rightMin->_left) {
rightMinParent = rightMin;
rightMin = rightMin->_left;
}
// ɾ����������С�����3�����(���Dz��Ǹ��޹�)
//1. Ҫɾ���Ľ����������С���ǡ�����Լ����Һ���.
//2. Ҫɾ���Ľ����Һ��ӵ���������������û���Һ���.
//3. Ҫɾ���Ľ����Һ��ӵ������������������Һ���.
//���۽���: ����ɾ����������,����ɾ��rightMin����
K tmp = rightMin->_key;
Erase(rightMin->_key); //ֻ�ܴӸ���ʼ����,������ʧ,���Ƕ��ֲ��Һܿ�,��ʧ����(�������,BSTֻѧϰ��)
cur->_key = tmp;
return true;
} //�����Һ��ӵ����
} //�ҵ���_���������Ĺ���
}//Ñ���ҵĹ���
//Ñ������,˵��û�ҵ�
return false;
}//Erase [end]
void InOrder() {
_InOrder(_root);
std::cout << std::endl;
}
bool InsertR(const K& key) {
_InsertR(_root, key);
}
bool EraseR(const K& key) {
return _EraseR(_root,key);
}
private:
//�˴�����ֵ����ʹ��ָ������,��Ȼһ������¿���ʹ��(���Ƽ�),����Ŀǰ�������ÿ�ֵ.
Node* Copy(const Node* root) {
if (root == nullptr) {
return nullptr;
}
Node* newRoot = new Node(root->_key);
newRoot->_left = Copy(root->_left);
newRoot->_right = Copy(root->_right);
return newRoot;
}
//�ò�����������ν,��ϰ��������
//(�����ӽڵ�ʱ,���ڵ�������Ա���Ϊ����ָ��,���ǽ���������ҲҪ������,ָ�����Ҳ��֮����)
void Destroy(Node*&root) {
if (root == nullptr) {
return ;
}
Destroy(root->_left);
Destroy(root->_right);
std::cout<_key<<" ";
delete root; //�ͷż��Զ��ÿ�
}
//��ϰ�ݹ�+���� -- ������Ӽ��
bool _EraseR(Node*& root, const K&key) {
//�ߵ���,˵��û�ҵ�,����false
if (root == nullptr) {
return false;
}
//�������ұ�,С�������
if (key > root->_key) {
return _EraseR(root->_right,key);
}
else if(key_key) {
return _EraseR(root->_left,key);
}
//�ҵ���
else {
if (root->_left == nullptr) {
Node* del = root;
root = root->_right;
delete del;
return true;
}
else if (root->_right == nullptr) {
Node* del = root;
root = root->_left;
delete del;
return true;
}
//�����Һ���
else {
Node* leftMax = root->_left;
//�������������
while (leftMax->_right) {
leftMax = leftMax->_right;
}
std::swap(root->_key, leftMax->_key);
return _EraseR(root->_left, key); //ֱ�Ӵ����ӿ�ʼ�ݹ�ɾ��.
}
}
}
//��ϰ�ݹ�+����ָ����淨,����ϰ
bool _InsertR(Node*& root, const K& key) { //���õ�����,��ջֱ֡�ӷ���ʵ��
if (root == nullptr) {
root == new Node(key);
return true;
}
if (key == root->_key) return false;
return (key > root->_key) ? _InsertR(root->_right, key) : _InsertR(root->_left, key);
}
void _InOrder(Node* root) {
if (root == nullptr) return;
_InOrder(root->_left);
std::cout << root->_key << " ";
_InOrder(root->_right);
}
private:
BSTreeNode* _root = nullptr;
};
void test() {
int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
BSTree bst;
for (int i : a) {
bst.Insert(i);
}
bst.InOrder();
////Find
//std::cout << std::boolalpha << bst.Find(8) << std::endl; //true
//std::cout << std::boolalpha << bst.Find(9) << std::endl; //false
BSTree cp(bst);
cp.InOrder();
//���������ӵ������������
bst.Erase(8); //1. Ҫɾ���Ľ�������������С���ǡ����Ҫɾ�������Һ���.
bst.Erase(10); //2. Ҫɾ���Ľ�������������С���û���Һ���
bst.Insert(5); //�������Һ��ӵ���С���
bst.Erase(3); //3. Ҫɾ���Ľ�������������С������Һ���
bst.Erase(4);
bst.Erase(7);
bst.Erase(1);
bst.Erase(14);
bst.Erase(13);
bst.Erase(6);
bst.Erase(5);
bst.InOrder();
//��ֹ��ʽ������������ --> ˫���ͷ�
//bst.~BSTree();
//cp.~BSTree();
}
int main() {
test();
}
ʹ��Blender���ɳ���ģ��
�Ķ�ȫ����������ERA5�����ط���
�Ķ�Xpath���������﷨
�Ķ�����ѧϰ�������繹�����£�
�Ķ���ΪMateƷ��ʢ�䣺HarmonyOS NEXT�ӳ�����Ϸ���ܵõ�����ͷ�
�Ķ�ʵ�ֶ��󼯺���DataTable���໥ת��
�Ķ�Ӳ�̵Ļ���֪ʶ��ѡ��ָ��
�Ķ�������й��ƶ��ı�ͼ��ײ�
�Ķ�����NEXTԪ�����������ѿ����ϼ���Ʒ
�Ķ��ᳲ���С������������Ƽ��رշ���
�Ķ������ArcMap�����н���դ��ͼ���ز�������
�Ķ��㷨�����ݽṹ 1 - ģ��
�Ķ���Ѷ�����߿ͷ���Ӫ��ϵͳ����
�Ķ���Ѷ��Ƶҹ��ģʽ���ý̳�
�Ķ����ں���NEXT��Ѫ���Ŵ���������������
�Ķ�5. Spring Cloud OpenFeign ����ʽ WebService �ͻ��˵ij���ϸʹ��
�Ķ�Java����ģʽ����̬�����Ͷ�̬�����ĶԱȷ���
�Ķ�Win11�ʼDZ����Զ�����Ӧ�õ���ɫ����ʾ����
�Ķ�˼�� V1.5.6 ��׿��
��ս�귨 V7.5.0 ��׿��
У��������������׵������� V1.0 ��׿��
��˸֮�� V1.9.7 ��׿��
������Ե����� v1.0.4 ��׿��
������֮ŠV5.2.3 ��׿��
��������������Դ V1.0 ��׿��
���֮Ϣ V1.0 ��׿��
��ħ������������䣩 V1.0 ��׿��
���ں�������ϵ�����������������վ�����������������Ƽ�����
Ƶ�� ����Ƶ��������ר������������׿�������app����
�Ƽ� ��Ô���������°��������ܿ������ز���
���� ����ɫ������������ ���������ս������������
ɨ��ά�����������ֻ��汾��
ɨ��ά����������΢�Ź��ںţ�
��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]
��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������