PAT-ADVANCED1066/Data Structures and Algorithms7-6——Root of AVL Tree

我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED

我的Data Structures and Algorithms代码仓:https://github.com/617076674/Data-Structures-and-Algorithms

原题链接:

PAT-ADVANCED1066:https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888

Data Structures and Algorithms7-6:https://pintia.cn/problem-sets/16/problems/668

题目描述:

PAT-ADVANCED1066、Data Structures and Algorithms7-6:

题目翻译:

AVL树是一棵自平衡的二分搜索树。在AVL树中,左右子树的高度差不会超过1;任何时刻如果左右子树的高度差超过了1,自平衡操作会使得其维持左右子树高度差不超过1这个性质。下图展示了旋转规则。

现在给你一串插入序列,你需要输出由该插入顺序构成的AVL树的根结点。

输入格式:

每个输入文件包含一个测试用例。在每个测试用例中,第一行有一个正整数N(<= 20)。第二行有N个不同的整数。一行中的所有数字由一个空格分隔。

输出格式:

对每个测试用例,输出该AVL树的根节点。

输入样例1:

5
88 70 61 96 120

输出样例1:

70

输入样例2:

7
88 70 61 96 120 90 65

输出样例2:

88

知识点:构建AVL树

思路一:按插入顺序构建AVL树(静态数组实现)

构建AVL树的思路和PAT-ADVANCED1123——Is It a Complete AVL Tree一模一样。

时间复杂度和空间复杂度均是O(N)。

C++代码:

#include<iostream>

using namespace std;

struct node{
	int data, height, lchild, rchild;
};

node Node[20];
int N, nums[20], index = 0;

int newNode(int num);
int getHeight(int root);
int getBalanceFactor(int root);
void updateHeight(int root);
void turnLeft(int &root);
void turnRight(int &root);
void insert(int &root, int num);
int create();

int main(){
	scanf("%d", &N);
	for(int i = 0; i < N; i++){
		scanf("%d", &nums[i]);
	}
	int root = create();
	printf("%d\n", Node[root].data);
	return 0;
} 

int newNode(int num){
	int root = index++;
	Node[root].data = num;
	Node[root].height = 1;
	Node[root].lchild = Node[root].rchild = -1;
	return root;
}

int getHeight(int root){
	if(root == -1){
		return 0;
	}
	return Node[root].height;
}

int getBalanceFactor(int root){
	return getHeight(Node[root].lchild) - getHeight(Node[root].rchild);
}

void updateHeight(int root){
	Node[root].height = max(getHeight(Node[root].lchild), getHeight(Node[root].rchild)) + 1;
}

void turnLeft(int &root){
	int temp = Node[root].rchild;
	Node[root].rchild = Node[temp].lchild;
	Node[temp].lchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

void turnRight(int &root){
	int temp = Node[root].lchild;
	Node[root].lchild = Node[temp].rchild;
	Node[temp].rchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

void insert(int &root, int num){
	if(root == -1){
		root = newNode(num);
		return;
	}
	if(Node[root].data < num){
		insert(Node[root].rchild, num);
		updateHeight(root);
		if(getBalanceFactor(root) == -2){
			if(getBalanceFactor(Node[root].rchild) == -1){
				turnLeft(root);
			}else if(getBalanceFactor(Node[root].rchild) == 1){
				turnRight(Node[root].rchild);
				turnLeft(root);
			}
		}
	}else{
		insert(Node[root].lchild, num);
		updateHeight(root);
		if(getBalanceFactor(root) == 2){
			if(getBalanceFactor(Node[root].lchild) == 1){
				turnRight(root);
			}else if(getBalanceFactor(Node[root].lchild) == -1){
				turnLeft(Node[root].lchild);
				turnRight(root);
			}
		}
	}
}

int create(){
	int root = -1;
	for(int i = 0; i < N; i++){
		insert(root, nums[i]);
	}
	return root;
}

C++解题报告:

思路二:按插入顺序构建AVL树(指针实现)

构建AVL树的思路和PAT-ADVANCED1123——Is It a Complete AVL Tree一模一样。

时间复杂度和空间复杂度均是O(N)。

C++代码:

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct node {
	int data;
	int height;
	node* lchild;
	node* rchild;
};

int N;
vector<int> input;

node* newNode(int num);
int getHeight(node* root);
int getBalanceFactor(node* root);
void updateHeight(node* root);
void leftTurn(node* &root);
void rightTurn(node* &root);
void insert(node* &root, int num);
node* create();

int main(){
	cin >> N;
	int num;
	for(int i = 0; i < N; i++){
		cin >> num;
		input.push_back(num);
	}
	node* root = create();
	cout << root->data << endl;
	return 0;
}

node* newNode(int num){
	node* root = new node;
	root->data = num;
	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 leftTurn(node* &root){
	node* temp = root->rchild;
	root->rchild = temp->lchild;
	temp->lchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

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

void insert(node* &root, int num){
	if(root == NULL){
		root = newNode(num);
		return;
	}
	if(num < root->data){
		insert(root->lchild, num);
		updateHeight(root);
		if(getBalanceFactor(root) == 2){
			if(getBalanceFactor(root->lchild) == 1){
				rightTurn(root);
			}else if(getBalanceFactor(root->lchild) == -1){
				leftTurn(root->lchild);
				rightTurn(root);
			}
		} 
	}else{
		insert(root->rchild, num);
		updateHeight(root); 
		if(getBalanceFactor(root) == -2){
			if(getBalanceFactor(root->rchild) == -1){
				leftTurn(root);
			}else if(getBalanceFactor(root->rchild) == 1){
				rightTurn(root->rchild);
				leftTurn(root);
			}
		}
	}
}

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

C++解题报告:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值