//avl.h
#ifndef __AVL_H__
#define __AVL_H__


typedef int KEY_TYPE;


/* struct */
typedef struct AVL{
 KEY_TYPE key;
 int height;
 struct AVL* lchild;
 struct AVL* rchild;
}AVL;


AVL* New_Node(KEY_TYPE key, AVL* lchild, AVL* rchild, int height = 0);
inline int GetHeight(AVL* node);


AVL* RR_Rotate(AVL* k2);
AVL* LL_Rotate(AVL* k2);
AVL* LR_Rotate(AVL* k3);
AVL* RL_Rotate(AVL* k3);


AVL* Insert(AVL* root, KEY_TYPE key);
AVL* Delete(AVL* root, KEY_TYPE key);
void InOrder(AVL* root);


#endif


//avl.cpp
#include "AVL.h"
#include<iostream> AVL* New_Node(KEY_TYPE key, AVL* lchild, AVL* rchild, int height)
{
AVL* p_avl = new AVL;
p_avl->key = key;
p_avl->lchild = lchild;
p_avl->rchild = rchild;
p_avl->height = height;
return p_avl;
} inline int GetHeight(AVL* node)
{
return (node==NULL)? -:node->height;
} inline int max(int a, int b)
{
return a>b?a:b;
} /* RR(Y rotates to the right): k2 k1
/ \ / \
k1 Z ==> X k2
/ \ / \
X Y Y Z
*/
AVL* RR_Rotate(AVL* k2)
{
AVL* k1 = k2->lchild;
k2->lchild = k1->rchild;
k1->rchild = k2;
k2->height = max(GetHeight(k2->lchild), GetHeight(k2->rchild)) + ;
k1->height = max(GetHeight(k1->lchild), k2->height) + ;
return k1;
} /* LL(Y rotates to the left): k2 k1
/ \ / \
X k1 ==> k2 Z
/ \ / \
Y Z X Y
*/
AVL* LL_Rotate(AVL* k2)
{
AVL* k1 = k2->rchild;
k2->rchild = k1->lchild;
k1->lchild = k2;
k2->height = max(GetHeight(k2->lchild), GetHeight(k2->rchild)) + ;
k1->height = max(GetHeight(k1->rchild), k2->height) + ;
return k1;
} /* LR(B rotates to the left, then C rotates to the right): k3 k3 k2
/ \ / \ / \
k1 D k2 D k1 k3
/ \ ==> / \ ==> / \ / \
A k2 k1 C A B C D
/ \ / \
B C A B */
AVL* LR_Rotate(AVL* k3)
{
k3->lchild = LL_Rotate(k3->lchild);
return RR_Rotate(k3);
} /* k3 k3 k2
/ \ / \ / \
A k1 A k2 k3 k1
/ \ ==> / \ ==> / \ / \
k2 B C k1 A C D B
/ \ / \
C D D B */
AVL* RL_Rotate(AVL* k3)
{
k3->rchild = RR_Rotate(k3->rchild);
return LL_Rotate(k3);
} AVL* Insert(AVL* root, KEY_TYPE key)
{
if(root == NULL)
return (root = New_Node(key, NULL, NULL));
else if(key < root->key)
root->lchild = Insert(root->lchild, key);
else
root->rchild = Insert(root->rchild, key); root->height = max(GetHeight(root->lchild), GetHeight(root->rchild)) + ;
if(GetHeight(root->lchild) - GetHeight(root->rchild) == )
{
if(key < root->lchild->key)
root = RR_Rotate(root);
else
root = LR_Rotate(root);
}
else if(GetHeight(root->rchild) - GetHeight(root->lchild) == )
{
if(key < root->rchild->key)
root = RL_Rotate(root);
else
root = LL_Rotate(root);
}
return root;
} AVL* Delete(AVL* root, KEY_TYPE key)
{
if(!root)
return NULL;
if(key == root->key)
{
if(root->rchild == NULL)
{
AVL* temp = root;
root = root->lchild;
delete(temp);
return root;
}
else
{
AVL* temp = root->rchild;
while(temp->lchild)
temp = temp->lchild; root->key = temp->key; root->rchild = Delete(root->rchild, temp->key);
}
}
else if(key < root->key)
root->lchild = Delete(root->lchild, key);
else
root->rchild = Delete(root->rchild, key); root->height = max(GetHeight(root->lchild), GetHeight(root->rchild)) + ;
if(GetHeight(root->rchild) - GetHeight(root->lchild) == )
{
if(GetHeight(root->rchild->rchild) >= GetHeight(root->rchild->lchild))
root = LL_Rotate(root);
else
root = RL_Rotate(root);
}
else if(GetHeight(root->lchild) - GetHeight(root->rchild) == )
{
if(GetHeight(root->lchild->lchild) >= GetHeight(root->lchild->rchild))
root = RR_Rotate(root);
else
root = LR_Rotate(root);
}
return root;
} void InOrder(AVL* root)
{
if(root)
{
InOrder(root->lchild);
printf("key: %d height: %d ", root->key, root->height);
if(root->lchild)
printf("left child: %d ", root->lchild->key);
if(root->rchild)
printf("right child: %d ", root->rchild->key);
printf("\n");
InOrder(root->rchild);
}
}
// main.cpp
#include<iostream>
#include "AVL.h" int main(int argc, char* argv[])
{
AVL* root = NULL;
int vector[] = {,,,,,,,,}; const int length = sizeof(vector)/sizeof(int);
for(int i = ; i< length;i++)
root = Insert(root, vector[i]); printf("\nInOrder: \n");
InOrder(root); int input;
printf("\nplease input the value you want to delete: ");
scanf("%d",&input); while()
{
root = Delete(root, input);
printf("\nAfter delete %u:\n",input);
InOrder(root);
printf("\nplease input another value you want to delete: ");
scanf("%u",&input);
}
printf("\n");
return ;
}

