平衡二叉树AVL操作模板
发布时间:2022-04-23 15:31:41
/**
* 目的:实现AVL
* 利用数组对左右儿子简化代码,但是对脑力难度反而增大不少,只适合acm模板
* 其实avl在acm中基本不用,基本被treap取代
* avl一般只要求理解思路,不要求写出代码,因为真心很烦
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <time.h>
#include <queue>
using namespace std;
int COUNT; //统计树中不重复节点的个数
int HEIGHT; //统计数的高度
//数据节点
class DNode
{
public:
int data;
DNode():data(0){};
DNode(int d):data(d){}
bool operator = (const DNode &d){
return data = d.data;
}
bool operator == (const DNode &d){
return data == d.data;
}
bool operator > (const DNode &d){
return data > d.data;
}
bool operator < (const DNode &d){
return data < d.data;
}
void show(){
cout << endl;
cout << "***************" << endl;
cout << "data: " << data << endl;
}
};
//treap的节点
template<class T>
class AVLNode{
private:
int hgt; //节点的高度
public:
T data;
int count;
AVLNode<T> *son[2]; //0是左儿子,1是右儿子
AVLNode<T>(T data):data(data), count(1){
son[0] = son[1] = NULL;
hgt = 1;
}
int max(int a, int b){return a > b ? a : b;}
void show(){
data.show();
cout << "c hgt: " << this->height() << endl;
cout << "l hgt: " << this->son[0]->height() << endl;
cout << "r hgt: " << this->son[1]->height() << endl;
cout << "count: " << count << endl;
cout << endl;
}
int height(){
if(NULL == this)
return 0;
return _getHeight(this);
}
int _getHeight(AVLNode<T> * cur){
if(NULL == cur)
return 0;
return 1 + max(_getHeight(cur->son[0]), _getHeight(cur->son[1]));
}
void setHeight(){
hgt = _getHeight(this);
}
};
//AVL Tree
//这里的T是节点中的数据类型
template<class T>
class AVLTree{
private:
AVLNode<T> * root; //根节点
int hgt; //树的高度
int size; //树中不重复节点数量
void _insert(AVLNode<T> *& cur, T data);
void _mid_travel(AVLNode<T> *cur);
//层次遍历
void _cengci_travel(AVLNode<T> *cur);
//单旋转(左左,右右), 左旋不是想左旋转的意思, 而是由于左子树的左儿子的高度太大
//与treap的旋转命名相反
void _singleRoate(AVLNode<T> *& cur, int dir);
//双旋转(两次单旋转)
void _doubleRoate(AVLNode<T> *& cur, int dir);
int max(int a, int b){
return a > b ? a : b;
}
public:
AVLTree():root(NULL), hgt(0){}
void insert(const T & data);
void mid_travel();
int height(){
return root->height();
}
//层次遍历
void cengci_travel(){
_cengci_travel(root);
};
};
/************* 私有方法开始**********************/
template<class T>
void AVLTree<T>::_insert(AVLNode<T> *& cur, T data){
if(NULL == cur){
cur = new AVLNode<T>(data);
}else if(data == cur->data){
cur->count++;
}else{
int dir = (data > cur->data);
_insert(cur->son[dir], data);
if(2 <= cur->son[dir]->height() - cur->son[!dir]->height()){
bool lr = (data > cur->data);
lr ? _singleRoate(cur, dir) : _doubleRoate(cur, dir);
}
}
cur->setHeight();
//cout << "set height: " << endl;
//cur->show();
}
template<class T>
void AVLTree<T>::_mid_travel(AVLNode<T> * cur){
if(NULL != cur){
//先查找做子树
_mid_travel(cur->son[0]);
//if(abs(cur->son[0]->height() - cur->son[1]->height()) >= 2)
{
cur->show();
}
_mid_travel(cur->son[1]);
}
}
//层次遍历,
//如果节点为空就输出0 来占位
template<class T>
void AVLTree<T>::_cengci_travel(AVLNode<T> * cur){
if(NULL == cur)
return;
queue<AVLNode<T>*> q;
q.push(cur);
AVLNode<T> *top = NULL;
queue<AVLNode<T>*> tmp;
while(!q.empty()){
while(!q.empty()){
top = q.front();
q.pop();
if(NULL == top){
//用#代表该节点是空,#的后代还是#
cout << '#' << " ";
tmp.push(top);
}else{
cout << top->data.data << " ";
tmp.push(top->son[0]);
tmp.push(top->son[1]);
}
}
bool flag = false;
while(!tmp.empty()){
if(NULL != tmp.front())
flag = true;
q.push(tmp.front());
tmp.pop();
}
cout << endl;
if(!flag)
break;
}
}
//单旋转,即只旋转一次
//dir = 0时,左左旋转;否则为右右旋转
template<class T>
void AVLTree<T>::_singleRoate(AVLNode<T> *& cur, int dir){
AVLNode<T> *& k2 = cur, * k1 = k2->son[dir]; //k2 必须是引用
k2->son[dir] = k1->son[!dir];
k1->son[!dir] = k2;
k2 = k1;
k2->setHeight();
k1->setHeight();
}
//双旋转,即调两次单旋转
//dir = 0是左右情况; 否则为右左情况
template<class T>
void AVLTree<T>::_doubleRoate(AVLNode<T> *& cur, int dir){
_singleRoate(cur->son[dir], !dir);
_singleRoate(cur, dir);
}
/************* 公有方法(接口)开始**********************/
template<class T>
void AVLTree<T>::insert(const T & data){
_insert(root, data);
}
template<class T>
void AVLTree<T>::mid_travel(){
_mid_travel(root);
}
int main(){
AVLTree<DNode>* avlt = new AVLTree<DNode>();
const int num = 30;
for(int i = 0; i < num; i++){
DNode * d = new DNode(i);
avlt->insert(*d);
}
cout << "*************中序遍历***************" << endl;
avlt->mid_travel();
cout << "**************中序遍历结束**********" << endl;
cout << "*************层次遍历开始***************" << endl;
avlt->cengci_travel();
cout << "**************层次遍历结束**********" << endl;
cout << "树的高度是: " << avlt->height() << endl;
return 0;
}


