亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在C++中實現AVL樹

發布時間:2021-06-02 16:17:43 來源:億速云 閱讀:133 作者:Leah 欄目:開發技術

怎么在C++中實現AVL樹?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

AVL樹的介紹

AVL樹是一種自平衡的二叉搜索樹,它通過單旋轉(single rotate)和雙旋轉(double rotate)的方式實現了根節點的左子樹與右子樹的高度差不超過1,。這有效的降低了二叉搜索樹的時間復雜度,為O(log n)。那么,下面小編將詳細介紹C++實現AVL樹的代碼。最后一步提供可靠的代碼實現

怎么在C++中實現AVL樹

這里先粘貼代碼
給大家的忠告,一定要及時去實現,不然之后再實現要花更多的時間

 

#ifndef _AVLTREE_
#define _AVLTREE_
#include<iostream>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define MAXFACTOR = 2;
template<class Key , class E>
class AVLNode
{
    private:
        Key key;
        E value;
        AVLNode<Key,E>* left;
        AVLNode<Key,E>* right;
        AVLNode<Key,E>* parent;
    public:
        AVLNode():left(nullptr),right(nullptr),parent(nullptr){}
        AVLNode(Key _key,E _value , AVLNode<Key,E>* _parent = nullptr,AVLNode<Key,E>*_left = nullptr, AVLNode<Key,E>*_right = nullptr):
                key(_key),value(_value),left(_left),right(_right),parent(_parent){}
        
        bool isLeaf(){return left==nullptr && right == nullptr ;}

        //元素設置
        Key getKey() const { return key;}
        void setKey(Key set) {key = set;}
        
        E getValue() const { return value;}
        void setValue(E set) {value = set;}

        AVLNode<Key,E>*  getLeft() { return left; }
        void setLeft (AVLNode< Key,E >* set){ left = set;}

        AVLNode<Key,E>*  getRight()  { return right;}
        void setRight (AVLNode<Key,E>* set) {right = set ;}

        AVLNode<Key,E>* getParent()  {return parent;}
        void setParent(AVLNode<Key,E>* set) { parent = set;}

};
template<class Key , class E>
class AVLTree
{
    private:
        AVLNode<Key,E>* root;
        void clear(AVLNode<Key,E>* &r)
        {
            if(r==nullptr)return;

            if(r->getLeft())clear(r->getLeft());
            if(r->getRight())clear(r->getRight);

            delete r; 
        }

        void Init()
        {
            root = new AVLNode<Key,E>();
            root=nullptr;
        }
        void preOrder(AVLNode<Key,E>* r,void(*visit) (AVLNode<Key,E> * node))
        {
            if(r==nullptr)return;
            (*visit) (r);
            preOrder(r->getLeft() , visit);
            preOrder(r->getRight(), visit);
        }

        void inOrder(AVLNode<Key,E>* r , void(*visit)(AVLNode<Key,E>* node) )
        {
            if(r==nullptr)return;
            inOrder(r->getLeft() , visit);
            (*visit)(r);
            inOrder(r->getRight(),visit);
        }

        void postOrder(AVLNode<Key,E>*r , void (*visit) (AVLNode<Key,E>* node))
        {
            if(r==nullptr)return;
            postOrder(r->getLeft(),visit);
            postOrder(r->getRight(),visit);
            (*visit)(r);
        }

        void levelOrder(AVLNode<Key,E>*r , void (*visit) (AVLNode<Key,E>* node))
        {
            queue< AVLNode<Key,E>* > q;
            if(r==nullptr)return;
            q.push(r);
            while( ! q.empty() )
            {
                AVLNode<Key,E>* tmp = q.front();
                q.pop();
                (*visit)(tmp);
                if(tmp->getLeft() ) q.push(tmp->getLeft() );
                if(tmp->getRight()) q.push(tmp->getRight());
                
            }
        }

        AVLNode<Key,E>* find(AVLNode<Key,E>* r,Key k)
        {
            if(r==nullptr)return nullptr;
            if(k == r->getKey() ) return r;
            else if( k < r->getKey())
            {
                find(r->getLeft(),k);
            }
            else {
                find(r->getRight(),k);
            }
        }
        //Find the smallest element in the avl tree
        AVLNode<Key,E>* getMin(AVLNode<Key,E>* r)
        {
            if(r->getLeft() == nullptr) return r;
            else{
                getMin(r->getLeft());
            }
        }
        //Remove the smallest element 
        AVLNode<Key,E>* deleteMin(AVLNode<Key,E>* rt)
        {
            if(rt->getLeft() == nullptr) return rt->getRight();
            else{
                rt->setLeft(deleteMin(rt->getLeft()));
                return rt;
            }
        }

        AVLNode<Key,E>* leftRotate(AVLNode<Key,E>* node)
        {
            if( node == nullptr) return nullptr;
            AVLNode<Key,E>* newHead = node->getRight();
            node->setRight( newHead -> getLeft() );
            newHead -> setLeft( node );
            return newHead; 
        }
        AVLNode<Key,E>* rightRotate(AVLNode<Key,E>* node)
        {
            if(node == nullptr)return nullptr;
            AVLNode<Key,E>* newHead = node->getLeft();
            node->setLeft( newHead->getRight() );
            newHead ->setRight(node);
            return newHead;
        }

