平衡二叉樹AVL操作模板
/**
* 目的:實(shí)現(xiàn)AVL
* 利用數(shù)組對(duì)左右兒子簡(jiǎn)化代碼,但是對(duì)腦力難度反而增大不少,只適合acm模板
* 其實(shí)avl在acm中基本不用,基本被treap取代
* avl一般只要求理解思路,不要求寫出代碼,因?yàn)檎嫘暮軣?BR>*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <time.h>
#include <queue>
using namespace std;
int COUNT; //統(tǒng)計(jì)樹中不重復(fù)節(jié)點(diǎn)的個(gè)數(shù)
int HEIGHT; //統(tǒng)計(jì)數(shù)的高度
//數(shù)據(jù)節(jié)點(diǎn)
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的節(jié)點(diǎn)
template<class T>
class AVLNode{
private:
int hgt; //節(jié)點(diǎn)的高度
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是節(jié)點(diǎn)中的數(shù)據(jù)類型
template<class T>
class AVLTree{
private:
AVLNode<T> * root; //根節(jié)點(diǎn)
int hgt; //樹的高度
int size; //樹中不重復(fù)節(jié)點(diǎn)數(shù)量
void _insert(AVLNode<T> *& cur, T data);
void _mid_travel(AVLNode<T> *cur);
//層次遍歷
void _cengci_travel(AVLNode<T> *cur);
//單旋轉(zhuǎn)(左左,右右), 左旋不是想左旋轉(zhuǎn)的意思, 而是由于左子樹的左兒子的高度太大
//與treap的旋轉(zhuǎn)命名相反
void _singleRoate(AVLNode<T> *& cur, int dir);
//雙旋轉(zhuǎn)(兩次單旋轉(zhuǎn))
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]);
}
}
//層次遍歷,
//如果節(jié)點(diǎn)為空就輸出0 來(lái)占位
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){
//用#代表該節(jié)點(diǎn)是空,#的后代還是#
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;
}
}
//單旋轉(zhuǎn),即只旋轉(zhuǎn)一次
//dir = 0時(shí),左左旋轉(zhuǎn);否則為右右旋轉(zhuǎn)
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();
}
//雙旋轉(zhuǎn),即調(diào)兩次單旋轉(zhuǎn)
//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 << "**************中序遍歷結(jié)束**********" << endl;
cout << "*************層次遍歷開始***************" << endl;
avlt->cengci_travel();
cout << "**************層次遍歷結(jié)束**********" << endl;
cout << "樹的高度是: " << avlt->height() << endl;
return 0;
}
相關(guān)文章
C++深入探索內(nèi)聯(lián)函數(shù)inline與auto關(guān)鍵字的使用
本篇文章主要包括內(nèi)聯(lián)函數(shù)和auto關(guān)鍵字。其中,內(nèi)斂函數(shù)包括概念,特性等;auto關(guān)鍵字的使用規(guī)則,使用場(chǎng)景等,接下來(lái)讓我們深入了解2022-05-05OpenCV利用背景建模檢測(cè)運(yùn)動(dòng)物體
這篇文章主要為大家詳細(xì)介紹了OpenCV利用背景建模檢測(cè)運(yùn)動(dòng)物體,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01C++ accumulate函數(shù)詳細(xì)介紹和具體案例
這篇文章主要介紹了C++ accumulate函數(shù)詳細(xì)介紹和具體案例,accumulate是numeric庫(kù)中的一個(gè)函數(shù),主要用來(lái)對(duì)指定范圍內(nèi)元素求和,但也自行指定一些其他操作,如范圍內(nèi)所有元素相乘、相除等2022-08-08c++將字符串轉(zhuǎn)數(shù)字的實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于c++將字符串轉(zhuǎn)數(shù)字的實(shí)例方法,有需要的朋友們可以參考下。2020-02-02新手小心:c語(yǔ)言中強(qiáng)符號(hào)與弱符號(hào)的使用
本篇文章適合新手。是對(duì)c語(yǔ)言中強(qiáng)符號(hào)與弱符號(hào)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05詳解C++設(shè)計(jì)模式編程中策略模式的優(yōu)缺點(diǎn)及實(shí)現(xiàn)
這篇文章主要介紹了C++設(shè)計(jì)模式編程中策略模式的優(yōu)缺點(diǎn)及實(shí)現(xiàn),文中討論了策略模式中設(shè)計(jì)抽象接口的繼承和組合之間的區(qū)別,需要的朋友可以參考下2016-03-03C++運(yùn)算符重載實(shí)例代碼詳解(調(diào)試環(huán)境 Visual Studio 2019)
這篇文章主要介紹了C++運(yùn)算符重載實(shí)例(調(diào)試環(huán)境 Visual Studio 2019),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03