AVL-tree的更多相关文章

  1. 04-树5 Root of AVL Tree

    平衡二叉树 LL RR LR RL 注意画图理解法 An AVL tree is a self-balancing binary search tree. In an AVL tree, the he ...

  2. 1066. Root of AVL Tree (25)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  3. 1066. Root of AVL Tree

    An AVL tree is a self-balancing binary search tree.  In an AVL tree, the heights of the two child su ...

  4. 树的平衡 AVL Tree

    本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...

  5. AVL Tree Insertion

    Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...

  6. 1123. Is It a Complete AVL Tree (30)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  7. A1123. Is It a Complete AVL Tree

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  8. A1066. Root of AVL Tree

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  9. PAT A1123 Is It a Complete AVL Tree (30 分)——AVL平衡二叉树,完全二叉树

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  10. 04-树5 Root of AVL Tree + AVL树操作集

    平衡二叉树-课程视频 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the tw ...

随机推荐

  1. php电话号码正则表达式常用例子

    电话号码正则表达式(支持手机号码,3-4位区号,7-8位直播号码,1-4位分机号) 02   03 ((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{ ...

  2. arm_linux_dt

    http://www.deyisupport.com/question_answer/dsp_arm/c6000_multicore/f/53/t/44862.aspx

  3. html 报表导出excel防止数字变科学计数

    在html 标签加:  <html xmlns:x="urn:schemas-microsoft-com:office:excel">    在要导出的tr加:  &l ...

  4. 通过RGB灯输出七色

    本文由博主原创,如有不对之处请指明,转载请说明出处. /********************************* 代码功能:输出模拟信号,控制RGB灯的颜色 使用函数: pinMode(引脚 ...

  5. 使用Maven自动部署Java Web应用到Tomcat服务器

    学习如何使用Maven,我推荐一本工具书,<maven the definitive guide>.在这本工具书手中,详细介绍了maven的使用思想,并且提供了从基本到复杂的具体项目应用. ...

  6. java_method_下拉框成json

    List<String[]> proList=service.getUserList(); int nTotal=0; String proJson="["; proJ ...

  7. linux操作系统-memcache安装

    安装配置 1. 安装libevent # tar zxf libevent-1.4.6-stable.tar.gz # cd libevent-1.4.6-stable # ./configure # ...

  8. apache结合svn创建svn资源库

    1.在登录过程中可以查看error日志,如果发生以下提示: (13)Permission denied: Could not open password file 2.运行:chcon -R -h - ...

  9. JavaScript Window 对象

    < JavaScript Window Object > && < IE check > JavaScript Window Object Window.loa ...

  10. flask-admin章节二:wtforms的使用以及在数据库场景中使用QuerySelectField代替SelectField

    概述 flask admin可以支持自定义视图,对于涉及到比较复杂的视图可以选择继承flask_admin.BaseView来定义自己期待的结构. 自定义的视图的每个函数可以使用flask_admin ...