#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct node{
int val;
struct node *left,*right;
};
node* leftRotate(node *tree){
node *temp = tree->right;
tree->right = temp->left;
temp->left = tree;
return temp;
}
node* righRotate(node *tree){
node *temp = tree->left;
tree->left = temp->right;
temp->right=tree;
return temp;
}
node* rightLeftRotate(node *tree) {
tree->right = rightRotate(tree->right);
return leftRotate(tree);
}
node* leftRightRotate(node *tree){
tree->left = leftRotate(tree->left);
return rightRotate(tree);
}
int getHeight(node *tree){
if(tree == NULL) return 0;
int l = getHeight(tree->left);
int r = getHeight(tree->right);
return max(l,r)+1;
}
node *insert(node *tree, int val){
if(tree ==NULL){
tree = new Node();
tree->val = val;
}else if (tree->val > val){
tree->left = insert(tree->left,val);
int l = getHeight(tree->left),r=getHeight(tree->right);
if(l-r>=2){
if(val<tree->left->val)
tree = rightRotate(tree);
else
tree = leftRightRotate(tree);
}
}else{
tree->right = insert(tree->left,val);
int l = getHeight(tree->left),r=getHeight(tree->right);
if(r-l>=2){
if(val>tree->right->val)
tree = leftRotate(tree);
else
tree = rightLeftRotate(tree);
}
}
return tree;
}
int isComplete = 1, after =0;
vector<int> levelOrder(node *tree){
vector<int> v;
queue<node *> queue;
queue.push(tree);
while(!queue.empty()){
node *temp = queue.front();
queue.pop();
v.push_back(temp->val);
if(temp->left != NULL){
if(after) isComplete = 0;
queue.push(temp->left);
} else{
after = 1;
}
if(temp->right != NULL){
if(after) isComplete = 0;
queue.push(temp->right);
} else{
after = 1;
}
}
return v;
}
int main(int argc, char** argv) {
int n,temp;
scanf("%d",&n);
node *tree = NULL;
for(int i=0;i<n;i++){
scanf("%d",&temp);
tree = insert(tree,temp);
}
vector<int> v=levelOrder(tree);
for(int i=0;i<v.size();i++){
if(i!=0) printf(" ");
printf("%d",v[i]);
}
printf("\n%s",isComplete ? "Yes": "No");
return 0;
}
AVL树的左旋右旋、计算高度以及判断是否完全
原文作者:AVL树
原文地址: https://blog.csdn.net/hit_shaoqi/article/details/81072033
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/hit_shaoqi/article/details/81072033
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。