Balanced binary tree AVL operation template

  • 2020-04-02 02:12:03
  • OfStack



#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <time.h>
#include <queue>
using namespace std;
int COUNT; //Count the number of non-repeating nodes in the tree
int HEIGHT; //The height of the statistics
//Data nodes
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;
 }
};
//The node treap
template<class T>
class AVLNode{
private:
 int hgt; //Height of node
public:
 T data;
 int count;

 AVLNode<T> *son[2]; //0 is the son of the left, and 1 is the son of the right
 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
//The T here is the data type in the node
template<class T>
class AVLTree{
private:
 AVLNode<T> * root;  //The root node
 int hgt;   //The height of the tree
 int size;   //No duplicate number of nodes in the tree
 void _insert(AVLNode<T> *& cur, T data);
 void _mid_travel(AVLNode<T> *cur);
 //Level traversal
 void _cengci_travel(AVLNode<T> *cur);
 //Single rotation (left left, right right), left rotation does not mean left rotation, but because the left son of the left subtree is too high
 //This is the opposite of the rotation name for treap
 void _singleRoate(AVLNode<T> *& cur, int dir);
 //Double rotation (two single rotations)
 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();
 }
 //Level traversal
 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){
  //So let's do subtrees
  _mid_travel(cur->son[0]);
  //if(abs(cur->son[0]->height() - cur->son[1]->height()) >= 2)
  {
   cur->show();
  }
  _mid_travel(cur->son[1]);
 }
}
//Level traversal . 
//If the node is empty, output 0 to occupy the space
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){
    //The use of # indicates whether the node is empty, the descendant of #, or #
    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;
 }
}
//Single rotation, that is, only one rotation
//Dir = 0, rotate left to left; Otherwise it rotates right to right
template<class T>
void AVLTree<T>::_singleRoate(AVLNode<T> *& cur, int dir){
 AVLNode<T> *& k2 = cur, * k1 = k2->son[dir]; //K2 must be a reference
 k2->son[dir] = k1->son[!dir];
 k1->son[!dir] = k2;
 k2 = k1;

 k2->setHeight();
 k1->setHeight();
}
//Double rotation, that is, two single rotation
//Dir = 0 is left and right; Otherwise, it is right-left case
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 << "************* In the sequence traversal ***************" << endl;
 avlt->mid_travel();
 cout << "************** Middle order traversal ends **********" << endl;
 cout << "*************Level traversal start ***************" << endl;
 avlt->cengci_travel();
 cout << "**************Level traversal The end of the **********" << endl;
 cout << "The height of the tree Is this:  " << avlt->height() << endl;
 return 0;
}


Related articles: