数据结构之AVLTree(04-1:Root of AVL Tree)
来源:互联网 发布:淘宝卖家怎么做直播 编辑:程序博客网 时间:2024/06/05 00:56
// 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;
}
0 0
- 数据结构之AVLTree(04-1:Root of AVL Tree)
- PAT 数据结构 04-树4. Root of AVL Tree (25)
- Root of AVL Tree
- Root of AVL Tree
- Root of AVL Tree
- 04-树5 Root of AVL Tree(25 分)
- 04-树5 Root of AVL Tree(25 分)
- 04-1. Root of AVL Tree (25)
- 04-树4. Root of AVL Tree
- 04-树3. Root of AVL Tree
- 04-树3. Root of AVL Tree
- 04-树5 Root of AVL Tree
- 04-树5 Root of AVL Tree
- 04-树5- Root of AVL Tree
- 04-树5 Root of AVL Tree
- 04-树5 Root of AVL Tree
- 04-树5 Root of AVL Tree
- 04-树5 Root of AVL Tree
- 电子商务和CRM相辅相成
- 《马云创业语录》——创业者就是要面对困难
- 数据结构之队列
- 10.Container With Most Water Leetcode Python
- 8dVASair max運動鞋台灣哪裡找 2JPf KzI
- 数据结构之AVLTree(04-1:Root of AVL Tree)
- Decode Ways - Leetcode
- poj 1125 最短路
- 基于经济学视角对我国职业教育起源进行探讨
- C#——调用exe程序并运行
- [Leetcode]Linked List Cycle II
- Binary Tree Level Order Traversal II
- iptables
- c#函数覆盖、隐藏、重载个人总结