平衡二叉樹(shù)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ì)樹(shù)中不重復(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;   //樹(shù)的高度
 int size;   //樹(shù)中不重復(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)的意思, 而是由于左子樹(shù)的左兒子的高度太大
 //與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);
 };
};
/*************  私有方法開(kāi)始**********************/
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){
  //先查找做子樹(shù)
  _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);
}
/*************  公有方法(接口)開(kāi)始**********************/
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 << "*************層次遍歷開(kāi)始***************" << endl;
 avlt->cengci_travel();
 cout << "**************層次遍歷結(jié)束**********" << endl;
 cout << "樹(shù)的高度是: " << avlt->height() << endl;
 return 0;
}
上一篇:c語(yǔ)言strftime時(shí)間格式化示例
欄 目:C語(yǔ)言
本文標(biāo)題:平衡二叉樹(shù)AVL操作模板
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3766.html
您可能感興趣的文章
- 01-10深入二叉樹(shù)兩個(gè)結(jié)點(diǎn)的最低共同父結(jié)點(diǎn)的詳解
 - 01-10深入理解二叉樹(shù)的非遞歸遍歷
 - 01-10深入遍歷二叉樹(shù)的各種操作詳解(非遞歸遍歷)
 - 01-10判斷整數(shù)序列是否為二元查找樹(shù)的后序遍歷結(jié)果的解決方法
 - 01-10探討:C++實(shí)現(xiàn)鏈?zhǔn)蕉鏄?shù)(用非遞歸方式先序,中序,后序遍歷二叉樹(shù)
 - 01-10如何在二叉樹(shù)中找出和為某一值的所有路徑
 - 01-10深入linux下遍歷目錄樹(shù)的方法總結(jié)分析
 - 01-10先序遍歷二叉樹(shù)的遞歸實(shí)現(xiàn)與非遞歸實(shí)現(xiàn)深入解析
 - 01-10二叉搜索樹(shù)的插入與刪除(詳細(xì)解析)
 - 01-10二叉查找樹(shù)的插入,刪除,查找
 


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
 - 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
 - 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語(yǔ)言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
 - 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
 - 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
 - 04-02c語(yǔ)言用函數(shù)寫分段 用c語(yǔ)言表示分段
 - 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
 - 04-02c語(yǔ)言編寫函數(shù)冒泡排序 c語(yǔ)言冒泡排
 - 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
 - 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
 - 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
 - 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
 
隨機(jī)閱讀
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
 - 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 04-02jquery與jsp,用jquery
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
 - 01-10delphi制作wav文件的方法
 - 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 - 01-10C#中split用法實(shí)例總結(jié)
 


