欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

平衡二叉樹AVL操作模板

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

復(fù)制代碼 代碼如下:

/**
* 目的:實現(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;
}

上一篇:c語言strftime時間格式化示例

欄    目:C語言

下一篇:深入理解c++中virtual關(guān)鍵字

本文標題:平衡二叉樹AVL操作模板

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3766.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有