#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <vector> //随机数
#include <algorithm>//随机数
#include <fstream>
#include <string>
using namespace std;
typedef int ElementType;
typedef struct AVLNode* AVLTree;
struct AVLNode {
ElementType data;
AVLTree left;
AVLTree right;
int height;
};
int Getheight(AVLTree T) {
int hl, hr, maxh;
if (T) {
hl = Getheight(T->left);
hr = Getheight(T->right);
maxh = hl > hr ? hl : hr;
return (maxh + 1);
}
else
return 0;
}
int getBF(AVLTree T)
{
if (T == NULL)
return 0;
return Getheight(T->left) - Getheight(T->right);
}
int MAX(int hl, int hr)
{
return hl > hr ? hl : hr;
}
AVLTree LLType(AVLTree A)
{
AVLTree B = A->left;
A->left = B->right;
B->right = A;
A->height = MAX(Getheight(A->left), Getheight(A->right)) + 1;
B->height = MAX(Getheight(B->left), A->height) + 1;
return B;
}
AVLTree RRType(AVLTree A)
{
AVLTree B = A->right;
A->right = B->left;
B->left = A;
A->height = MAX(Getheight(A->left), Getheight(A->right)) + 1;
B->height = MAX(Getheight(B->right), A->height) + 1;
return B;
}
AVLTree LLRType(AVLTree A)
{
A->left = RRType(A->left);
A = LLType(A);
return A;
}
AVLTree RRLType(AVLTree A)
{
A->right = LLType(A->right);
A = RRType(A);
return A;
}
AVLTree insert(AVLTree T, ElementType data) {
if (T == NULL) {
T = new AVLNode();
T->data = data;
T->left = T->right = NULL;
T->height = 1;
}
else if (data < T->data)
{
T->left = insert(T->left, data);
if(getBF(T)==2)
{
if (data < T->left->data)
T = LLType(T);
else
T = LLRType(T);
}
}
else if (data > T->data)
{
T->right = insert(T->right, data);
if(getBF(T)==-2)
{
if (data > T->right->data)
T = RRType(T);
else
T = RRLType(T);
}
}
else
// cout<<"该值已存在"<<endl;
T->height = MAX(Getheight(T->left), Getheight(T->right)) + 1;
return T;
}
AVLTree leftNode(AVLTree T)
{
AVLTree leftt=T;
while(leftt->left!=NULL){
leftt=leftt->left;
}
return leftt;
}
//删除结点
AVLTree deleteNode(AVLTree &T,int m){
if(T==NULL){
return T;
}
if(m<T->data){
T->left = deleteNode(T->left,m);
}
else if(m>T->data){
T->right = deleteNode(T->right,m);
}
else
{
if ((T->left == NULL) || (T->right == NULL))//左为空或右为空
{
AVLTree temp = T->left ? T->left : T->right;
if (temp == NULL)
{
temp = T;
T = NULL;
}
else
*T = *temp;//根等于子树
delete temp;
}
//左右都有
else
{ //让删除节点右子树的最左侧节点代替当前节点
AVLTree temp=leftNode(T->right);
T->data=temp->data;
T->right=deleteNode(T->right,temp->data);
}
}
//调整
if (T == NULL)
return T;
T->height = 1 + MAX(Getheight(T->left),Getheight(T->right));
int BF = getBF(T);
if (BF> 1 && getBF(T->left) >= 0) //LL
return LLType(T);
if (BF> 1 && getBF(T->left) < 0) //LLR
{
return LLRType(T);
}
if (BF< -1 && getBF(T->right) <= 0) //RR
return RRType(T);
if (BF< -1 && getBF(T->right) > 0) //RRl
{
return RRLType(T);
}
return T;
}
void preorder(AVLTree T)
{
if (T != NULL)
{
printf("%d ", T->data);
preorder(T->left);
preorder(T->right);
}
}
void inorder(AVLTree T)
{
if (T != NULL)
{
inorder(T->left);
printf("%d ", T->data);
inorder(T->right);
}
}
AVLTree searchNode(AVLTree T,int m){
if(!T){
printf("该值不存在");
return NULL;
}
if(T->data==m){
printf("该值存在"); return T;
}
else if(m<T->data) return searchNode(T->left,m);
else return searchNode(T->right,m);
}
void randperm(AVLTree &T,int Num)
{
vector<int> temp;
for (int i = 0; i < Num; ++i)
{
temp.push_back(i + 1);
}
random_shuffle(temp.begin(), temp.end());
for (int i = 0; i < temp.size(); i++)
{
T=insert(T,temp[i]);
}
}
void ofsOrder(AVLTree T,ofstream &outfile){
if(T!=NULL)
{
ofsOrder(T->left,outfile);
outfile<<T->data<<" ";
ofsOrder(T->right,outfile);
}
}
void ofStream(AVLTree T,ofstream &outfile){
outfile.open("AVLTree.txt",ios::trunc);
outfile<<"中序遍历"<<endl;
ofsOrder(T,outfile);
outfile.close();
}
void ifStream(AVLTree &T){
ifstream infile("input.txt",ios::in);
//ifstream是输入文件流
if(!infile) {//文件没打开
cout<<"文件出错"<<endl;
}
int s;//我创建的变量,存储数据用的
while(infile>>s){//输入文件流
T=insert(T,s);
}
}
int main(){
int data;
int x,m,Num,deT,seR,y;
ofstream outfile;
AVLTree T = NULL;
cout<<"---------1、随机个数---------"<<endl;
cout<<"---------2、从文件中读取-----"<<endl;
cin>>y;
switch(y){
case 1:
cout<<"请输入个数:"<<endl;
cin>>Num;
randperm(T,Num);
break;
case 2:
ifStream(T);
break;
}
printf("前序遍历结果");printf("\n");
preorder(T);printf("\n");
printf("中序遍历结果");printf("\n");
inorder(T);printf("\n");
cout<<"请输入删除的值:"<<endl;
cin>>deT;
T = deleteNode(T, deT);
ofStream(T,outfile);
printf("前序遍历结果");printf("\n");
preorder(T);printf("\n");
printf("中序遍历结果");printf("\n");
inorder(T);printf("\n");
cout<<"请输入查找的值:"<<endl;
cin>>seR;
searchNode(T,seR);
return 0;
}