//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. Word2013创建目录

    1.写好文档内容后,将光标移到标题行,点击“开始”里的“样式”->“创建样式”,为该标题创建一个新的样式,同时点击“修改”,在打开的窗口中选择左下方的“格式”,进行标题格式的调整.依次可设定子标 ...

  2. Android开发--仿微信语音对讲录音

    原文地址:http://www.2cto.com/kf/201502/378704.html 自微信出现以来取得了很好的成绩,语音对讲的实现更加方便了人与人之间的交流.今天来实践一下微信的语音对讲的录 ...

  3. 更新lispbox中的ccl和slime版本

    首先C-x C-f然后输入~,找到.emacs文件,根据slime官方文档说明的添加如下代码到文件末尾,重启一下emacs,slime就编译好了,然后这段代码就可以删除.否则每次启动emacs就算不用 ...

  4. loadrunner ---模拟多IP登录

    1.打开HP LoadRunner ->Tools ->IP Wizard

  5. error-2016-4-20

    问题: Compilation ErrorDescription: An error occurred during the compilation of a resource required to ...

  6. MyBatis复习【简单配置CRUD】

    这里的案例集成了log4j的日志框架,项目架构: 用到的jar文件 添加配置文件:mybatis-config.xml  和dao层配置文件StudentDao.xml 这里书写了个简单的案例仅为了说 ...

  7. CMFCPropertyGridProperty SetValue 出错处理

    对CMFCPropertyGridProperty SetValue时容易报错,这种情况一般是Property和value的类型不匹配造成的. 在创建property的时候,指定了数据类型,如果set ...

  8. oracle 表被锁定 杀死进程

    /*查出被锁biao*/ select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_obje ...

  9. Appium移动自动化测试之Eclipse

    下载eclipse,这个下载方式比较多,eclipse官网,CSDN都有的下,版本根据自己操作系统选择,切记eclipse版本一定要与JDK版本一至,不然eclipse无法启动.现在我们来搭建Andr ...

  10. js 创建 JSON对象

    //定义变量 var Type = [{}]; Type.push({ label: "labelname", value: "value" });