// Root_of_AVL_Tree.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#define MAX 50
int length=0;
typedef struct node
{
int data;
struct node *left;
struct node *right;
}TREE;
/*
void LevelOrderTranversal(TREE * tree){
Queue q;
InitQueue(&q);
if(!tree) return;
EnQueue(&q,tree->data);
while(!IsQueueEmpty(&q))
{int tmp=DeQueue(&q);
if(tree->left) EnQueue(&q,tree->left->data);
if(tree->right) EnQueue(&q,tree->right->data);
}
}
*/
//把数组方式的二叉树变成链表形式,这里没用到
TREE *createTree(int * data,int pos){
if(pos>length||data[pos]==0)
return NULL;
TREE* tree=NULL;
tree=(TREE *) malloc(sizeof(TREE));
tree->data=data[pos];
tree->left=createTree(data,2*pos+1);
tree->right=createTree(data,2*pos+2);
return tree;
}
void printTree(TREE * tree){
if(tree)
{printf("%d",tree->data);
printTree(tree->left);
printTree(tree->right);
}
}
int level(TREE * tree)
{
int l=0,r=0,maxH=0;
int i=0;
if(tree)
{
l=level(tree->left);
r=level(tree->right);
maxH=l>r?l:r;
return maxH+1;
}else
return 0;
}
int isBalanced(TREE * tree){
int flag=1;
if(abs(level(tree->left)-level(tree->right))==2)
flag=0;
else
flag=1;
return flag;
}
void changeToAVLTree(TREE * tree)
{
if(!isBalanced(tree))
changeToAVLTree(tree->left);
}
TREE * SingleLeftRotation(TREE * tree)
{
TREE * b=tree->left;
tree->left=b->right;
b->right=tree;
return b;
}
TREE * SingleRightRotation(TREE * tree)
{
TREE * b=tree->right;
tree->right=b->left;
b->left=tree;
return b;
}
TREE * DoubleLeftRightRotation(TREE * tree)
{
tree->left=SingleRightRotation(tree->left);
return SingleLeftRotation(tree);
}
TREE * DoubleRightLeftRotation(TREE * tree)
{
tree->right=SingleLeftRotation(tree->right);
return SingleRightRotation(tree);
}
递归方法插入节点
TREE * insert(TREE * root, int x)
{
if(root == NULL){
TREE* p = (TREE*)malloc(sizeof(TREE));
p->data = x;
p->left = NULL;
p->right = NULL;
root = p;
}
else if(x < root->data){
root->left = insert(root->left, x);
if(abs(level(root->left)-level(root->right))==2)
if(x<root->left->data)
root=SingleLeftRotation(root);
else
root=DoubleLeftRightRotation(root);
}
else{
root->right = insert(root->right, x);
if(abs(level(root->left)-level(root->right))==2)
if(x>root->right->data)
root=SingleRightRotation(root);
else
root=DoubleRightLeftRotation(root);
}
return root;
}
int main()
{
int data[MAX]={0}; //0 为设定的初始值,
int i=0;
scanf("%d",&length);
while(i<length)
{
scanf("%d",&data[i]);
i++;
}
TREE * tree=0;
for(i=0;i<length;i++){
tree=insert(tree,data[i]);
}
printTree(tree);
//TREE * tree=createTree(data,0);
return 0;
}
//
#include "stdafx.h"
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#define MAX 50
int length=0;
typedef struct node
{
int data;
struct node *left;
struct node *right;
}TREE;
/*
void LevelOrderTranversal(TREE * tree){
Queue q;
InitQueue(&q);
if(!tree) return;
EnQueue(&q,tree->data);
while(!IsQueueEmpty(&q))
{int tmp=DeQueue(&q);
if(tree->left) EnQueue(&q,tree->left->data);
if(tree->right) EnQueue(&q,tree->right->data);
}
}
*/
//把数组方式的二叉树变成链表形式,这里没用到
TREE *createTree(int * data,int pos){
if(pos>length||data[pos]==0)
return NULL;
TREE* tree=NULL;
tree=(TREE *) malloc(sizeof(TREE));
tree->data=data[pos];
tree->left=createTree(data,2*pos+1);
tree->right=createTree(data,2*pos+2);
return tree;
}
void printTree(TREE * tree){
if(tree)
{printf("%d",tree->data);
printTree(tree->left);
printTree(tree->right);
}
}
int level(TREE * tree)
{
int l=0,r=0,maxH=0;
int i=0;
if(tree)
{
l=level(tree->left);
r=level(tree->right);
maxH=l>r?l:r;
return maxH+1;
}else
return 0;
}
int isBalanced(TREE * tree){
int flag=1;
if(abs(level(tree->left)-level(tree->right))==2)
flag=0;
else
flag=1;
return flag;
}
void changeToAVLTree(TREE * tree)
{
if(!isBalanced(tree))
changeToAVLTree(tree->left);
}
TREE * SingleLeftRotation(TREE * tree)
{
TREE * b=tree->left;
tree->left=b->right;
b->right=tree;
return b;
}
TREE * SingleRightRotation(TREE * tree)
{
TREE * b=tree->right;
tree->right=b->left;
b->left=tree;
return b;
}
TREE * DoubleLeftRightRotation(TREE * tree)
{
tree->left=SingleRightRotation(tree->left);
return SingleLeftRotation(tree);
}
TREE * DoubleRightLeftRotation(TREE * tree)
{
tree->right=SingleLeftRotation(tree->right);
return SingleRightRotation(tree);
}
递归方法插入节点
TREE * insert(TREE * root, int x)
{
if(root == NULL){
TREE* p = (TREE*)malloc(sizeof(TREE));
p->data = x;
p->left = NULL;
p->right = NULL;
root = p;
}
else if(x < root->data){
root->left = insert(root->left, x);
if(abs(level(root->left)-level(root->right))==2)
if(x<root->left->data)
root=SingleLeftRotation(root);
else
root=DoubleLeftRightRotation(root);
}
else{
root->right = insert(root->right, x);
if(abs(level(root->left)-level(root->right))==2)
if(x>root->right->data)
root=SingleRightRotation(root);
else
root=DoubleRightLeftRotation(root);
}
return root;
}
int main()
{
int data[MAX]={0}; //0 为设定的初始值,
int i=0;
scanf("%d",&length);
while(i<length)
{
scanf("%d",&data[i]);
i++;
}
TREE * tree=0;
for(i=0;i<length;i++){
tree=insert(tree,data[i]);
}
printTree(tree);
//TREE * tree=createTree(data,0);
return 0;
}