����λ�ã���ҳ > �����̳� > �̳� > BST ���������� BinarySearchTree C++ʵ��(�ݹ�/�ǵݹ�)

BST ���������� BinarySearchTree C++ʵ��(�ݹ�/�ǵݹ�)

��Դ������������|��ʱ�䣺2024-08-21 10:22:09 |���Ķ���192��|�� ��ǩ�� a T S C in AR ���� EA c++ �� |����������

Ŀ¼������������������ý�����;���������������ܷ��������������IJ������Ҳ���ɾ������ʵ��BSTree.hpptest.cc ���������� �������� ����������(BST,Binary Search Tree) �����������ֳƶ�������������������һ�ÿ����������Ǿ����������ʵĶ�����: ��������������Ϊ��

����������

��������

����������(BST,Binary Search Tree)

�����������ֳƶ�������������������һ�ÿ����������Ǿ����������ʵĶ�����:

  • ��������������Ϊ�գ��������������нڵ��ֵ��С�ڸ��ڵ��ֵ
  • ��������������Ϊ�գ��������������нڵ��ֵ�����ڸ��ڵ��ֵ
  • ������������Ҳ�ֱ�Ϊ����������

BST ¶þ²æËÑË÷Ê÷ BinarySearchTree C++ʵÏÖ(µÝ¹é/·ÇµÝ¹é)

int a[] = {8, 3, 1, 10, 6, 4, 7, 14, 13};

����������/���������Ҳ�ƶ���������,��Ϊ����������������������������

���ý���

������������������һ��С�ڸ�,������һ�����ڸ�, ��϶���ݹ����� ���Եõ�

  • �����������ҽڵ��������������ڵ�,�����������ҽڵ��������������ڵ�.

  • ������������ڵ�������������С�ڵ�,������������ڵ�������������С�ڵ�.

  • ��������������С�ڵ���������������ڵ�,���ڵ��������������ҽڵ�

��;

ʵ���������ֱ��ʹ������������,���Ǹ��������������ĸ�Ч��������,��������Ϊʵ�õĸ߽����ݽṹ,����ƽ�����������(AVL��,�����)��...

  1. Kģ�ͣ�Kģ�ͼ�ֻ��key��Ϊ�ؼ��룬�ṹ��ֻ��Ҫ�洢Key���ɣ��ؼ��뼴Ϊ��Ҫ��������ֵ��( �ڲ��� ������)
    ���磺��һ������word���жϸõ����Ƿ�ƴд��ȷ�����巽ʽ���£�
    �Դʿ������е��ʼ����е�ÿ��������Ϊkey������һ�ö���������
    �ڶ����������м����õ����Ƿ���ڣ�������ƴд��ȷ����������ƴд����

������:�Ž�ϵͳ,����ϵͳ��...

  1. KVģ�ͣ�ÿһ���ؼ���key��������֮��Ӧ��ֵValue�����ļ�ֵ�ԡ����ַ�
    ʽ����ʵ�����зdz������� ( ͨ��һ��ֵ������һ��ֵ )
    ����Ӣ���ʵ����Ӣ�������ĵĶ�Ӧ��ϵ��ͨ��Ӣ�Ŀ��Կ����ҵ������Ӧ�����ģ�Ӣ
    �ĵ��������Ӧ�������͹���һ�ּ�ֵ�ԣ�
    �ٱ���ͳ�Ƶ��ʴ�����ͳ�Ƴɹ��󣬸������ʾͿɿ����ҵ�����ֵĴ��������������
    �ִ��������͹���һ�ּ�ֵ�ԡ�

������:ͨѶ¼

���������������ܷ���

�����ɾ�������������Ȳ��ң�����Ч�ʴ����˶����������и������������ܡ�

����n�����Ķ�������������ÿ��Ԫ�ز��ҵĸ�����ȣ������������ƽ�����ҳ����ǽ���ڶ�������������ȵĺ����������Խ���Ƚϴ���Խ�ࡣ
������ͬһ���ؼ��뼯�ϣ�������ؼ������Ĵ���ͬ�����ܵõ���ͬ�ṹ�Ķ�����������

