AVL-tree
//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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 树的平衡 AVL Tree
本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...
- AVL Tree Insertion
Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- bat调用bat的一个巨坑
[一个巨坑] a.bat的内容:echo 1b.batecho 2执行结果:运行a.bat时,输出1,然后调用b.bat, 但是 echo 2 显示不出来. bat怎么调用bat文件并返回? 例如主文 ...
- js中的原形链问题
---恢复内容开始--- 一.在js中大家讨论的原形链都是围绕在prototype和__proto__. 1.__proto__是内部原型 2.prototype是构造器原型(构造器就是构造函数) 3 ...
- nginx配置文件httpd.conf详解
PS:Nginx使用有两三年了,现在经常碰到有新用户问一些很基本的问题,我也没时间一一回答,今天下午花了点时间,结合自己的使用经验,把Nginx的主要配置参数说明分享一下,也参考了一些网络的内容,这 ...
- HDU 1285 确定比赛名次 拓扑排序模板题
http://acm.hdu.edu.cn/showproblem.php?pid=1285 #include <cstdio> #include <cstdlib> #inc ...
- 在项目中添加ReactiveCocoa #安装与配置
这是对官方教程的补充 To add RAC to your application: Add the ReactiveCocoa repository as a submodule of your a ...
- 01 Apache Solr:提升检索体验 为什么是Solr
背景: 最近开发一个大型的仓储管理平台项目,项目的前身是无数个版本的历史悠久的基于CS模式的Windows桌面程序.然后对于每一个客户,我们可能需要为之定制比较个性化的特殊功能.于是,有一个 ...
- 关于monkeyrunner的一些初步理解性的题目
1.Monkeyrunner中包含几个基本类?分别大概的作用是什么? Monkeyrunner中基本包含了MonkeyRunner,MonkeyDevice,MonkeyImage MonkeyRun ...
- XUtils
//HttpUtils实例化对象 HttpUtils http = new HttpUtils(); /* *发送请求send(HttpMethod ...
- Cookie的Secure属性
基于安全的考虑,需要给cookie加上Secure和HttpOnly属性,HttpOnly比较好理解,设置HttpOnly=true的cookie不能被js获取到,无法用document.cookie ...
- Android SDK更新以及ADT更新出现问题的解决办法
http://jingyan.baidu.com/article/148a192196209d4d70c3b168.html