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 ...
随机推荐
- nagios二次开发(五)---nagios和nagiosql的关系
根据对nagios和nagiosql的了解,笔者简要的将二者的关系粗略的梳理了一下,具体情况如下图所示: 从上面的关系图中可以看出,nagios与nagiosql共享了主机.主机组.服务.服务组等.c ...
- 笔记26-徐 SQLSERVER内存分配和常见内存问题
1 --64位SQLSERVER 应用在IA64操作系统 7TB 2TB ...
- 循序渐进Python3(十一) --2-- web之javascript
JavaScrip JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之后 ...
- oracle 队列
Oracle 高级队列(AQ) 适用对象:初步了解oracle高级队列人群 注意事项: 序号 注意事项 1 JMS监听部分可参考官方文档: http://docs.oracle.com/cd/e128 ...
- 尝试一下代码高亮。。成功的话明天写一篇blog
using System; using System.Collections; using UnityEngine; public class Time : MonoBehaviour { // Us ...
- PostrgreSQL 表名大小些问题(public."tablename")
问题: 今天做表查询的时候,发现用以前的代码查询出现问题,提示说表名不存在. 现象: 通过PostrgreSQL客户端查询,发现出问题的表的查询语句如下: SELECT * FROM public.& ...
- iOS网络请求之multipart/form-data提交数据
multipart/form-data表单数据 在http网络请求中,post没有请求长度的限制,因为post把数据放在了body中,而不是像Get一样放在了浏览器的地址栏中(可以这么理解), 所以相 ...
- C#中常用的几种读取XML文件的方法
1.C#中常用的几种读取XML文件的方法:http://blog.csdn.net/tiemufeng1122/article/details/6723764/
- mysql定时器Events
MySQL定时器Events 一.背景 我们MySQL的表A的数据量已经达到1.6亿,由于一些历史原因,需要把表A的数据转移到一个新表B,但是因为这是线上产品,所以宕机时间需要尽量的短,在不影响数据持 ...
- Objective-C 变量和基本的数据类型
转自:http://blog.csdn.net/mengxiangyue/article/details/11369679 OC中有如下基本数据类型: int:声明整型变量 double:声明双精度变 ...