猜你喜欢
- 现在提起Android开发工具,大多人第一个想到的肯定是Android Studio。谷歌专门为Android开发者推出的这款IDE,以其强
- java后端介绍今天我正式开始了一个新话题,那就是 Web。目前我主要会介绍后端。作为后端的老大哥 java,也有很多后端框架,比如大家耳熟
- 要用TextView使用渐变色,那我们就必须要了解LinearGradient(线性渐变)的用法。LinearGradient的参数解释Li
- 使用JAVA工程管理越来越多的jar包,担心导错了,多导了,漏导了怎么办?换一个IDE项目后项目会不会出一堆BUG,看的头皮发麻?自己写的代
- 本文实例讲述了C#检查键盘大小写锁定状态的方法。分享给大家供大家参考。具体分析如下:1、命名空间:using System.Runtime.
- 本文实例讲述了C#基于NPOI生成具有精确列宽行高的Excel文件的方法,是非常具有实用价值的技巧分享给大家供大家参考。具体方法如下:。一、
- 1.导入System.Runtime.InteropServices命名空间。2.API函数ShowWindow()能够控制人和窗体的现实状
- 1、volley 项目地址 https://github.com/smanikandan14/Volley-demo (1)&nb
- 本文实例为大家分享了C#添加读取Word脚注尾注的具体代码,供大家参考,具体内容如下脚注和尾注是对文本的补充说明。脚注一般位于页面的底部,可
- 在使用ComboBox控件时,遇到了重新绑定赋值出问题的情况。正常情况下,对于数据重新赋值的或者绑定数据源的时候,为了防止数据出现问题,都会
- 这个模拟功能的实现主要依靠了PATH和二阶贝塞尔曲线。首先上一张图来简单看一下:这个模拟功能有以下几个特点:在开始的时候点击圆以外的区域不会
- 本文实例为大家分享了Java生成6位随机字符串的实现代码,具体内容如下package com.amos.tools;import java.
- hibernate一级缓存和二级缓存的区别缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用
- 一、效果图二、代码public class TextSubView extends TextView {private TextPaint
- Mybatis typeAlias配置1.定义别名<typeAliases> <ty
- java里有数字long来表示大的整数,如果两个数字的范围超过了long,要做加法算法怎么做呢?这个问题在面试中经常碰到,如果之前没有经历的
- java 闰年判断前言:给定一个年份,判断这一年是不是闰年。当以下情况之一满足时,这一年是闰年:1. 年份是4的倍数而不是100的倍数;2.
- 概要设计模式是一门艺术,如果真正了解这门艺术,你会发现,世界都将变得更加优美。定义定义一个用于创建对象的接口,让其子类去决定实例化那个类使用
- 本文将讲解如何通过codecogs.com和Google.com提供的API接口来将LaTeX数学函数表达式转化为图片形式。具体思路如下:&
- 本文实例讲述了Android实现便于批量操作可多选的图片ListView。分享给大家供大家参考,具体如下:之前项目需要实现一个可多选的图片列