平衡二叉樹AVL操作模板
/**
* 目的:實現(xiàn)AVL
* 利用數(shù)組對左右兒子簡化代碼,但是對腦力難度反而增大不少,只適合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; //統(tǒng)計樹中不重復(fù)節(jié)點的個數(shù)
int HEIGHT; //統(tǒng)計數(shù)的高度
//數(shù)據(jù)節(jié)點
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é)點
template<class T>
class AVLNode{
private:
int hgt; //節(jié)點的高度
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é)點中的數(shù)據(jù)類型
template<class T>
class AVLTree{
private:
AVLNode<T> * root; //根節(jié)點
int hgt; //樹的高度
int size; //樹中不重復(fù)節(jié)點數(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é)點為空就輸出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){
//用#代表該節(jié)點是空,#的后代還是#
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時,左左旋轉(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;
}
您可能感興趣的文章
- 01-10深入二叉樹兩個結(jié)點的最低共同父結(jié)點的詳解
- 01-10深入理解二叉樹的非遞歸遍歷
- 01-10深入遍歷二叉樹的各種操作詳解(非遞歸遍歷)
- 01-10判斷整數(shù)序列是否為二元查找樹的后序遍歷結(jié)果的解決方法
- 01-10探討:C++實現(xiàn)鏈式二叉樹(用非遞歸方式先序,中序,后序遍歷二叉樹
- 01-10如何在二叉樹中找出和為某一值的所有路徑
- 01-10深入linux下遍歷目錄樹的方法總結(jié)分析
- 01-10先序遍歷二叉樹的遞歸實現(xiàn)與非遞歸實現(xiàn)深入解析
- 01-10二叉搜索樹的插入與刪除(詳細解析)
- 01-10二叉查找樹的插入,刪除,查找


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)