pat 1022(AVL树+完全二叉树)
1066 Root of AVL Tree (25分)
PAT1123 AVL树的调整与判断完全二叉树
link
1110 Complete Binary Tree (25分)
1064 Complete Binary Search Tree (30分)
考察点,二叉平衡树、层序遍历和完全二叉树,
void insert(node* &root,int v){
if(root==NULL){
root=new node;
root->v=v;
root->h=1;
root->lchild=root->rchild=NULL;
return;
}
if(v<root->v){
insert(root->lchild,v);
updateHeight(root);
if(getBalanceFactor(root)==2){
if(getBalanceFactor(root->lchild)==1){
R(root);
}else if (getBalanceFactor(root->lchild)==-1){
L(root->lchild);
R(root);
}
}
}else{
insert(root->rchild,v);
updateHeight(root);
if(getBalanceFactor(root)==-2){
if(getBalanceFactor(root->rchild)==-1){
L(root);
}else if(getBalanceFactor(root->rchild)==1){
R(root->rchild);
L(root);
}
}
}
}
bool isNullAfter=false,isCBT=true;
vector<int>vv;
void bfs(node* root){
queue<node*>q;
q.push(root);
while(!q.empty()){
node* now=q.front();
q.pop();///
vv.push_back(now->v);/
if(now->lchild!=NULL){
q.push(now->lchild);
if(isNullAfter==true)isCBT=false;
}else isNullAfter=true;
if(now->rchild!=NULL){
q.push(now->rchild);/
if(isNullAfter==true)isCBT=false;//
}else isNullAfter=true;
}
}
//8
//88 70 61 96 120 90 65 68
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
int n;
struct node{
int v,h;
node* lchild,*rchild;
};
int getHeight(node* root){
if(root==NULL)return 0;
else return root->h;
}
void updateHeight(node* root){
root->h=max(getHeight(root->lchild),getHeight(root->rchild))+1;
}
int getBalanceFactor(node* root){
return getHeight(root->lchild)-getHeight(root->rchild);
}
void L(node* &root){
node* tmp=root->rchild;
root->rchild=tmp->lchild;
tmp->lchild=root;
updateHeight(root);
updateHeight(tmp);
root=tmp;
}
void R(node* &root){
node* tmp=root->lchild;
root->lchild=tmp->rchild;
tmp->rchild=root;
updateHeight(root);
updateHeight(tmp);
root=tmp;
}
void insert(node* &root,int v){
if(root==NULL){
root=new node;
root->v=v;
root->h=1;
root->lchild=root->rchild=NULL;
return;
}
if(v<root->v){
insert(root->lchild,v);
updateHeight(root);
if(getBalanceFactor(root)==2){
if(getBalanceFactor(root->lchild)==1){
R(root);
}else if (getBalanceFactor(root->lchild)==-1){
L(root->lchild);
R(root);
}
}
}else{
insert(root->rchild,v);
updateHeight(root);
if(getBalanceFactor(root)==-2){
if(getBalanceFactor(root->rchild)==-1){
L(root);
}else if(getBalanceFactor(root->rchild)==1){
R(root->rchild);
L(root);
}
}
}
}
bool isNullAfter=false,isCBT=true;
vector<int>vv;
void bfs(node* root){
queue<node*>q;
q.push(root);
while(!q.empty()){
node* now=q.front();
q.pop();
vv.push_back(now->v);
if(now->lchild!=NULL){
q.push(now->lchild);
if(isNullAfter==true)isCBT=false;
}else isNullAfter=true;
if(now->rchild!=NULL){
q.push(now->rchild);
if(isNullAfter==true)isCBT=false;
}else isNullAfter=true;
}
}
int main(){
cin>>n;
node* root=NULL;
for(int i=0;i<n;i++){
int tmp;
cin>>tmp;
insert(root,tmp);
}
bfs(root);
for(int i=0;i<vv.size();i++){
if(i==0)cout<<vv[i];
else cout<<" "<<vv[i];
}
cout<<endl;
if(isCBT==true)cout<<"YES\n";
else cout<<"NO\n";
}