#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;
}
}
//自己之前写的代码,注释改天补上。
平衡二叉树实现
最新推荐文章于 2021-08-19 00:10:06 发布