天天看點

1123 Is It a Complete AVL Tree (30 point(s))

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. Figures 1-4 illustrate the rotation rules.

1123 Is It a Complete AVL Tree (30 point(s))
1123 Is It a Complete AVL Tree (30 point(s))
1123 Is It a Complete AVL Tree (30 point(s))
1123 Is It a Complete AVL Tree (30 point(s))

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.

Input Specification:

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.

Output Specification:

For 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.

Sample Input 1:

5
88 70 61 63 65
           

Sample Output 1:

70 63 88 61 65
YES
           

Sample Input 2:

8
88 70 61 96 120 90 65 68
           

Sample Output 2:

88 65 96 61 70 90 120 68
NO
           

題目大意:給你n個資料,讓你建立一個AVL,然後輸出它的層次周遊并判斷它是不是一顆完全二叉樹。

分析:

1)如何建立AVL:參考模闆

2)完全二叉樹的判斷&&層次周遊輸出:完全二叉樹判斷模闆

完全二叉樹的判斷借助層次周遊完成:

做法:層次周遊時将所有結點入隊(包括非空結點),開一個cnt,記錄層次周遊已經輸出的結點總數。周遊過程中如果空結點後面還有非空結點,那就不是完全二叉樹  -->轉化為 遇到空指針時 cnt<n ,此時 flag标記一下 ( n為總結點數)。最後BFS()傳回flag即可。

完整代碼:

#include<bits/stdc++.h>
using namespace std;
int n;
struct node{
	int val;
	node *l,*r;
};
node *RR(node *root){ // 右旋 
	node *temp=root->l;
	root->l =temp->r ;
	temp->r=root;
	return temp;
}

node *LL(node *root){ //左旋 
	node *temp=root->r ;
	root->r =temp->l ;
	temp->l =root;
	return temp;
}

node *RL(node *root){//麻煩結點在右子樹的左子樹 
	root->r=RR(root->r );
	return LL(root);
}

node *LR(node *root){ //麻煩結點在左子樹的右子樹 
	root->l=LL(root->l );
	return RR(root);
}

int getHeight(node *root){
	if(!root) return 0;
	return max(getHeight(root->l),getHeight(root->r ) ) +1;
}
 
node *insert(node *&root, int val) {  //建立 BST tree 并調整平衡,不能有相同節點 
    if(!root) {
        root = new node;
        root->val = val;
        root->l= root->r = NULL;
    } else if(val < root->val) {
        insert(root->l, val); //插入
        if(getHeight(root->l) - getHeight(root->r) ==2)
            root = val < root->l->val ? RR(root) : LR(root);   //旋轉調整平衡 
    } else {
        insert(root->r, val);//插入
        if(getHeight(root->l) - getHeight(root->r) == -2)
            root = val > root->r->val ? LL(root) : RL(root); //旋轉調整平衡
    }
    return root;
}

bool BFS(node *root){
	if(!root) return true;
	queue<node*>q;
	q.push(root);
	bool flag=true;
	int cnt=0;
	while(!q.empty()){
		node *temp=q.front();
		q.pop();
		if(temp!=NULL){
			cnt++;
			printf("%d%s",temp->val,cnt==n?"\n":" ");
			q.push(temp->l ); //不用判空,直接入隊
			q.push(temp->r );
		}else 
			if(cnt<n) flag=false;//說明空結點後還有非空結點 
    }
	return flag;
}
			
int main(){
	cin>>n;
	node *root=NULL;
	for(int i=0;i<n;i++){
		int val;
		cin>>val;
		root=insert(root,val);
	}
	printf("%s",BFS(root)>0? "YES\n":"NO\n");
	return 0;
}			
           

That‘s all !

繼續閱讀