PAT程序设计考题——甲级1066(Root of AVL Tree ) C++实现
来源:互联网 发布:android源码编译后rom 编辑:程序博客网 时间:2024/06/11 12:45
点击打开pat链接#include<iostream>
#include<math.h>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<vector>
using namespace std;
#define INF 100000000
#define maxn 100010
#include<math.h>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<vector>
using namespace std;
#define INF 100000000
#define maxn 100010
struct node{
int num,height;
node *lchild,*rchild;
};
node* newnode(int v)
{ node *root;
root=new node;
root->num=v;
root->height=1;
root->lchild=root->rchild=NULL;
return root;
}
int getheight(node *root)
{ if(root==NULL) return 0;
return root->height;
}
int getbalancefactor(node *root )
{
return getheight(root->lchild)-getheight(root->rchild);
}
void updateheight(node* root)
{
root->height=max(getheight(root->lchild),getheight(root->rchild))+1;
}
void L(node *&root){
node *temp;
temp=root->rchild;
root->rchild=temp->lchild;
temp->lchild=root;
updateheight(root);
updateheight(temp);
root=temp;
}
void R(node *&root)
{
node *temp;
temp=root->lchild;
root->lchild=temp->rchild;
temp->rchild=root;
updateheight(root);
updateheight(temp);
root=temp;
}
void insert(node* &root,int v)
{
if(root==NULL) {
root=newnode(v);
return;}
if(root->num>v)
{
insert(root->lchild,v);
updateheight(root);
if(getbalancefactor(root)==2)
{ if(getbalancefactor(root->lchild)==1)
{ R(root);
}
int num,height;
node *lchild,*rchild;
};
node* newnode(int v)
{ node *root;
root=new node;
root->num=v;
root->height=1;
root->lchild=root->rchild=NULL;
return root;
}
int getheight(node *root)
{ if(root==NULL) return 0;
return root->height;
}
int getbalancefactor(node *root )
{
return getheight(root->lchild)-getheight(root->rchild);
}
void updateheight(node* root)
{
root->height=max(getheight(root->lchild),getheight(root->rchild))+1;
}
void L(node *&root){
node *temp;
temp=root->rchild;
root->rchild=temp->lchild;
temp->lchild=root;
updateheight(root);
updateheight(temp);
root=temp;
}
void R(node *&root)
{
node *temp;
temp=root->lchild;
root->lchild=temp->rchild;
temp->rchild=root;
updateheight(root);
updateheight(temp);
root=temp;
}
void insert(node* &root,int v)
{
if(root==NULL) {
root=newnode(v);
return;}
if(root->num>v)
{
insert(root->lchild,v);
updateheight(root);
if(getbalancefactor(root)==2)
{ if(getbalancefactor(root->lchild)==1)
{ R(root);
}
else if(getbalancefactor(root->lchild)==-1)
{ L(root->lchild);
R(root);
}} }
else{
insert(root->rchild,v);
updateheight(root);
if(getbalancefactor(root)==-2){
if(getbalancefactor(root->rchild)==1)
{
R(root->rchild);
L(root);
}
else if(getbalancefactor(root->rchild)==-1)
{ L(root);
}
}
}
}
int main(){
int num;
cin>>num;
node *root;
int qw;
for(int i=0;i<num;i++)
{
cin>>qw;
insert(root,qw);
}
cout<<root->num;
return 0;
}
{ L(root->lchild);
R(root);
}} }
else{
insert(root->rchild,v);
updateheight(root);
if(getbalancefactor(root)==-2){
if(getbalancefactor(root->rchild)==1)
{
R(root->rchild);
L(root);
}
else if(getbalancefactor(root->rchild)==-1)
{ L(root);
}
}
}
}
int main(){
int num;
cin>>num;
node *root;
int qw;
for(int i=0;i<num;i++)
{
cin>>qw;
insert(root,qw);
}
cout<<root->num;
return 0;
}
阅读全文
0 0
- PAT程序设计考题——甲级1066(Root of AVL Tree ) C++实现
- PAT程序设计考题——甲级1043(Is It a Binary Search Tree ) C++实现
- PAT程序设计考题——甲级1020( Tree traverse ) C++实现
- PAT程序设计考题——甲级1102( Invert a Binary Tree ) C++实现
- PAT程序设计考题——甲级1009( Product of Polynomials ) C++实现
- PAT程序设计考题——甲级1053( Path of Equal Weight) C++实现
- PAT程序设计考题——甲级1079( Total Sales of Supply Chain ) C++实现
- PAT程序设计考题——甲级1065( A+B and C (64bit) ) C++实现
- 1066. Root of AVL Tree (25)-PAT甲级真题
- 【PAT甲级】1066. Root of AVL Tree (25)
- 1066. Root of AVL Tree (25) PAT甲级
- 未完成-PAT甲级练习1066. Root of AVL Tree (25)
- PAT甲级1066. Root of AVL Tree (25)
- PAT 甲级 1066. Root of AVL Tree (25)
- PAT程序设计考题——甲级1007 (最大连续子序列和) C++实现
- PAT程序设计考题——甲级1045 (最长不下降子序列) C++实现
- PAT程序设计考题——甲级1045 (最长公共子序列) C++实现
- PAT程序设计考题——甲级1040 (最长回文串) C++实现
- 学习shiro代码报错:Line argument must contain a key and a value. Only one string token was found.
- 牛客网_华为机试_005_进制转换
- 2017.7.05 学习记录 Servlet02
- 自适应分辨率引用单元
- 快速开发android应用1-服务器搭建
- PAT程序设计考题——甲级1066(Root of AVL Tree ) C++实现
- 将博客搬至CSDN
- 【JavaScript的调试】
- Hazama下载安装使用教程
- 一份面试总结
- java获取当前路径的几种方法
- caffe mnist测试Check failed: status == CUDNN_STATUS_SUCCESS (4 vs. 0) CUDNN_STATUS_INTERNA
- 第一篇博客,习惯性Hello,world~!
- 利用Python将多个excel文件合并为一个文件