平衡二叉树实现

#include "avltree.h"

void avltree_t::rotate_left(avl_node_ptr_t &root_)
{
    avl_node_ptr_t right_child;
    right_child = root_->rchild;
    root_->rchild = right_child->lchild;
    right_child->lchild = root_;
    root_ = right_child;
}

void avltree_t::rotate_right(avl_node_ptr_t &root_)
{
    avl_node_ptr_t left_child;
    left_child = root_->lchild;
    root_->lchild = left_child->rchild;
    left_child->rchild = root_;
    root_ = left_child;
}

void avltree_t::balance_left(avl_node_ptr_t &root_)
{
    avl_node_ptr_t left_child, child;
    left_child = root_->lchild;
    switch (left_child->balance)
    {
    case LH:
        root_->balance = EH;
        left_child->balance = EH;
        rotate_right(root_);
        break;
    case RH:
        child = left_child->rchild;
        switch (child->balance)
        {
        case LH:
            root_->balance = RH;
            left_child->balance = EH;
            break;
        case EH:
            root_->balance = EH;
            left_child->balance = EH;
            break;
        case RH:
            root_->balance = EH;
            left_child->balance =LH;
            break;
        }
        child->balance = EH;
        rotate_left(root_->lchild);
        rotate_right(root_);
        break;
    }
}

void avltree_t::balance_right(avl_node_ptr_t &root_)
{
    avl_node_ptr_t right_child, child;
    right_child = root_->rchild;
    switch (right_child->balance)
    {
    case LH:
        child = right_child->lchild;
        switch (child->balance)
        {
        case LH:
            root_->balance = EH;
            right_child->balance = RH;
            break;
        case EH:
            root_->balance = EH;
            right_child->balance = EH;
            break;
        case RH:
            root_->balance = LH;
            right_child->balance = EH;
            break;
        }
        child->balance = EH;
        rotate_right(root_->rchild);
        rotate_left(root_);
        break;
    case RH:
        root_->balance = EH;
        right_child->balance = EH;
        rotate_left(root_);
        break;
    }
}
int avltree_t::insert_node(node_t node_)
{
   return insert_node(m_root, node_);
}

int avltree_t::insert_node(avl_node_ptr_t &root_, node_t node_)
{
    if (NULL == root_)
    {
        root_ = new avl_node_t(node_.key, node_.value);
        m_tall = true;
    }
    else
    {
        if (node_.key == root_->node.key)
        {
            m_tall = false;
            return 0;
        }
        else if(node_.key < root_->node.key)
        {
            if (0 == insert_node(root_->lchild, node_))
            {
                return 0;
            }
            if (true == m_tall)
            {
                switch (root_->balance)
                {
                case LH:
                    balance_left(root_);
                    m_tall = false;
                    break;
                case EH:
                    root_->balance = LH;
                    m_tall = true;
                    break;
                case RH:
                    root_->balance = EH;
                    m_tall = false;
                    break;
                }
            }
        }
        else
        {
            if (0 == insert_node(root_->rchild, node_))
            {
                m_tall = false;
                return 0;
            }
            if (true == m_tall)
            {
                switch (root_->balance)
                {
                case LH:
                    root_->balance = EH;
                    m_tall = false;
                    break;
                case EH:
                    root_->balance = RH;
                    m_tall = true;
                    break;
                case RH:
                    balance_right(root_);
                    m_tall = false;
                    break;
                }
            }
        }
    }
    return 1;
}

void avltree_t::visit_value_i(avl_node_ptr_t root_)
{
    if (NULL != root_)
    {
        cout << root_->node.key << ":" << root_->node.value << endl;
    }
}

void avltree_t::show_pre()
{
    show_pre_i(m_root);
}

void avltree_t::show_pre_i(avl_node_ptr_t root_)
{
    if (NULL != root_)
    {
        visit_value_i(root_);
        show_pre_i(root_->lchild);
        show_pre_i(root_->rchild);
    }
}

void avltree_t::show_mid()
{
    show_mid_i(m_root);
}

void avltree_t::show_mid_i(avl_node_ptr_t root_)
{
    if (NULL != root_)
    {
        show_mid_i(root_->lchild);
        visit_value_i(root_);
        show_mid_i(root_->rchild);
    }
}

int avltree_t::search_value(int key_)
{
    return search_value_i(m_root, key_);
}

int avltree_t::search_value_i(avl_node_ptr_t root_, int key_)
{
    if (NULL == root_)
    {
        return -1;
    }
    else
    {
        if (key_ < root_->node.key)
        {
            return search_value_i(root_->lchild, key_);
        }
        else if(key_ == root_->node.key)
        {
            return root_->node.value;
        }
        else
        {
            return search_value_i(root_->rchild, key_);
        }
    }
    return -1;
}

