zoukankan      html  css  js  c++  java
  • 1123 Is It a Complete AVL Tree (30 分)

    这题的旋转过程参考的柳婼学姐的,链接:
    https://blog.csdn.net/liuchuo/article/details/53561924

    思路:

    发现左右高度不平衡的点会遇到四种情况:
    1.左子树的左边插入了结点;
    2.左子树的右边插入了结点;
    3.右子树的左边插入了结点;
    4.右子树的右边插入了结点;
    解决办法分别是:
    1.以此结点右旋;
    2.以此结点的左结点左旋,再以此结点右旋;
    3.以此结点的右结点右旋,再以此结点左旋;
    4.以此结点左旋;

    层次遍历我使用的递归方法,因为好写,但是对完全二叉树的判断就稍微复杂了
    1.中根遍历二叉树,每遍历到一个结点,将此结点放入它所对应的层次序列里,可以保证同一层次左边的结点一定会在右边结点之前遍历到;
    2.记录倒数第二层的每个结点的孩子情况,有孩子记为1,否则记0,左右孩子分开记;
    3.依次将有无孩子的序列放入vector里,如果除了最后一层和倒数第二层,其它所有结点都有左右孩子,且此序列是降序序列(例如:111000或111111,若110100则不行),那么就是一颗完全二叉树,否则就不是;

    代码:

    #include<iostream>
    #include<vector>
    #include<map>
    #include<algorithm>
    using namespace std;
    struct node{
    	int val;
    	struct node *lf,*rt;
    };
    node* leftRotate(node* p){
    	node* temp=p->rt;
    	p->rt=temp->lf;
    	temp->lf=p;
    	return temp;
    }
    node* rightRotate(node* p){
    	node* temp=p->lf;
    	p->lf=temp->rt;		
    	temp->rt=p;
    	return temp;
    }
    node* leftRightRotate(node* p){
    	p->lf=leftRotate(p->lf);
    	return rightRotate(p);
    }
    node* rightLeftRotate(node* p){
    	p->rt=rightRotate(p->rt);
    	return leftRotate(p);
    }
    int getHeight(node* p){
    	if(p==NULL) return 0;
    	int left=getHeight(p->lf);
    	int right=getHeight(p->rt);
    	return max(left,right)+1;
    }
    node* insert(node* p,int v){
    	if(p==NULL){
    		p=new node();
    		p->val=v;
    	}else if(v<p->val){
    		p->lf=insert(p->lf,v);
    		int lfh=getHeight(p->lf),rth=getHeight(p->rt);
    		if(lfh-rth>1){
    			if(v<p->lf->val) return rightRotate(p);
    			else return leftRightRotate(p);
    		}
    	}else if(v>p->val){
    		p->rt=insert(p->rt,v);
    		int lfh=getHeight(p->lf),rth=getHeight(p->rt);
    		if(rth-lfh>1){
    			if(v<p->rt->val) return rightLeftRotate(p);
    			else return leftRotate(p);
    		}
    	}
    	return p;
    }
    map<int,vector<int>> mp;
    map<int,pair<int,int>> leaf;
    int n,val,lvl,flag=true;
    void levelOrder(node* p,int level){
    	mp[level].push_back(p->val);
    	if(p->lf!=NULL) levelOrder(p->lf,level+1);
    	if(p->rt!=NULL) levelOrder(p->rt,level+1);
    	if(level==lvl) leaf[p->val]=make_pair(p->lf!=NULL?1:0,p->rt!=NULL?1:0);
    	if(level!=lvl&&level!=lvl+1&&(p->lf==NULL||p->rt==NULL)) flag=false;
    }
    int main(){
    	cin>>n;
    	node* root=NULL;
    	for(int i=0;i<n;i++){
    		cin>>val;
    		root=insert(root,val);
    	}
    	lvl=getHeight(root)-1;
    	levelOrder(root,1);
    	vector<int> v;
    	for(auto i:leaf){
    		v.push_back(i.second.first);
    		v.push_back(i.second.second);
    	}
    	for(auto i:mp)
    		for(auto j:i.second) printf(j==root->val?"%d":" %d",j);
    	printf(is_sorted(v.begin(),v.end(),greater<int>())&&flag?"
    YES":"
    NO");
    	return 0;
    }
    
  • 相关阅读:
    docker 安装mysql
    Java web项目搭建系列之二 Jetty下运行项目
    Java web项目搭建系列之一 Eclipse中新建Maven项目
    Maven 添加其他Maven组件配置问题
    C# 中定义扩展方法
    Oracle 函数
    【Webservice】2 counts of IllegalAnnotationExceptions Two classes have the same XML type name
    Linux精简版系统安装网络配置问题解决
    Rsync 故障排查整理
    Failed to set session cookie. Maybe you are using HTTP instead of HTTPS to access phpMyAdmin.
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12309027.html
Copyright © 2011-2022 走看看