我的心愿是世界和平!
平衡二叉树:不想写定义~~(可以写,但是没必要)~~
#include<cstdio>
#include<iostream>
using namespace std;
typedef struct node{
int data;
int height;
struct node *left;
struct node *right;
}*tree,Tree;
int Height(tree &T,int len)
{
if(T == NULL) return len;
if(T->left == NULL && T->right == NULL) return T->height;
return max(Height(T->left,T->height),Height(T->right,T->height));
}
void change(tree &T,int len){
if(T == NULL) return ;
T->height = len;
change(T->left,len+1);
change(T->right,len+1);
}
//单旋转右旋
void singleRotateWithRight(tree &T)
{
int t = T->height;
tree L = T;
T = L->left;
L->left = T->right;
T->right = L;
T->height = t;
change(T->left,t+1);
change(T->right,t+1);
}
//单旋转左旋
void singleRotateWithLeft(tree &T)
{
int t = T->height;
tree R = T;
T = R->right;
R->right = T->left;
T->left = R;
T->height = t;
change(T->left,t+1);
change(T->right,t+1);
}
//双旋转,先左后右
void doubleRotateWithLeft(tree &T)
{
singleRotateWithLeft(T->left);
singleRotateWithRight(T);
}
//双旋转,先右后左
void doubleRotateWithRight(tree &T)
{
singleRotateWithRight(T->right);
singleRotateWithLeft(T);
}
void addNode(tree &T,int x,int len)
{
if(T==NULL){
T=new Tree;
T->data = x;
T->left = NULL;
T->right = NULL;
T->height = len;
}else if(x < T->data){
addNode(T->left,x,len+1);
if(Height(T->left,T->height) - Height(T->right,T->height) == 2){//获得深度判断左右节点深度差
if(x < T->left->data){
singleRotateWithRight(T);
}else{
doubleRotateWithLeft(T);
}
}
}else if(x > T->data){
addNode(T->right,x,len+1);
if(Height(T->right,T->height) - Height(T->left,T->height) == 2){
if(x > T->right->data){
singleRotateWithLeft(T);
}else{
doubleRotateWithRight(T);
}
}
}
}
int main(){
tree T = NULL;
int m,a;
scanf("%d",&m);
for(int i = 0;i < m;i++){
scanf("%d",&a);
addNode(T,a,0);// T 这是我的树,a 这是树的节点值, 0 这是树的该节点的深度
}
return 0;
}
题目描述:An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property.
Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.
输入:Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 20). Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.
输出:or each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print YES if the tree is complete, or NO if not.
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<map>
using namespace std;
typedef struct node{
int data;
int height;
struct node *left;
struct node *right;
}*tree,Tree;
int m,a;
int Height(tree &T,int len)
{
if(T == NULL) return len;
if(T->left == NULL && T->right == NULL) return T->height;
return max(Height(T->left,T->height),Height(T->right,T->height));
}
void change(tree &T,int len){
if(T == NULL) return ;
T->height = len;
change(T->left,len+1);
change(T->right,len+1);
}
void singleRotateWithRight(tree &T)
{
int t = T->height;
tree L = T;
T = L->left;
L->left = T->right;
T->right = L;
T->height = t;
change(T->left,t+1);
change(T->right,t+1);
}
void singleRotateWithLeft(tree &T)
{
int t = T->height;
tree R = T;
T = R->right;
R->right = T->left;
T->left = R;
T->height = t;
change(T->left,t+1);
change(T->right,t+1);
}
void doubleRotateWithLeft(tree &T)
{
singleRotateWithLeft(T->left);
singleRotateWithRight(T);
}
void doubleRotateWithRight(tree &T)
{
singleRotateWithRight(T->right);
singleRotateWithLeft(T);
}
void addNode(tree &T,int x,int len)
{
if(T==NULL){
T=new Tree;
T->data = x;
T->left = NULL;
T->right = NULL;
T->height = len;
}else if(x < T->data){
addNode(T->left,x,len+1);
if(Height(T->left,T->height) - Height(T->right,T->height) == 2){
if(x < T->left->data){
singleRotateWithRight(T);
}else{
doubleRotateWithLeft(T);
}
}
}else if(x > T->data){
addNode(T->right,x,len+1);
if(Height(T->right,T->height) - Height(T->left,T->height) == 2){
if(x > T->right->data){
singleRotateWithLeft(T);
}else{
doubleRotateWithRight(T);
}
}
}
}
void levelOrder(tree &T){
queue<int>q;
queue<tree>que;
tree t;
que.push(T);
int flag = 1,sign = 1;
while(!que.empty()){
t = new Tree;
t = que.front();
que.pop();
q.push(t->data);
if(t->left != NULL){
que.push(t->left);
if(flag == 0){
sign = 0;
}
}else{
flag = 0;
}
if(t->right!=NULL){
que.push(t->right);
if(flag == 0){
sign = 0;
}
}else{
flag = 0;
}
}
cout<<q.front();
q.pop();
while(!q.empty()){
cout<<" "<<q.front();
q.pop();
}
if(sign) printf("\nYES");
else printf("\nNO");
}
int main(){
tree T = NULL;
scanf("%d",&m);
for(int i = 0;i < m;i++){
scanf("%d",&a);
addNode(T,a,0);
}
levelOrder(T);
return 0;
}