Day 45 算法笔记之提高篇(3)9.5 平衡二叉树

目录

1.节点定义

2.新建节点

3.计算平衡因子

4.更新高度

5.查找(与二叉查找树相同)

6.左旋

7.插入

8.建树

9.Root of AVL Tree


1.节点定义

struct node{
	int v,height;
	node *lchild,*rchild;
};

2.新建节点

node* newnode(int v){
	node* nodes=new node;
	nodes->v=v;
	nodes->height=1;
	nodes->lchild=nodes->rchild=NULL;
	return nodes;
}

3.计算平衡因子

int getheight(node* root){
	if(root==NULL) return 0;
	return root->height;
}

int getbalancefactor(node* root){
	return getheight(root->lchild)-getheight(root->rchild);
}

4.更新高度

void updateheight(node* root){
	root->height = max(getheight(root->lchild),getheight(root->rchild))+1;
}

5.查找(与二叉查找树相同)

void search(node* root,int x){
	if(root==NULL){
		printf("search failed\n");
		return;
	}
	if(x==root->v){
		printf("%d\n",root->v);
	}else if(x<root->v){
		search(root->lchild,x);
	}else{
		search(root->rchild,x);
	}
}

6.左旋

void l(node* &root){
	node* temp = root->rchild;
	root->rchild=temp->rchild;
	temp->lchild=root;
	updateheight(root);
	updataheight(temp);
	root=temp;
}

7.插入


void insert(node* &root,int v){
	if(root==NULL){
		root=newnode(v);
		return;
	}
	if(v<root->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->lchild)==-1){
				l(root);
			}else if(getbalancefactor(root->rchild)==1){
				r(root->rchild);
				l(root);
			}
		}
	}
}

8.建树

node* create(int data[],int n){
	node* root = NULL;
	for(int i=0;i<n;i++){
		insert(root,data[i]);
	}
	return root;
}

9.Root of AVL Tree

#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

struct node{
	int v,height;
	node *lchild,*rchild;
} *root;

node* newnode(int v){
	node* nodes=new node;
	nodes->v=v;
	nodes->height=1;
	nodes->lchild=nodes->rchild=NULL;
	return nodes;
}

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 search(node* root,int x){
	if(root==NULL){
		printf("search failed\n");
		return;
	}
	if(x==root->v){
		printf("%d\n",root->v);
	}else if(x<root->v){
		search(root->lchild,x);
	}else{
		search(root->rchild,x);
	}
}

void l(node* &root){
	node* temp = root->rchild;
	root->rchild=temp->lchild;
	temp->lchild=root;
	updateheight(root);
	updateheight(temp);
	root=temp;
}

void r(node* &root){
	node* 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(v<root->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){
				l(root);
			}else if(getbalancefactor(root->rchild)==1){
				r(root->rchild);
				l(root);
			}
		}
	}
}

node* create(int data[],int n){
	node* root = NULL;
	for(int i=0;i<n;i++){
		insert(root,data[i]);
	}
	return root;
}

int main(){
	
	int n,v;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&v);
		insert(root,v);
	}
	
	printf("%d\n",root->v);
	return 0;
}

おすすめ

転載: blog.csdn.net/aixiaoxiao13/article/details/121472826