void avltree_t::delete_node(avl_node_ptr_t &root_)
{
    avl_node_ptr_t p;
    p = root_;
    int key, value;
    if (NULL == root_->lchild)
    {
        root_ = root_->rchild;
        delete p;
        p = NULL;
        m_short = true;
    }
    else if(NULL == root_->rchild)
    {
        root_ =root_->lchild;
        delete p;
        p = NULL;
        m_short = true;
    }
    else
    {
        p = root_->rchild;
        while (NULL != p->lchild)
        {
            p = p->rchild;
        }
        key = p->node.key;
        value = p->node.value;
        avldelete_node_i(root_, p->node.key);
        root_->node.key = key;
        root_->node.value = value;
    }
}

int avltree_t::avldelete_node(int key_)
{
    return avldelete_node_i(m_root, key_);
}

int avltree_t::avldelete_node_i(avl_node_ptr_t &root_, int key_)
{
    if (NULL == root_)
    {
        return 0;
    }

    if (key_ == root_->node.key)
    {
        delete_node(root_);
    }

    else if (key_ < root_->node.key)
    {
        if (0 == avldelete_node_i(root_->lchild, key_))
        {
            return 0;
        }
        if (true == m_short)
        {
            switch (root_->balance)
            {
            case LH:
                root_->balance = EH;
                m_short = true;
                break;
            case EH:
                root_->balance = RH;
                m_short = false;
                break;
            case RH:
                delete_balance_right(root_);
                //m_short = true;
                break;
            }
        }
    }

    else
    {
        if (0 == avldelete_node_i(root_->rchild, key_))
        {
            return 0;
        }
        if (true == m_short)
        {
            switch (root_->balance)
            {
            case LH:
                delete_balance_left(root_);
                //m_short = true;
                break;
            case EH:
                root_->balance = LH;
                m_short = false;
                break;
            case RH:
                root_->balance = EH;
                m_short = true;
                break;
            }
        }
    }
    return 1;
}

void avltree_t::delete_balance_left(avl_node_ptr_t &root_)
{
    avl_node_ptr_t left_child, child;
    left_child = root_->lchild;
    switch (left_child->balance)
    {
    case LH:
        root_->lchild = left_child->rchild;
        left_child->rchild = root_;
        root_->balance = EH;
        left_child->balance = EH;
        root_ = left_child;
        m_short = true;
        break;
    case EH:
        root_->lchild = left_child->rchild;
        left_child->rchild = root_;
        root_->balance = LH;
        left_child->balance = RH;
        root_ = left_child;
        m_short = false;
        break;
    case RH:
        child = left_child->rchild;
        left_child->rchild = child->lchild;
        child->lchild = left_child;
        root_->lchild = child->rchild;
        child->rchild = root_;
        switch (child->balance)
        {
        case LH:
            root_->balance = RH;
            left_child->balance = EH;
            break;
        case EH:
            root_->balance = EH;
            left_child->balance = EH;
            break;
        case RH:
            root_->balance = EH;
            left_child->balance = LH;
            break;
        }
        child->balance = EH;
        root_ = child;
        m_short = true;
        break;
    }
}
void avltree_t::delete_balance_right(avl_node_ptr_t &root_)
{
    avl_node_ptr_t right_child, child;
    right_child = root_->rchild;
    switch(right_child->balance)
    {
    case LH:
        child = right_child->lchild;
        right_child->lchild = child->rchild;
        child->rchild = right_child;
        root_->rchild = child->lchild;
        child->lchild = root_;
        switch(child->balance)
        {
        case LH:
            root_->balance = EH;
            right_child->balance = RH;
            break;
        case EH:
            root_->balance = EH;
            right_child->balance = EH;
            break;
        case RH:
            root_->balance = LH;
            right_child->balance = EH;
            break;
        }
        child->balance = EH;
        root_ = child;
        m_short = true;
        break;
    case EH:
        root_->rchild = right_child->lchild;
        right_child->lchild = root_;
        right_child->balance = LH;
        root_->balance = RH;
        root_ = right_child;
        m_short = false;
        break;
    case RH:
        root_->rchild = right_child->lchild;
        right_child->lchild = root_;
        right_child->balance = EH;
        root_->balance = EH;
        root_ = right_child;
        m_short = true;
        break;
    }
}

void avltree_t::destroy_tree()
{
    destroy_tree_i(m_root);
}

void avltree_t::destroy_tree_i(avl_node_ptr_t &root_)
{
    if (NULL != root_)
    {
        destroy_tree_i(root_->lchild);
        root_->lchild = NULL;
        destroy_tree_i(root_->rchild);
        root_->rchild = NULL;
        delete root_;
        root_ = NULL;
    }
}
//自己之前写的代码,注释改天补上。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值