自己动手实现数据结构——AVL树(C++实现)

这类教程有很多了,就用C++简单实现下以供记录和参考,以后再有补充版本。

实现了查找和插入、删除操作有些复杂,感觉个人实现的效率不是很高,以后再补充,先把做过的东西记录下来。

Avl.h

#ifndef __AVL_H
#define __AVL_H

#include<stddef.h>
#include<vector>

template< class T>
struct AvlNode{
    T data;
    int height;
    AvlNode* left;
    AvlNode* right;
    AvlNode(T value):data(value),left(NULL),right(NULL),height(1){};
};


template< class T >
class AvlTree{
    public:
        AvlTree():pTree(NULL){};
        ~AvlTree();
        int height(AvlNode<T>* node)const;
        int max_height(int a, int b);

        //order
        void PreOrderTree(AvlNode<T>* tree)const;
        void InOrderTree(AvlNode<T>* tree)const;
        void PostOrderTree(AvlNode<T>* tree)const;

        // rotate
        void SingleRotateL(AvlNode<T>* &parent);
        void SingleRotateR(AvlNode<T>* &parent);
        void DoubleRotateLR(AvlNode<T>* &parent);
        void DoubleRotateRL(AvlNode<T>* &parent);

        // find
        void find_min(AvlNode<T>* node);
        void find_max(AvlNode<T>* node);

        // insert
        void insert(AvlNode<T>* &root, T value);
        AvlNode<T>* pTree;
};

#endif

Avl.cc

#include "Avl.h"
#include <iostream>
#include <cmath>
using namespace std;


template< class T>
void AvlTree<T>::insert(AvlNode<T>* &root, T value){
    if ( NULL == root)
    {
        root = new AvlNode<T>( value );
        if ( NULL == root )
        {
            cout << "init failed"<<endl;
        }
    }
    else if ( value < root-> data ){
        insert( root-> left, value );
        if ( 2 == height(root->left) - height(root->right))
        {
            if ( value < root->left->data )
                SingleRotateR( root);
            else
                DoubleRotateLR(root);
        }
    }
    else{
        insert(root->right, value );
        if( 2 == height(root->right) - height(root->left) ){
            if ( value < root->right->data )
                DoubleRotateRL(root);
            else
                SingleRotateL(root);
        }
    }
    root->height = max_height(height(root->left), height(root->right)) + 1;
}

template< class T>
void AvlTree<T>::SingleRotateR( AvlNode<T>* &parent){
    AvlNode<T>* child = parent->left;
    parent->left = child->right;
    child->right = parent;
    parent->height = max_height(height(parent->left), height(parent->right))+1;
    child->height = max_height(height(child->left), height(child->right))+1;
    parent = child;
}


template< class T>
void AvlTree<T>::SingleRotateL( AvlNode<T>* &parent){
    AvlNode<T>* child = parent->right;
    parent->right = child->left;
    child->left = parent;
    parent->height = max_height(height(parent->left), height(parent->right))+1;
    child->height = max_height(height(child->left), height(child->right))+1;
    parent = child;
}

template< class T >
void AvlTree<T>::DoubleRotateLR( AvlNode<T>* &parent ){
    SingleRotateL( parent -> left );
    SingleRotateR( parent );
}

template< class T >
void AvlTree<T>::DoubleRotateRL( AvlNode<T>* &parent ){
    SingleRotateR( parent -> right);
    SingleRotateL( parent );
}

template<class T>
int AvlTree<T>::height(AvlNode<T>* node) const{
    if ( NULL == node )
        return 0;
    else
        return node->height;
}

template<class T>
int AvlTree<T>::max_height(int a, int b){
    return a > b ? a:b;
}

template<class T>
void AvlTree<T>::PreOrderTree(AvlNode<T>* tree)const{
    if ( NULL != tree){
        cout << tree->data <<" ";
        PreOrderTree(tree->left);
        PreOrderTree(tree->right);
    }
}

template<class T>
void AvlTree<T>::InOrderTree(AvlNode<T>* tree)const{
    if ( NULL != tree ){
        InOrderTree(tree->left);
        cout << tree->data <<" ";
        InOrderTree(tree->right);
    }
}


template<class T>
void AvlTree<T>::PostOrderTree(AvlNode<T>* tree)const{
    if ( NULL != tree ){
        PostOrderTree(tree->left);
        PostOrderTree(tree->right);
        cout << tree->data <<" ";
    }
}

int main(void){
    int input;
    int size;
    int i = 0;
    AvlTree<int>* tree = new AvlTree<int>();
    cout << " input the size" <<endl;
    cin >> size;
    cout << " input the value" <<endl;
    while ( i < size )
    {
        cin >> input;
        tree->insert( tree->pTree, input);
        ++i;
    }

    tree->PreOrderTree(tree->pTree);
    cout<<endl;
    tree->InOrderTree(tree->pTree);
    cout<<endl;
    tree->PostOrderTree(tree->pTree);
        
    return 0;
}

测试:

《自己动手实现数据结构——AVL树(C++实现)》

    原文作者:AVL树
    原文地址: https://blog.csdn.net/sshhiixx/article/details/47397157
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