1.按照给定序列依次插入建立AVL树.
2.树的层序遍历.
3.判断该AVL树是否为完全二叉树.
对于考点3: 建立队列,进行层序遍历时将每一个非空结点的左右孩子节点均加入队列,
然后从头到尾遍历该队列,如果再遍历完所有非空节点之前遇到空节点,则
说明该AVL树并非为完全二叉树。
#include <stdio.h>
#include <stdlib.h>
typedef struct AVLNode{ // THE DEFINITION OF THE AVLNODE;
int KEY;
int HEIGHT;
struct AVLNode * left;
struct AVLNode * right;
}AVLNode;
int HEIGHT ( AVLNode * AVLtree ){
if ( AVLtree == NULL ){
return 0;
}else{
return (HEIGHT(AVLtree->left) > HEIGHT(AVLtree->right) ? HEIGHT(AVLtree->left) : HEIGHT(AVLtree->right)) + 1;
}
}
AVLNode * LL_ROTATION ( AVLNode * AVLtree ){
AVLNode * temp ;
temp = AVLtree -> left ;
AVLtree -> left = temp -> right ;
temp -> right = AVLtree ;
AVLtree-> HEIGHT = (HEIGHT(AVLtree->left) > HEIGHT(AVLtree->right) ? HEIGHT(AVLtree->left) : HEIGHT(AVLtree->right)) + 1;
temp -> HEIGHT = ( HEIGHT(temp->left) > AVLtree->HEIGHT ? HEIGHT(temp->left) : AVLtree->HEIGHT ) + 1;
return temp;
}
AVLNode * RR_ROTATION ( AVLNode * AVLtree ){
AVLNode * temp ;
temp = AVLtree -> right;
AVLtree -> right = temp -> left;
temp -> left = AVLtree;
AVLtree -> HEIGHT = (HEIGHT(AVLtree ->left) > HEIGHT(AVLtree -> right) ? HEIGHT(AVLtree->left) : HEIGHT(AVLtree->right) ) + 1;
temp -> HEIGHT = ( AVLtree-> HEIGHT > HEIGHT(temp -> right) ? AVLtree -> HEIGHT : HEIGHT( temp-> right) ) + 1;
return temp;
}
AVLNode * LR_ROTATION ( AVLNode * AVLtree ){
AVLNode * temp ;
temp = AVLtree -> left;
AVLtree -> left = RR_ROTATION(temp);
return LL_ROTATION(AVLtree);
}
AVLNode *RL_ROTATION ( AVLNode * AVLtree ){
AVLNode * temp ;
temp = AVLtree -> right;
AVLtree -> right = LL_ROTATION(temp);
return RR_ROTATION(AVLtree);
}
AVLNode * AVL_INSERT ( AVLNode * AVLtree , int key ){
if ( AVLtree == NULL ){
AVLtree = (AVLNode *) malloc( sizeof(AVLNode) );
AVLtree -> KEY = key ;
AVLtree -> HEIGHT = 0 ;
AVLtree -> left = NULL;
AVLtree -> right = NULL;
}else if ( key < AVLtree -> KEY ){
AVLtree->left = AVL_INSERT(AVLtree->left, key);
if( HEIGHT(AVLtree->left) - HEIGHT(AVLtree ->right) >= 2 ){
if ( key < AVLtree -> left -> KEY ){
AVLtree = LL_ROTATION(AVLtree);
}else{
AVLtree = LR_ROTATION(AVLtree);
}
}
}else{
AVLtree->right = AVL_INSERT(AVLtree->right, key);
if (HEIGHT(AVLtree -> right) - HEIGHT(AVLtree -> left) >= 2){
if( key > AVLtree -> right ->KEY ){
AVLtree = RR_ROTATION(AVLtree);
}else{
AVLtree = RL_ROTATION(AVLtree);
}
}
}
return AVLtree;
}
int main(int argc, const char * argv[]) {
int N ;
scanf ( "%d" , &N );
int *KEY = (int *)malloc( N * sizeof(int) );
AVLNode *AVLtree = NULL;
for ( int i = 0 ; i < N ; i++ ){
scanf("%d",&KEY[i]);
AVLtree = AVL_INSERT(AVLtree, KEY[i]);
}
int isCompleteBinTREE = 1;
AVLNode ** QUEUE =(AVLNode **)malloc( 100 * sizeof(AVLNode *) );
int front = 0;
int rear = 0;
QUEUE[rear++] = AVLtree;
while (front != rear) {
AVLNode *temp = QUEUE[front];
front++;
if( temp != NULL ){
QUEUE[rear++] = temp->left;
QUEUE[rear++] = temp->right;
}
}
int cnt = 0;
int *result = (int *)malloc(N * sizeof(int) );
for ( int i = 0 ; i < rear ; i++ ){
if(QUEUE[i] == NULL ){
isCompleteBinTREE = 0;
}else{
result[cnt++] = QUEUE[i]->KEY;
}
if ( cnt == N ){
break;
}
}
for (int i = 0 ; i < N ; i++ ){
printf("%d",result[i]);
if ( i != N-1 ){
printf(" ");
}else{
printf("\n");
}
}
if (isCompleteBinTREE == 1){
printf("YES\n");
}else{
printf("NO\n");
}
return 0;
}