BST ¶þ²æËÑË÷Ê÷ BinarySearchTree C++ʵÏÖ(µÝ¹é/·ÇµÝ¹é)

��������£�����������Ϊ��ȫ������(���߽ӽ���ȫ������)����ƽ���Ƚϴ���Ϊ��$log_2 N$ ( $log_2 N$ )
�������£������������˻�Ϊ��֧��(�������Ƶ�֧)����ƽ���Ƚϴ���Ϊ��$\frac{N}{2}$ ( $\frac{N}{2}$ )

�����������IJ���

����

  1. �Ӹ���ʼ�Ƚϣ����ң��ȸ��������ұ��߲��ң��ȸ�С��������߲��ҡ�
  2. �����Ҹ߶ȴΣ��ߵ����գ���û�ҵ������ֵ�����ڡ�

����

  1. ��Ϊ�գ���ֱ�������ڵ㣬��ֵ��rootָ��(��һ���ڵ����root)
  2. �����գ����������������ʲ��Ҳ���λ�ã������½ڵ�

BST ¶þ²æËÑË÷Ê÷ BinarySearchTree C++ʵÏÖ(µÝ¹é/·ÇµÝ¹é)

�ر��

  • ͬ��һ������,����˳��ͬ,�õ��Ķ�����Ҳ��ͬ

  • �������ֵ�Ѵ���ʱ,����ʧ��(������multi)

ɾ��

���Ȳ���Ԫ���Ƿ��ڶ����������У���������ڣ��򷵻�.

����,�������Ľṹ����,���Եõ�3�����

  1. Ҫɾ���Ľ���޺��ӽ��
  2. Ҫɾ���Ľ��ֻ�����ӻ��Һ���ʱ
  3. Ҫɾ���Ľ�������Һ��ӽ��

�������д�ɾ���ڵ���4�������ʵ�����:

  • Ҫɾ���Ľ���޺��ӽ��ʱ,ֱ��ɾ��

  • Ҫɾ���Ľ��ֻ�����ӻ��Һ���ʱ,�����ӻ��Һ��Ӹ�����

    1. Ҫɾ���Ľ������Ǹ��׵����ӻ������Һ���,��2*2�����(Ҫɾ���Ľ���Ǹ��׵����ӻ��Һ���)

    2. ���Һ��Ӷ��ǿ�ʱ,Ҳ�������,��˿��Ժϲ��޺��ӽ�����

    3. ��1��ǰ����,ǡ���Ǹ��ڵ�,Ҳ��һ�����(������һ��������������)

  • Ҫɾ���Ľ�������Һ���(����)ʱ,��Ҫ��һ����Ҫ����������ҲҪ��������С�Ľڵ�������.

    ���� �ݹ鶨�� ��֪,ֻ�����ӵ����ҽ����Һ��ӵ��������������,��ѡһ����

    ��ѡ��ʹ���Һ��ӵ�������ʱ,�������������(���Dz��Ǹ��޹�)

    1. Ҫɾ���Ľ�������������С���ǡ����Ҫɾ�������Һ���.

    2. Ҫɾ���Ľ�������������С���û���Һ���.

    3. Ҫɾ���Ľ�������������С������Һ���

      BST ¶þ²æËÑË÷Ê÷ BinarySearchTree C++ʵÏÖ(µÝ¹é/·ÇµÝ¹é)

      (��ͼ��������)

����ʵ��

BSTree.hpp

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;
};



test.cc

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();
}
С���Ƽ��Ķ�

�������������Ľ�Ϊ������Ϣ����������������ͬ���޹۵��֤ʵ��������

a 1.0
a 1.0
���ͣ���������������Ӫ״̬����ʽ��Ӫ�������ԣ����� ����

��Ϸ����

��Ϸ���

��Ϸ��Ƶ

��Ϸ����

��Ϸ�

��alittletotheleft������������һ��ܻ�ӭ����������������Ϸ����ҵ������Ƕ��ճ������еĸ���������

�����Ƶ����

����

ͬ������

����

ɨ��ά�����������ֻ��汾��

ɨ��ά����������΢�Ź��ںţ�

��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]

��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������