        int getHeight(AVLNode<Key,E>*node)
        {
            if(node == nullptr)return 0;
            if(node->isLeaf())return 1;
            else return ( getHeight( node->getLeft() ) > getHeight( node->getRight() ) ?
                        getHeight( node->getLeft() ) : getHeight( node->getRight() ) ) + 1;
        }

        int getBalanceFactor(AVLNode<Key,E>* node)
        {
            return getHeight(node->getLeft()) - getHeight(node->getRight() );
        }
        AVLNode<Key,E>* balance(AVLNode<Key,E>* node)
        {
            if(!node) return nullptr;
            else if ( getBalanceFactor( node ) == 2)
            {
                if(getBalanceFactor( node ->getLeft() ) == 1)
                {
                    node = rightRotate(node);
                }
                else
                {
                    node->setLeft(leftRotate( node->getLeft() ) );
                    node = rightRotate(node);
                }
            }
            else if(getBalanceFactor( node ) == -2)
            {
                if(getBalanceFactor( node->getRight()) == -1)
                {
                    node = leftRotate(node);
                }
                else
                {
                    node->setRight( rightRotate( node->getRight() ) );
                    node = leftRotate(node);
                }
            }
            return node;
        }

        AVLNode<Key,E>* insert( AVLNode<Key,E>* root ,const pair<Key,E>& it)
        {
            if(root == nullptr)
            {
                return new AVLNode<Key,E>(it.first , it.second,NULL,NULL,NULL);
            }
            else if (it.first < root->getKey() )
            {
                
                root ->setLeft( insert(root->getLeft() , it) ) ;
            }
            else{
                root ->setRight( insert(root->getRight() , it) );
                
            }
            root = balance(root);
            return root;
        }

        AVLNode<Key,E>* remove(AVLNode<Key,E>*  node , const Key k)
        {
            if(node == nullptr) return nullptr;
            if(node->getKey() > k)
            {
                node->setLeft( remove(node->getLeft() , k) );
                node = balance(node);
            }
            else if(node->getKey() < k)
            {
                node->setRight( remove(node->getRight(), k) );
                node = balance(node);
            }
            else if(node->getKey() == k)
            {
                if(! node->isLeaf() )
                {
                    AVLNode<Key,E>* tmp = getMin(node->getRight() );
                    node->setKey( tmp->getKey() );
                    node->setValue( tmp->getValue() );
                    node->setRight( deleteMin(node->getRight() ) );
                    delete tmp;
                }
                else {
                    AVLNode<Key,E>* tmp = node;
                    node = (node->getLeft() != nullptr) ? node->getLeft() : node->getRight() ;
                    delete tmp;
                }
            }
            return node;
        }
   
    public:
        ~AVLTree(){clear(root);}
        AVLTree(){/*Init();*/ root = nullptr; }
    //四種遍歷方式
        void preOrder( void (*visit)(AVLNode<Key,E>* r))
        {
            preOrder(root,visit);
        }
        void inOrder(void (*visit)(AVLNode<Key,E>* r))
        {
            inOrder(root,visit);
        }
        void postOrder(void (*visit)(AVLNode<Key,E>* r))
        {
            postOrder(root,visit);
        }
        void levelOrder( void(*visit)(AVLNode<Key,E>*r) )
        {
            levelOrder(root,visit);
        }
         //插入
        void insert(const pair<Key,E> &it)
        {
            root = insert(root,it);
        }

        //刪除
       void remove(const Key& k)
        {
            remove(root,k);
        }
        bool find(const Key&k)
        {
            return find(root,k);   
        }   



            
};
#endif
//AVLtest.cpp
#include"NewAvl.h"
#include<iostream>
using namespace std;
template<typename Key,typename E>
void traverse(AVLNode<Key,E>* root)
{
    cout<<root->getKey()<<" "<<root->getValue()<<" ";
    cout<<endl;
}
int main()
{
    AVLTree<int,int>* tree = new AVLTree<int ,int>;
    for(int i = 0 ; i < 5 ; i ++)
    {
        tree->insert(make_pair(i,i));
    }
    tree->remove(1);
    cout<<"PreOrder: "<<endl;
    tree->preOrder(traverse);
    cout<<endl;
    cout<<"LevelOrder: "<<endl;
    tree->levelOrder(traverse);
    cout<<endl;
    cout<<"InOrder: "<<endl;
    tree->inOrder(traverse);
    cout<<endl;
    cout<<"PostOrder: "<<endl;
    tree->postOrder(traverse);
    cout<<endl;
    cout<<tree->find(2)<<endl;
    tree->insert(make_pair(9,9));
    tree->levelOrder(traverse);

}

運行結果

怎么在C++中實現AVL樹

看完上述內容,你們掌握怎么在C++中實現AVL樹的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

广安市| 格尔木市| 九龙城区| 拜城县| 新乡县| 田阳县| 黄平县| 浏阳市| 沁阳市| 印江| 福贡县| 西安市| 炎陵县| 新巴尔虎右旗| 庆阳市| 全南县| 新竹县| 洮南市| 抚宁县| 长丰县| 巫山县| 清涧县| 新河县| 霍林郭勒市| 巴东县| 增城市| 益阳市| 浏阳市| 凤山县| 夏邑县| 沈丘县| 胶南市| 漳浦县| 嘉善县| 铁岭市| 北辰区| 米林县| 江北区| 五常市| 水城县| 江都市|